jarol.messages
Class Buffer

java.lang.Object
  extended by jarol.messages.Buffer

public class Buffer
extends java.lang.Object

Buffer class for serialization.

This class defines the basic methods for interfacing with a buffer encoded in big endian or small endian byte order.

It is essentially an extension wrapper for java.nio.ByteBuffer which cannot be subclassed due to the particular mechanism of creating ByteBuffer objects (ie factory methods), thus the wrapper functionality (instead of subclassing)

.

Note: The buf field is protected for fast access through eventual subclasses.

Version:
0.1
Author:
Eduardo Marques

Field Summary
protected  java.nio.ByteBuffer buf
          Internal ByteBuffer instance, can be accessed by subclasses.
 
Constructor Summary
Buffer(byte[] data)
          Constructor from a data byte array.
Buffer(byte[] data, java.nio.ByteOrder order)
          Constructor from a data byte array and specified byte order.
Buffer(byte[] data, int off, int len)
          Constructor from a data byte array, plus specified offset and usable length.
Buffer(byte[] data, int off, int len, java.nio.ByteOrder order)
          Constructor from a data byte array, plus specified offset, usable length and byte order to use.
 
Method Summary
 java.nio.ByteOrder getByteOrder()
          Get byte order in use (big endian or little endian).
 byte[] getData()
          Get data array.
 int position()
          Get buffer position.
 float readFP32()
          Read a 32-bit floating point value from the buffer.
 double readFP64()
          Read a 64-bit floating point value from the buffer.
 short readINT16()
          Read a signed 16 bit integer from the buffer.
 int readINT32()
          Read a signed 32 bit integer from the buffer.
 long readINT64()
          Read a signed 64 bit integer from the buffer.
 byte readINT8()
          Read a signed 8 bit integer from the buffer.
 java.lang.String readPlainText(boolean encodedLength)
          Read plain text (string) data with optional length prefix specifier.
 byte[] readRawData()
          Read raw data.
 int readUINT16()
          Read an unsigned 16 bit integer from the buffer.
 long readUINT32()
          Read an unsigned 32 bit integer from the buffer.
 long readUINT64()
          Read an unsigned 64 bit integer from the buffer (fake).
 short readUINT8()
          Read an unsigned 8 bit integer from the buffer.
 void reset(byte[] data, int off, int len, java.nio.ByteOrder order)
          Reset buffer.
 void rewind()
          Rewind buffer to starting position.
 void setByteOrder(java.nio.ByteOrder order)
          Change byte order in use.
 void writeFP32(float f32)
          Write 32-bit floating point value onto buffer.
 void writeFP64(double f64)
          Write 64-bit floating point value onto buffer.
 void writeINT16(short i16)
          Write 16-bit signed integer value onto buffer.
 void writeINT32(int i32)
          Write 32-bit signed integer value onto buffer.
 void writeINT64(long i64)
          Write 64-bit signed integer value onto buffer.
 void writeINT8(byte i8)
          Write 8-bit signed integer value onto buffer.
 void writePlainText(java.lang.String text, boolean encodeLength)
          Write plain text with optional 2-byte length prefix specifier.
 void writeRawData(byte[] data)
          Write raw data with 2-byte length prefix specifier.
 void writeUINT16(int u16)
          Write 16-bit unsigned integer value onto buffer.
 void writeUINT32(long u32)
          Write 32-bit unsigned integer value onto buffer.
 void writeUINT64(long u64)
          Write 64-bit unsigned integer value onto buffer.
 void writeUINT8(short u8)
          Write 8-bit unsigned integer value onto buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buf

protected java.nio.ByteBuffer buf
Internal ByteBuffer instance, can be accessed by subclasses.

Constructor Detail

Buffer

public Buffer(byte[] data)
Constructor from a data byte array. Little Endian byte order will be used by default.

Parameters:
data - the source data

Buffer

public Buffer(byte[] data,
              int off,
              int len)
Constructor from a data byte array, plus specified offset and usable length. Little Endian byte order will be used by default.

Parameters:
data - the source data
off - the source data offset
len - the source data length

Buffer

public Buffer(byte[] data,
              java.nio.ByteOrder order)
Constructor from a data byte array and specified byte order.

Parameters:
data - the source data
order - byte order to use

Buffer

public Buffer(byte[] data,
              int off,
              int len,
              java.nio.ByteOrder order)
Constructor from a data byte array, plus specified offset, usable length and byte order to use.

Parameters:
data - the source data
off - the source data offset
len - the source data length
order - the byte order to use
Method Detail

reset

public void reset(byte[] data,
                  int off,
                  int len,
                  java.nio.ByteOrder order)
Reset buffer.

Parameters:
data - the source data
off - the source data offset
len - the source data length
order - the byte order to use

getData

public byte[] getData()
Get data array.

Returns:
the byte array

rewind

public void rewind()
Rewind buffer to starting position.


