FileDocCategorySizeDatePackage
ByteBuffer.javaAPI DocphoneME MR2 API (J2ME)22108Wed May 02 17:59:48 BST 2007com.sun.cldchi.tools.memoryprofiler.jdwp

ByteBuffer

public class ByteBuffer extends Object
This class encapsulates dynamic linear byte array that is handled as stream. This class is a superclass for Packet that encapsulates JDWP packet. ByteBuffer class contains all the generic (not JDWP specific) methods to work with JDWP packet-kind structures.
see
jdwp.Packet
see
jdwp.Command
see
jdwp.Reply

Fields Summary
private int
Delta
The size that buffer increases if necessary
private int
CurrentSize
The current size of filled part of the buffer
int
parseOffset
The index of the byte that will be read next
protected byte[]
bytes
The contents of the buffer
Constructors Summary
public ByteBuffer()
Constructs a new ByteBuffer object with default initial size and delta.

see
ByteBuffer#Delta

		this(128, 128);
    
public ByteBuffer(int InitialSize, int Delta)
Constructs a new ByteBuffer object with specified initial size and delta.

param
InitialSize initial size of the buffer
param
Delta the size that buffer increases if necessary
see
ByteBuffer#Delta

	    if (Delta <= 0)
	    	Delta = 16;
		this.Delta = Delta;
		CurrentSize = 0;
		bytes = new byte[InitialSize];
		parseOffset = 0;
    
Methods Summary
public voidaddByte(int b)
Adds byte to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased.

param
b a byte to be appended

    	checkSpace(1);

    	int where = CurrentSize;
        CurrentSize++;

    	try {
	        putByte(where, b);
	    }
	    catch (BoundException e) {};
    
public voidaddBytes(byte[] b, int start, int len)
Adds a number of bytes to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased.

param
b a byte array the data should be appended from
param
start the index in the byte array of the first byte that should be appended
param
len a number of bytes that should be added to the buffer

    	checkSpace(len);

    	int where = CurrentSize;
        CurrentSize = CurrentSize + len;

    	try {
	        putBytes(where, b, start, len);
	    }
	    catch (BoundException e) {};
    
public voidaddID(long l, int count)
Adds an integer value (int, short, long) to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased. The value is stored in the little endian format.

param
l a value to be appended
param
count a size of the value in bytes (2 for short, 4 for int, 8 for long)

        checkSpace(count);

    	int where = CurrentSize;
        CurrentSize = CurrentSize + count;

    	try {
	        putID(where, l, count);
	    }
	    catch (BoundException e) {};
    
public voidaddInt(int b)
Adds int to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased. The value is stored in little endian format.

param
b a value to be appended

		addID(b, 4);		
    
public voidaddLong(long l)
Adds long to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased. The value is stored in little endian format.

param
b a value to be appended

		addID(l, 8);
    
public voidaddShort(int b)
Adds short to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased. The value is stored in little endian format.

param
b a value to be appended

		addID(b, 2);		
    
public voidaddString(java.lang.String s)
Adds String to the end of the filled part of the buffer. If there is no place to store this data the buffer is automatically increased. The value is stored in UTF-8 format.

param
s a string to be appended

                
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                try{
                    dos.writeUTF(s);
                }catch(IOException e ){
                    System.err.println("Error creating the UTF-8 sring");
                    return;
                }
                byte[] buf = baos.toByteArray();
                int len = ((buf[0] << 8) & 0xFF00) | (buf[1] & 0xFF);
                addByte(0);
                addByte(0);
		addBytes(buf, 0, len + 2);
    
private voidcheckSpace(int Space)
Checks if the buffer is large enough to place the specified number of bytes. If no, the buffer is automatically increased to necessary size.

param
Space a number of bytes that it's necessary to place

		if (bytes.length >= CurrentSize + Space) 
			return;

		int NewSize = bytes.length;
		while (NewSize < CurrentSize + Space)
			NewSize = NewSize + Delta;

		byte[] NewData = new byte[NewSize];

		for (int i = 0; i < CurrentSize; i++)
			NewData[i] = bytes[i];

		bytes = NewData;
    
public voiddeleteBytes(int count)
Deletes a few bytes from the beginning of the buffer and copies the last part to the beginning of the buffer.

param
count a number of bytes to be deleted from the head of the buffer

        int j = 0;
        while (count < CurrentSize)
        	bytes[j++] = bytes[count++];
        CurrentSize = j;
    
public intgetByte()
Tries to read next byte from the buffer. Byte is to read is one that is pointed by reading marker. After completing the operation the reading marker is incremented.

return
current byte from the buffer
throws
BoundException if byte to be read is outside the filled area

    	if (parseOffset >= CurrentSize)
    		throw new BoundException();

		return (int) (bytes[parseOffset++] & 0xFF);
    
