com.sun.sgs.impl.sharedutil
Class MessageBuffer

java.lang.Object
  extended by com.sun.sgs.impl.sharedutil.MessageBuffer

public class MessageBuffer
extends Object

A buffer for composing/decomposing messages.

Strings are encoded in modified UTF-8 format as described in DataInput.


Constructor Summary
MessageBuffer(byte[] buf)
          Constructs a message buffer using the specified byte array as the byte array that backs this buffer.
MessageBuffer(int capacity)
          Constructs an empty message buffer with the specified capacity.
 
Method Summary
 int capacity()
          Returns the capacity of this buffer.
 byte[] getBuffer()
          Returns the byte array that backs this buffer.
 byte getByte()
          Returns the byte in this buffer's current position, and advances the buffer's position by one.
 byte[] getByteArray()
          Returns a byte array encoded as a 2-byte length followed by the bytes, starting at this buffer's current position, and advances the buffer's position by the number of bytes obtained.
 byte[] getBytes(int size)
          Returns a byte array containing the specified number of bytes, starting at this buffer's current position, and advances the buffer's position by the number of bytes obtained.
 char getChar()
          Returns a char, composed of the next two bytes (high byte first) in this buffer, and advances the buffer's position by two.
 int getInt()
          Returns an int value, composed of the next four bytes (high byte first) in this buffer, and advances the buffer's position by 4.
 long getLong()
          Returns a long value, composed of the next eight bytes (high byte first) in this buffer, and advances the buffer's position by 8.
 short getShort()
          Returns a short value, composed of the next two bytes (high byte first) in this buffer, and advances the buffer's position by two.
