com.sentilla.io
Class ByteBuffer

java.lang.Object
  extended by com.sentilla.io.ByteBuffer
All Implemented Interfaces:
DataInput, DataInputStream, DataOutput, DataOutputStream, Input, InputStream, ObjectInput, ObjectInputStream, ObjectOutput, ObjectOutputStream, Output, OutputStream, Serializable

public class ByteBuffer
extends Object
implements Serializable, ObjectInputStream, ObjectOutputStream

A stream access wrapper around a byte array. Tracks the size of the array contents and allows for platform-independent reading and writing of primitive types and Objects using stream access methods.

See Also:
Serialized Form

Field Summary
 byte[] data
          Data buffer
 int end
          End of the data in data
 int start
          Start of the data in data
 
Constructor Summary
ByteBuffer()
          Create a new zero-length byte buffer with no associated data array.
ByteBuffer(byte[] data, int start, int end)
          Wrap an existing byte array into a ByteBuffer.
ByteBuffer(int maxlength)
          Create (allocate) a new byte buffer of specified length.
 
Method Summary
 int available()
          Get the available number of remaining bytes in the byte buffer stream.
 void clear()
          Empty the buffer.
 void close()
          Close the byte buffer stream.
 int cmp(ByteBuffer b)
          Check if a given ByteBuffer is equal to this byte buffer.
 void flush()
          Flush the stream writes (commit).
 byte[] getData()
          Return a copy of the current buffer contents as a byte array.
 byte read()
          Read a byte.
 int read(byte[] b)
          Read bytes from the byte buffer stream into a byte array.
 int read(byte[] b, int offset, int length)
          Read a specified length of bytes from the byte buffer stream into a byte array starting at a given offset in the destination array.
 int read(ByteBuffer b, int length)
          Read a specified length of bytes from the byte buffer stream into another ByteBuffer
 boolean readBoolean()
          Read a boolean value from the buffer.
 byte readByte()
          Read a byte value from the buffer.
 char readChar()
          Read a char value from the buffer.
 double readDouble()
          Read a double value from the buffer.
 float readFloat()
          Read a float value from the buffer.
 int readInt()
          Read an int value from the buffer.
 long readLong()
          Read a long value from the buffer.
 Object readObject()
          Read a serialized object from the byte buffer.
 short readShort()
          Read a short value from the buffer.
 int readUnsignedByte()
          Read an unsigned byte value from the buffer.
 String readUTF()
          Read a String value from the buffer.
 void skip(int n)
          Skip bytes in the byte buffer stream.
 void write(byte[] b)
          Write bytes into the byte buffer.
 void write(byte[] b, int offset, int length)
          Write bytes into the byte buffer.
 void write(ByteBuffer b)
          Write bytes into the byte buffer.
 void write(ByteBuffer b, int length)
          Write bytes into the byte buffer.
 void write(int b)
          Write a byte into the byte buffer.
 void writeBoolean(boolean a)
          Write a boolean value to the buffer.
 void writeByte(int a)
          Write a byte value to the buffer.
 void writeChar(int a)
          Write a char value to the buffer.
 void writeDouble(double a)
          Write a double value to the buffer.
 void writeFloat(float a)
          Write a float value to the buffer.
 void writeInt(int a)
          Write an int value to the buffer.
 void writeLong(long a)
          Write a long value to the buffer.
 void writeObject(Object o)
          Write a serialized representation of the object into the byte buffer.
 void writeShort(int a)
          Write a short value to the buffer.
 void writeUTF(String s)
          Write a String object to the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

start

public int start
Start of the data in data


end

public int end
End of the data in data


data

public byte[] data
Data buffer

Constructor Detail

ByteBuffer

public ByteBuffer()
Create a new zero-length byte buffer with no associated data array.


ByteBuffer

public ByteBuffer(int maxlength)
Create (allocate) a new byte buffer of specified length.

Parameters:
maxlength - Length of the buffer.

ByteBuffer

public ByteBuffer(byte[] data,
                  int start,
                  int end)
Wrap an existing byte array into a ByteBuffer.