public intgetByte(int off)
Tries to read a single byte from the buffer. Byte is to read is located at specified index.

param
off the index of the buffer where the required byte is located
return
a required byte from the buffer
throws
BoundException if byte to be read is outside the filled area

        int old_offset = parseOffset;
        parseOffset = off;

        int r = getByte();
        parseOffset = old_offset;
        return r;
    
public longgetID(int count)
Tries to read next integer value (short, int, long) from the buffer. Value is read is one that is pointed by reading marker. After completing the operation the reading marker is incremented. The value is stored in little endian format.

param
count a size in bytes of integer value (2 for short, 4 for int, 8 for long)
return
current integer value from the buffer
throws
BoundException if value to be read is outside the filled area


		if ((count <= 0) || (count > 8))
			throw new BoundException();

		long l = 0;
		for (int i = 0; i < count; i++)
			l = (l * 0x100) + getByte();
		return l;
    
public longgetID(int off, int count)
Tries to read an integer value (short, int, long) from the buffer. Value is to read is located at specified index. The value is stored in little endian format.

param
off the index of the buffer where the required value is located
param
count a size in bytes of integer value (2 for short, 4 for int, 8 for long)
return
a required value from the buffer
throws
BoundException if value to be read is outside the filled area

        int old_offset = parseOffset;
        parseOffset = off;

        long l = getID(count);
        parseOffset = old_offset;
        return l;
    
public intgetInt()
Tries to read next int from the buffer. Value is read is one that is pointed by reading marker. After completing the operation the reading marker is incremented. The value is stored in little endian format.

return
current int value from the buffer
throws
BoundException if value to be read is outside the filled area

		return (int) getID(4);
    
public intgetInt(int off)
Tries to read an int value from the buffer. Value is to read is located at specified index. The value is stored in little endian format.

param
off the index of the buffer where the required value is located
return
a required value from the buffer
throws
BoundException if value to be read is outside the filled area

	return (int) getID(off, 4);
    
public longgetLong()
Tries to read next long from the buffer. Value is read is one that is pointed by reading marker. After completing the operation the reading marker is incremented. The value is stored in little endian format.

return
current long value from the buffer
throws
BoundException if value to be read is outside the filled area

		return getID(8);
    
public longgetLong(int off)
Tries to read an long value from the buffer. Value is to read is located at specified index. The value is stored in little endian format.

param
off the index of the buffer where the required value is located
return
a required value from the buffer
throws
BoundException if value to be read is outside the filled area

	return getID(off, 8);
    
public intgetShort()
Tries to read next short from the buffer. Value is read is one that is pointed by reading marker. After completing the operation the reading marker is incremented. The value is stored in little endian format.

return
current short value from the buffer
throws
BoundException if value to be read is outside the filled area

		return (int) getID(2);
    
public intgetShort(int off)
Tries to read an short value from the buffer. Value is to read is located at specified index. The value is stored in little endian format.

param
off the index of the buffer where the required value is located
return
a required value from the buffer
throws
BoundException if value to be read is outside the filled area

	return (int) getID(off, 2);
    
public java.lang.StringgetString()
Tries to read next string from the buffer. Value is read is one that is pointed by reading marker. After completing the operation the reading marker is incremented. The value is stored in UTF-8 format.

return
current UTF-8 string from the buffer
throws
BoundException if value to be read is outside the filled area

		int l = getInt();
		if (l < 0)
    		throw new BoundException();
                if (l == 0)
                    return "";

                byte[] d = new byte[l+2];
                d[0] = (byte)((l >> 8) & 0xFF);
                d[1] = (byte)(l & 0xFF);
		for (int i = 0; i < l; i++){                        
                    d[i+2] = (byte) getByte();                        
                }
                
                ByteArrayInputStream bais = new ByteArrayInputStream(d);
                DataInputStream dis = new DataInputStream(bais);
                
                try{
                    String res = dis.readUTF();
                    return res;
                }catch(IOException e){
                    throw new BoundException(e.getMessage());
                }
    
public java.lang.StringgetString(int off)
Tries to read a string from the buffer. The string is to read is located at specified index. The value is stored in UTF-8 format.

param
off the index of the buffer where the required string is located
return
a required string from the buffer
throws
BoundException if string to be read is outside the filled area


        int old_offset = parseOffset;
        parseOffset = off;

        String s = getString();

        parseOffset = old_offset;

        return s;
    
public booleanisParsed()
Checks if reading marker points to the end of filled data of the buffer. This method is analogue of eof method for the files.

return
true if reading marker points to the end of filled data, false otherwise

    	return (parseOffset == CurrentSize);
    
public intlength()
Returns length of the filled part of the buffer

return
length of the filled part of the buffer

    	return CurrentSize;
    