static int getSize(String str)
          Returns the size of the specified string, encoded in modified UTF-8 format.
 String getString()
          Returns a string that has been encoded in modified UTF-8 format, starting at the buffer's current position, and advances the buffer's position by the length of the encoded string.
 int getUnsignedShort()
          Returns an unsigned short value (as an int), composed of the next two bytes (high byte first) in this buffer, and advances the buffer's position by two.
 int limit()
          Returns the limit of this buffer.
 int position()
          Returns the position of this buffer.
 MessageBuffer putByte(int b)
          Puts the specified byte in this buffer's current position, and advances the buffer's position and limit by one.
 MessageBuffer putByteArray(byte[] bytes)
          Puts into this buffer a short representing the length of the specified byte array followed by the bytes from the specified byte array, starting at the buffer's current position.
 MessageBuffer putBytes(byte[] bytes)
          Puts the bytes from the specified byte array in this buffer, starting at the buffer's current position.
 MessageBuffer putChar(int v)
          Puts the specified char as a two-byte value (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by two.
 MessageBuffer putInt(int v)
          Puts the specified int as four bytes (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by 4.
 MessageBuffer putLong(long v)
          Puts the specified long as eight bytes (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by 8.
 MessageBuffer putShort(int v)
          Puts the specified short as a two-byte value (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by two.
 MessageBuffer putString(String str)
          Puts the specified string, encoded in modified UTF-8 format, in the buffer starting in the buffer's the current position, and advances the buffer's position and limit by the size of the encoded string.
 void rewind()
          Sets the position of this buffer to zero, making this buffer ready for re-reading of its elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageBuffer

public MessageBuffer(int capacity)
Constructs an empty message buffer with the specified capacity.

Parameters:
capacity - the buffer's capacity

MessageBuffer

public MessageBuffer(byte[] buf)
Constructs a message buffer using the specified byte array as the byte array that backs this buffer. Intializes this buffer's capacity and limit to the length of the specified byte array.

Parameters:
buf - the byte array to back this buffer
Method Detail

getSize

public static int getSize(String str)
Returns the size of the specified string, encoded in modified UTF-8 format.

Parameters:
str - a string
Returns:
the size of the specified string, encoded in modified UTF-8 format

capacity

public int capacity()
Returns the capacity of this buffer. The capacity is the number of elements this buffer contains.

Returns:
this buffer's capacity

limit

public int limit()
Returns the limit of this buffer. The limit is the index of the first element that should not be written or read. The limit is never negative and is never greater than the buffer's capacity.

Returns:
this buffer's limit

position

public int position()
Returns the position of this buffer. The position is the index of the next element to be written or read.

Returns:
this buffer's position

rewind

public void rewind()
Sets the position of this buffer to zero, making this buffer ready for re-reading of its elements.


putByte

public MessageBuffer putByte(int b)
Puts the specified byte in this buffer's current position, and advances the buffer's position and limit by one.

Parameters:
b - a byte
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the byte to the buffer would overflow the buffer

putByteArray

public MessageBuffer putByteArray(byte[] bytes)
Puts into this buffer a short representing the length of the specified byte array followed by the bytes from the specified byte array, starting at the buffer's current position. The buffer's position and limit are advanced by the length of the specified byte array plus two.

Parameters:
bytes - a byte array
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the bytes to this buffer would overflow the buffer

putBytes

public MessageBuffer putBytes(byte[] bytes)
Puts the bytes from the specified byte array in this buffer, starting at the buffer's current position. The buffer's position and limit are advanced by the length of the specified byte array.

Parameters:
bytes - a byte array
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the bytes to this buffer would overflow the buffer

putChar

public MessageBuffer putChar(int v)
Puts the specified char as a two-byte value (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by two.

Parameters:
v - a char value
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the char to this buffer would overflow the buffer

putShort

public MessageBuffer putShort(int v)
Puts the specified short as a two-byte value (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by two.

Parameters:
v - a short value
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the short to this buffer would overflow the buffer

putInt

public MessageBuffer putInt(int v)
Puts the specified int as four bytes (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by 4.

Parameters:
v - an int value
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the int to this buffer would overflow the buffer

putLong

public MessageBuffer putLong(long v)
Puts the specified long as eight bytes (high byte first) starting in the buffer's current position, and advances the buffer's position and limit by 8.

Parameters:
v - a long value
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the long to this buffer would overflow the buffer

putString

public MessageBuffer putString(String str)
Puts the specified string, encoded in modified UTF-8 format, in the buffer starting in the buffer's the current position, and advances the buffer's position and limit by the size of the encoded string.

Parameters:
str - a string
Returns:
this buffer
Throws:
IndexOutOfBoundsException - if adding the encoded string to this buffer would overflow the buffer

getByte

public byte getByte()
Returns the byte in this buffer's current position, and advances the buffer's position by one.

Returns:
the byte at the buffer's current position
Throws:
IndexOutOfBoundsException - if this buffer's limit has been reached

getByteArray

public byte[] getByteArray()
Returns a byte array encoded as a 2-byte length followed by the bytes, starting at this buffer's current position, and advances the buffer's position by the number of bytes obtained.

Returns:
a byte array with the bytes from this buffer
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the encoded bytes

getBytes

public byte[] getBytes(int size)
Returns a byte array containing the specified number of bytes, starting at this buffer's current position, and advances the buffer's position by the number of bytes obtained.

Parameters:
size - the number of bytes to get from this buffer
Returns:
a byte array with the bytes from this buffer
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the specified number of bytes

getShort

public short getShort()
Returns a short value, composed of the next two bytes (high byte first) in this buffer, and advances the buffer's position by two.

Returns:
the short value
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the next two bytes

getUnsignedShort

public int getUnsignedShort()
Returns an unsigned short value (as an int), composed of the next two bytes (high byte first) in this buffer, and advances the buffer's position by two. The value returned is between 0 and 65535, inclusive.

Returns:
the unsigned short value as an int between 0 and 65535
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the next two bytes

getInt

public int getInt()
Returns an int value, composed of the next four bytes (high byte first) in this buffer, and advances the buffer's position by 4.

Returns:
the int value
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the next four bytes

getLong

public long getLong()
Returns a long value, composed of the next eight bytes (high byte first) in this buffer, and advances the buffer's position by 8.

Returns:
the long value
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the next eight bytes

getChar

public char getChar()
Returns a char, composed of the next two bytes (high byte first) in this buffer, and advances the buffer's position by two.

Returns:
the char
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the next two bytes

getString

public String getString()
Returns a string that has been encoded in modified UTF-8 format, starting at the buffer's current position, and advances the buffer's position by the length of the encoded string.

Returns:
the string
Throws:
IndexOutOfBoundsException - if this buffer's limit would be reached as a result of getting the encoded string

getBuffer

public byte[] getBuffer()
Returns the byte array that backs this buffer.

Returns:
the byte array that backs this buffer

Project Darkstar, Version 0.9.9.4
2013-07-29 20:49:18

Copyright © 2007-2013 Sun Microsystems, Inc. All rights reserved