setByteOrder

public void setByteOrder(java.nio.ByteOrder order)
Change byte order in use.

Parameters:
order - the byte order to use (little endian or big endian)

getByteOrder

public java.nio.ByteOrder getByteOrder()
Get byte order in use (big endian or little endian).

Returns:
byte order in use.

position

public final int position()
Get buffer position.


readINT8

public final byte readINT8()
Read a signed 8 bit integer from the buffer.

Returns:
8-bit signed value read

readUINT8

public final short readUINT8()
Read an unsigned 8 bit integer from the buffer.

Returns:
8-bit unsigned value read

readINT16

public final short readINT16()
Read a signed 16 bit integer from the buffer.

Returns:
16-bit signed value read

readUINT16

public final int readUINT16()
Read an unsigned 16 bit integer from the buffer.

Returns:
16-bit unsigned value read

readINT32

public final int readINT32()
Read a signed 32 bit integer from the buffer.

Returns:
32-bit signed value read

readUINT32

public final long readUINT32()
Read an unsigned 32 bit integer from the buffer.

Returns:
32-bit unsigned value read

readINT64

public final long readINT64()
Read a signed 64 bit integer from the buffer.

Returns:
64-bit signed value read

readUINT64

public final long readUINT64()
Read an unsigned 64 bit integer from the buffer (fake).
This method does not actually return unsigned 64-bit values, but 64-bit signed values (ie the Java 'long' scalar type).
TODO: possibly handle this another way or implement it with big integer class.

Returns:
64-bit signed value read

readFP32

public final float readFP32()
Read a 32-bit floating point value from the buffer.

Returns:
32-bit floating point value read

readFP64

public final double readFP64()
Read a 64-bit floating point value from the buffer.

Returns:
64-bit floating point value read

readRawData

public final byte[] readRawData()
Read raw data. Raw bytes are prefixed by an unsigned 16-bit header stating the data length.

Returns:
null for a 0 byte-buffer, a byte array otherwise

readPlainText

public final java.lang.String readPlainText(boolean encodedLength)
Read plain text (string) data with optional length prefix specifier.
If 'encodedLength' equals true then String contents are prefixed by an unsigned 16-bit header stating the string length. Otherwise the buffer will be read until the null character is found.
Note that current implementation will decode strings using the ISO-8859-1 charset.

Parameters:
encodedLength - encoded length flag
Returns:
string value

writeINT8

public final void writeINT8(byte i8)
Write 8-bit signed integer value onto buffer.

Parameters:
i8 - 8-bit signed integer value

writeUINT8

public final void writeUINT8(short u8)
Write 8-bit unsigned integer value onto buffer.

Parameters:
u8 - 8-bit unsigned integer value (8-bit truncation will be made)

writeINT16

public final void writeINT16(short i16)
Write 16-bit signed integer value onto buffer.

Parameters:
i16 - 16-bit signed integer value

writeUINT16

public final void writeUINT16(int u16)
Write 16-bit unsigned integer value onto buffer.

Parameters:
u16 - 16-bit signed integer value (truncation to 16 bits will be made)

writeINT32

public final void writeINT32(int i32)
Write 32-bit signed integer value onto buffer.

Parameters:
i32 - 32-bit signed integer value

writeUINT32

public final void writeUINT32(long u32)
Write 32-bit unsigned integer value onto buffer.

Parameters:
u32 - 32-bit signed integer value (truncation to 32 bits will be made)

writeINT64

public final void writeINT64(long i64)
Write 64-bit signed integer value onto buffer.

Parameters:
i64 - 64-bit signed integer value

writeUINT64

public final void writeUINT64(long u64)
Write 64-bit unsigned integer value onto buffer. The parameter is actually a 64-bit signed integer, which may be understood as an unsigned 64-bit value at a receiver's end in C/C++ for example

Parameters:
u64 - 64-bit signed integer value

writeFP32

public final void writeFP32(float f32)
Write 32-bit floating point value onto buffer.

Parameters:
f32 - 32-bit floating point value

writeFP64

public final void writeFP64(double f64)
Write 64-bit floating point value onto buffer.

Parameters:
f64 - 64-bit floating point value

writeRawData

public final void writeRawData(byte[] data)
Write raw data with 2-byte length prefix specifier. The outcome onto the buffer will be an unsigned 16-bit value encoding the data length followed by the actual data.

Parameters:
data - to write

writePlainText

public final void writePlainText(java.lang.String text,
                                 boolean encodeLength)
Write plain text with optional 2-byte length prefix specifier.
The outcome onto the buffer will be, if 'encodeLength' is true, an unsigned 16-bit value encoding the string length followed by the actual text. Otherwise, only the text data will be written followed by the null terminator.
Note that current implementation will encode strings using the ISO-8859-1 charset.

Parameters:
text - text to write
encodeLength - indicate that length should be encoded as a 2-byte prefix