public voidputByte(int off, int b)
Stores byte at the specified index in the buffer. If specified index is outside the filled area of the buffer BoundException is raised.

param
off index where byte should be placed
param
b byte that should be placed
throws
BoundException if specified index is outside the filled area of the buffer

    	if ((off < 0) || (off >= CurrentSize))
    		throw new BoundException();

        bytes[off] = (byte) (b & 0xFF);
    
public voidputBytes(int off, byte[] b, int start, int len)
Stores a number of bytes at the specified location of the buffer. If at least one byte is to be stored outside the already filled area of the buffer BoundException is raised.

param
off index in the buffer where the first byte should be stored
param
b array of bytes whose values should be stored in the buffer
param
start the first index in the array of bytes that should be stored
param
len the number of bytes that should be stored in the buffer
throws
BoundException if at least one byte should be stored outside the already filled area of the buffer

        for (int i = 0; i < len; i++)
			putByte(off++, b[start++]);
    
public voidputID(int off, long l, int count)
Stores an integer value (short, int, long) in the buffer. The value is stored in Java (little endian) format. If at least one byte of the value is to be placed outside the alrady filled area of the buffer BoundException is raised

param
off index where the first byte of the value should be stored
param
l a value that should be stored
param
count length of the value (2 for short, 4 for int, 8 for long)
throws
BoundException if at least one byte of the value should be stored outside the already filled area of the buffer


		if ((count <= 0) || (count > 8))
			throw new BoundException();
	
		int shift = (count - 1) * 8;

		for (int i = 0; i < count; i++) {
			putByte(off++, (int) ((l >>> shift) & 0xFF));
			shift = shift - 8;
        }
    
public voidputInt(int off, int b)
Stores int value in the buffer starting from specified index. The value is stored in little endian format. If at least one byte of the value is to be placed outside the alrady filled area of the buffer BoundException is raised.

param
off index where the first byte of the value should be stored
param
b the value that should be stored
throws
BoundException if at least one byte of the value should be stored outside the already filled area of the buffer

		putID(off, b, 4);		
    
public voidputLong(int off, long l)
Stores long value in the buffer starting from specified index. The value is stored in little endian format. If at least one byte of the value is to be placed outside the alrady filled area of the buffer BoundException is raised.

param
off index where the first byte of the value should be stored
param
b the value that should be stored
throws
BoundException if at least one byte of the value should be stored outside the already filled area of the buffer

		putID(off, l, 8);
    
public voidputShort(int off, int b)
Stores short value in the buffer starting from specified index. The value is stored in little endian format. If at least one byte of the value is to be placed outside the alrady filled area of the buffer BoundException is raised.

param
off index where the first byte of the value should be stored
param
b the value that should be stored
throws
BoundException if at least one byte of the value should be stored outside the already filled area of the buffer

		putID(off, b, 2);		
    
public voidresetBuffer()
Clears the buffer by setting the size of the filled area to zero. The reading marker's position is not affected by this method so if the reading marker was not positioned to the beginning of the buffer it'll become to be in invalid position.

        CurrentSize = 0;
    
public voidresetParser()
Moves the reading marker to the beginning of the buffer. Typically the process of reading data from the buffer is organized as follows: at first move the reading marker to the some start position (for example, in the beginning of the buffer) and then consequently read the data using get methods (for example, getInt()). So the process of reading data from the byte buffer is looks like reading data from generic stream (for example, from file).

		parseOffset = 0;
    
public voidresetParser(int i)
Move the reading marker to the specified position.

param
i index of the buffer where reading marker should point

		parseOffset = i;
    
public java.lang.StringtoString(int start)
Returns string representation of the contents of the buffer from the specified index. This method is invoked by KJDB when JDWP reply packet is not received (usually it's a fatal error)and this information is useful for localizing the problem.

param
start the first index that should be printed
return
string representation of a part of the byte buffer
see
jdwp.BackEndTest#printReplies


	String Result = "", HexLine = "", DisplayLine = "";

	int j = 0;

        for (int i = start; i < length(); i++) {

			HexLine = HexLine + Tools.Hex(bytes[i], 2) + " ";

			if (bytes[i] >= 0 && bytes[i] < 32)
				DisplayLine = DisplayLine + ".";
			else
				DisplayLine = DisplayLine + new String(bytes, i, 1);

			if ((i == length() - 1) || (((i - start) & 0x0F) == 0x0F)) {
				Result = Result +
						 Tools.Hex(j, 4) + ": " +
						 Tools.PadR(HexLine, 48) + "  " +
						 DisplayLine + "\n";
				HexLine = "";
				DisplayLine = "";
				j = j + 16;
			}
        }
		return Result;
    
public java.lang.StringtoString()
Returns string representation of the object. Currently this method is not used by KJDB but it may be useful for debugging purposes.

return
string representation of the object

		return toString(0);