Parameters:
data - existing byte array
start - start buffer position
end - end buffer position
Method Detail

clear

public void clear()
Empty the buffer. Does not clear the data array.


getData

public byte[] getData()
Return a copy of the current buffer contents as a byte array.

Returns:
a new byte array with the contents of the byte buffer

close

public void close()
Close the byte buffer stream.

Specified by:
close in interface Input
Specified by:
close in interface Output

available

public int available()
Get the available number of remaining bytes in the byte buffer stream.

Specified by:
available in interface InputStream
Returns:
available bytes

skip

public void skip(int n)
          throws IORuntimeException
Skip bytes in the byte buffer stream.

Specified by:
skip in interface Input
Parameters:
n - number of bytes to skip
Throws:
IORuntimeException - if the number of bytes to skip is more than the available bytes in the stream

read

public byte read()
          throws IORuntimeException
Read a byte.

Specified by:
read in interface Input
Returns:
one byte from the byte buffer stream
Throws:
IORuntimeException - if the stream cannot be read or is at the end

read

public int read(byte[] b)
         throws IORuntimeException
Read bytes from the byte buffer stream into a byte array. Only as many bytes as will fit in the array are read, or until the end of the stream, whichever is first.

Specified by:
read in interface Input
Parameters:
b - array to read bytes into from the stream
Returns:
number of bytes read
Throws:
IORuntimeException - if an error occurs during the read process

read

public int read(ByteBuffer b,
                int length)
         throws IORuntimeException
Read a specified length of bytes from the byte buffer stream into another ByteBuffer

Parameters:
b - byte read destination
length - number of bytes to read
Returns:
number of bytes read
Throws:
IORuntimeException - if an error occurs during the read process

read

public int read(byte[] b,
                int offset,
                int length)
         throws IORuntimeException
Read a specified length of bytes from the byte buffer stream into a byte array starting at a given offset in the destination array.

Specified by:
read in interface Input
Parameters:
b - byte read destination
offset - position to start in the destination array b
length - number of bytes to read
Returns:
number of bytes read
Throws:
IORuntimeException - if an error occurs during the read process

write

public void write(int b)
           throws IORuntimeException
Write a byte into the byte buffer.

Specified by:
write in interface Output
Parameters:
b - byte to write
Throws:
IORuntimeException - if the write exceeds the maximum size of the buffer

write

public void write(byte[] b)
           throws IORuntimeException
Write bytes into the byte buffer.

Specified by:
write in interface Output
Parameters:
b - byte array to write
Throws:
IORuntimeException - if the write exceeds the maximum size of the buffer

write

public void write(ByteBuffer b,
                  int length)
           throws IORuntimeException
Write bytes into the byte buffer.

Parameters:
b - ByteBuffer to write
length - number of bytes to write
Throws:
IORuntimeException - if the write exceeds the maximum size of the buffer

write

public void write(ByteBuffer b)
           throws IORuntimeException
Write bytes into the byte buffer.

Parameters:
b - ByteBuffer to write, b.available() bytes will be written
Throws:
IORuntimeException - if the write exceeds the maximum size of the buffer

write

public void write(byte[] b,
                  int offset,
                  int length)
           throws IORuntimeException
Write bytes into the byte buffer.

Specified by:
write in interface Output
Parameters:
b - byte array to write
offset - offset in input array b where the write should start
length - number of bytes to write
Throws:
IORuntimeException - if the write exceeds the maximum size of the buffer

flush

public void flush()
           throws IORuntimeException
Flush the stream writes (commit).

Specified by:
flush in interface Output
Throws:
IORuntimeException - if an error occurs during the flush

readBoolean

public boolean readBoolean()
                    throws IORuntimeException
Read a boolean value from the buffer.

Specified by:
readBoolean in interface DataInput
Returns:
boolean value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readByte

public byte readByte()
              throws IORuntimeException
Read a byte value from the buffer.

Specified by:
readByte in interface DataInput
Returns:
byte value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readInt

public int readInt()
            throws IORuntimeException
Read an int value from the buffer.

Specified by:
readInt in interface DataInput
Returns:
int value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readLong

