Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 57dbcefd5fdd16e3734b7719d4937ea2e5420c9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 * Copyright (c) 2009 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Ted R Williams (Wind River Systems, Inc.) - initial implementation
 *******************************************************************************/

package org.eclipse.cdt.debug.ui.memory.transport;

import java.math.BigInteger;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IMemoryBlockExtension;

public class BufferedMemoryWriter 
{
	private IMemoryBlockExtension fBlock;
	private byte[] fBuffer;
	private int fBufferPosition = 0;
	private BigInteger fBufferStart = null;
	
	public BufferedMemoryWriter(IMemoryBlockExtension block, int bufferLength)
	{
		fBlock = block;
		fBuffer = new byte[bufferLength];
	}
	
	public void write(BigInteger address, byte[] data) throws DebugException
	{
		while(data.length > 0)
		{
			if(fBufferStart == null)
			{
				fBufferStart = address;
				int length = data.length <= fBuffer.length ? data.length : fBuffer.length;
				System.arraycopy(data, 0, fBuffer, 0, length);
				fBufferPosition = length;
				byte[] dataRemainder = new byte[data.length - length];
				System.arraycopy(data, length, dataRemainder, 0, data.length - length);
				data = dataRemainder;
				address = address.add(BigInteger.valueOf(length));
			}
			else if(fBufferStart.add(BigInteger.valueOf(fBufferPosition)).compareTo(address) != 0)
			{
				flush();
			}
			else
			{
				int availableBufferLength = fBuffer.length - fBufferPosition;
				int length = data.length <= availableBufferLength 
					? data.length : availableBufferLength;
				System.arraycopy(data, 0, fBuffer, fBufferPosition, length);
				fBufferPosition += length;
				
				byte[] dataRemainder = new byte[data.length - length];
				System.arraycopy(data, length, dataRemainder, 0, data.length - length);
				data = dataRemainder;
				address = address.add(BigInteger.valueOf(length));
			}
			
			if(fBufferPosition == fBuffer.length)
				flush();
		}
	}
	
	public void flush() throws DebugException
	{
		if(fBufferStart != null)
		{
			byte data[] = new byte[fBufferPosition];
			System.arraycopy(fBuffer, 0, data, 0, fBufferPosition);
			fBlock.setValue(fBufferStart, data);
			fBufferStart = null;
		}
	}

}


Back to the top