public long readLong()
              throws IORuntimeException
Read a long value from the buffer.

Specified by:
readLong in interface DataInput
Returns:
long value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readShort

public short readShort()
                throws IORuntimeException
Read a short value from the buffer.

Specified by:
readShort in interface DataInput
Returns:
short value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readUnsignedByte

public int readUnsignedByte()
                     throws IORuntimeException
Read an unsigned byte value from the buffer.

Specified by:
readUnsignedByte in interface DataInput
Returns:
unsigned byte value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readChar

public char readChar()
              throws IORuntimeException
Read a char value from the buffer.

Specified by:
readChar in interface DataInput
Returns:
char value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readFloat

public float readFloat()
                throws IORuntimeException
Read a float value from the buffer.

Specified by:
readFloat in interface DataInput
Returns:
float value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readDouble

public double readDouble()
                  throws IORuntimeException
Read a double value from the buffer.

Specified by:
readDouble in interface DataInput
Returns:
double value
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

readUTF

public String readUTF()
               throws IORuntimeException
Read a String value from the buffer.

Specified by:
readUTF in interface DataInput
Returns:
String object
Throws:
IORuntimeException - if no value can be read or the end of the buffer is reached

writeBoolean

public void writeBoolean(boolean a)
                  throws IORuntimeException
Write a boolean value to the buffer.

Specified by:
writeBoolean in interface DataOutput
Parameters:
a - boolean value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeByte

public void writeByte(int a)
               throws IORuntimeException
Write a byte value to the buffer.

Specified by:
writeByte in interface DataOutput
Parameters:
a - byte value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeInt

public void writeInt(int a)
              throws IORuntimeException
Write an int value to the buffer.

Specified by:
writeInt in interface DataOutput
Parameters:
a - int value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeLong

public void writeLong(long a)
               throws IORuntimeException
Write a long value to the buffer.

Specified by:
writeLong in interface DataOutput
Parameters:
a - long value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeShort

public void writeShort(int a)
                throws IORuntimeException
Write a short value to the buffer.

Specified by:
writeShort in interface DataOutput
Parameters:
a - short value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeChar

public void writeChar(int a)
               throws IORuntimeException
Write a char value to the buffer.

Specified by:
writeChar in interface DataOutput
Parameters:
a - char value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeFloat

public void writeFloat(float a)
                throws IORuntimeException
Write a float value to the buffer.

Specified by:
writeFloat in interface DataOutput
Parameters:
a - float value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeDouble

public void writeDouble(double a)
                 throws IORuntimeException
Write a double value to the buffer.

Specified by:
writeDouble in interface DataOutput
Parameters:
a - double value
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

writeUTF

public void writeUTF(String s)
              throws IORuntimeException
Write a String object to the buffer.

Specified by:
writeUTF in interface DataOutput
Parameters:
s - String object
Throws:
IORuntimeException - if the write operation exceeds the length of the buffer

cmp

public int cmp(ByteBuffer b)
Check if a given ByteBuffer is equal to this byte buffer.

Parameters:
b - buffer to compare
Returns:
0 if equal, otherwise the difference in available length

writeObject

public void writeObject(Object o)
Write a serialized representation of the object into the byte buffer. The main limitation of this method is that once a writeObject has been called on the ByteBuffer, no further writes shall take place, as they will not be readable by the mote-side ByteBuffer code. On success, it returns no value, on failure it throws an unchecked exception. The possible errors thrown are:

Specified by:
writeObject in interface ObjectOutput
Specified by:
writeObject in interface ObjectOutputStream
Parameters:
o - Object to write must implement java.io.Serializable

readObject

public Object readObject()
Read a serialized object from the byte buffer. Deserialization is done in place: the deserialized object will not consume new memory, instead it will use the memory allocated by this ByteBuffer. As a result, the current ByteBuffer will shrink in size; rather than reusing the ByteBuffer we recommend that the user reinstantiate the ByteBuffer. Under normal circustances, the method returns the object that is the deserialized. On error, we throw the following runtime exceptions and errors:


Copyright © 2007 Sentilla Corporation. All Rights Reserved.