javax.baja.nre.util
Class ByteBuffer

java.lang.Object
  extended byjavax.baja.nre.util.ByteBuffer
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput

public class ByteBuffer
extends java.lang.Object
implements java.io.DataOutput, java.io.DataInput

ByteBuffer is a dynamically growable byte array which has implicit support for the DataOuput and DataInput interfaces.


Field Summary
protected  boolean bigEndian
          Flag which determines whether to use big or little endian byte ordering
protected  byte[] buffer
          Byte array buffer.
protected  int length
          Actual length of valid data in buffer.
protected  int pos
          Pos is used to store the next read index
 
Constructor Summary
ByteBuffer()
          Create a buffer with an inital capacity of 64 bytes.
ByteBuffer(byte[] buf)
          Create a ByteBuffer of length buf.length using the specified byte array for the internal buffer.
ByteBuffer(byte[] buf, int len)
          Create a ByteBuffer of length len using the specified byte array for the internal buffer.
ByteBuffer(int initialCapacity)
          Create a buffer with the specified inital capacity.
 
Method Summary
 int available()
          Return the number of bytes available based on the current read position and the buffer length.
 void dump()
          Dump the byte buffer to standard output.
 java.lang.String dumpToString()
          Dump the byte buffer to a String.
 boolean endsWith(byte[] b)
          Return true if the current byte buffer ends with the specified byte array.
 boolean endsWith(int b)
          Return true if the current byte buffer ends with the specified byte.
 byte[] getBytes()
          Get a direct reference to the internal buffer array.
 java.io.InputStream getInputStream()
          Get a new InputStream for reading the internal buffer.
 int getLength()
          Get the length of the buffer.
 java.io.OutputStream getOutputStream()
          Get a new OutputStream for the writing to the internal buffer.
 int getPosition()
          Get the current read position index.
 int indexOf(byte[] b)
          Get first the index of the specified byte array pattern or return -1 if the pattern is not found.
 int indexOf(byte[] b, int fromIndex)
          Get first the index of the specified byte array pattern or return -1 if the pattern is not found.
 int indexOf(int b)
          Get first the index of the specified byte or return -1 if the byte is not found.
 int indexOf(int b, int fromIndex)
          Get first the index of the specified byte or return -1 if the byte is not found.
 boolean isBigEndian()
          Get the big endian flag for byte ordering.
 int lastIndexOf(byte[] b)
          Get last the index of the specified byte array pattern or return -1 if the pattern is not found.
 int lastIndexOf(byte[] b, int fromIndex)
          Get last the index of the specified byte array pattern or return -1 if the pattern is not found.
 int lastIndexOf(int b)
          Get last the index of the specified byte or return -1 if the byte is not found.
 int lastIndexOf(int b, int fromIndex)
          Get last the index of the specified byte or return -1 if the byte is not found.
 int peek()
          Peek at the next byte to read without actually changing the read position.
 int read()
          Read the next byte from the internal buffer.
 int read(byte[] buf)
          Read buf.length bytes from the internal buffer into the specified byte array.
 int read(byte[] buf, int offset, int len)
          Read len bytes from the internal buffer into the specified byte array at offset.
 boolean readBoolean()
          Read a boolean from the internal buffer.
 byte readByte()
          Read a 8-bit signed byte value value from the internal buffer.
 char readChar()
          Read a unicode char from the internal buffer.
 double readDouble()
          Read a 64-bit double value from the internal buffer.
 float readFloat()
          Read a 32-bit float value from the internal buffer.
 int readFrom(java.io.InputStream in, int len)
          Read the specified number of bytes from the input stream into the internal buffer.
 void readFully(byte[] buf)
          Read buf.length bytes from the internal buffer into the specified byte array.
 void readFully(byte[] buf, int offset, int len)
          Read len bytes from the internal buffer into the specified byte array at offset.
 void readFullyFrom(java.io.InputStream in, int len)
          Read the specified number of bytes from the input stream into the internal buffer.
 int readInt()
          Read a 32-bit integer value from the internal buffer.
 java.lang.String readLine()
          Always throw an UnsupportedOperationException.
 long readLong()
          Read a 64-bit long value from the internal buffer.
 short readShort()
          Read a 16-bit signed byte value from the internal buffer.
 void readToEnd(java.io.InputStream in)
          Read from the specified InputStream into the internal buffer.
 int readUnsignedByte()
          Read a 8-bit unsigned byte value from the internal buffer.
 int readUnsignedShort()
          Read a 16-bit unsigned byte value from the internal buffer.
 java.lang.String readUTF()
          Read a UTF encoded string from the internal buffer.
 void reset()
          Set the length and pos back to 0.
 void seek(int pos)
          Move the current read position index.
 void setBigEndian(boolean bigEndian)
          Set the big endian flag for byte ordering.
 void setBuffer(byte[] newBuf)
           
 void setLength(int newLen)
           
 int skipBytes(int n)
          Skip n bytes in the internal buffer.
 boolean startsWith(byte[] b)
          Return true if the current byte buffer starts with the specified byte array.
 boolean startsWith(int b)
          Return true if the current byte buffer starts with the specified byte.
 byte[] toByteArray()
          Get a copy of the byte array sized to actual length.
static int utfEncodedSize(char c)
          Utility which returns the number of bytes required to encode the given character as part of a string with the modified UTF-8 format used by DataOutput.writeUTF().
static int utfEncodedSize(java.lang.String string)
          Utility which returns the number of bytes required to encode the given string with the modified UTF-8 format used by DataOutput.writeUTF().
 void write(byte[] buf)
          Write the specified buffer to the internal buffer.
 void write(byte[] buf, int offset, int len)
          Writes len bytes from the specified byte array starting at offset to the internal buffer.
 void write(int b)
          Write the specified byte to the internal buffer.
 void writeBoolean(boolean v)
          Write a boolean value to the internal buffer.
 void writeByte(int v)
          Write a 8 bit value to the internal buffer.
 void writeBytes(java.lang.String s)
          Write the specified String's bytes to the internal buffer.
 void writeChar(int v)
          Write a unicode char to the internal buffer.
 void writeChars(java.lang.String s)
          Write the specified String's characters to the internal buffer.
 void writeDouble(double v)
          Write a 64 bit double to the internal buffer.
 void writeFloat(float v)
          Write a 32 bit float to the internal buffer.
 void writeInt(int v)
          Write a 32 bit value to the internal buffer.
 void writeLong(long v)
          Write a 64 bit value to the internal buffer.
 void writeShort(int v)
          Write a 16 bit value to the internal buffer.
 void writeTo(java.io.DataOutput out)
          Write the entire internal buffer to the specified output.
 void writeTo(java.io.DataOutput out, int offset, int len)
          Write len bytes of the internal buffer starting at offset to the specified output.
 void writeTo(java.io.OutputStream out)
          Write the internal buffer in its entirity to the specified output stream.
 void writeTo(java.io.OutputStream out, int offset, int len)
          Write len bytes of the internal buffer starting at offset to the specified output stream.
 void writeUTF(java.lang.String s)
          Write the specified String's to the internal buffer using modified UTF-8 format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

length

protected int length
Actual length of valid data in buffer.


buffer

protected byte[] buffer
Byte array buffer.


pos

protected int pos
Pos is used to store the next read index


bigEndian

protected boolean bigEndian
Flag which determines whether to use big or little endian byte ordering

Constructor Detail

ByteBuffer

public ByteBuffer(byte[] buf,
                  int len)
Create a ByteBuffer of length len using the specified byte array for the internal buffer.


ByteBuffer

public ByteBuffer(byte[] buf)
Create a ByteBuffer of length buf.length using the specified byte array for the internal buffer.


ByteBuffer

public ByteBuffer(int initialCapacity)
Create a buffer with the specified inital capacity.


ByteBuffer

public ByteBuffer()
Create a buffer with an inital capacity of 64 bytes.

Method Detail

getLength

public int getLength()
Get the length of the buffer.


reset

public void reset()
Set the length and pos back to 0.


seek

public void seek(int pos)
Move the current read position index.


getPosition

public int getPosition()
Get the current read position index.


getBytes

public byte[] getBytes()
Get a direct reference to the internal buffer array.


toByteArray

public byte[] toByteArray()
Get a copy of the byte array sized to actual length.


isBigEndian

public boolean isBigEndian()
Get the big endian flag for byte ordering.


setBigEndian

public void setBigEndian(boolean bigEndian)
Set the big endian flag for byte ordering.


getInputStream

public java.io.InputStream getInputStream()
Get a new InputStream for reading the internal buffer.


getOutputStream

public java.io.OutputStream getOutputStream()
Get a new OutputStream for the writing to the internal buffer.


readFrom

public int readFrom(java.io.InputStream in,
                    int len)
             throws java.io.IOException
Read the specified number of bytes from the input stream into the internal buffer.

Returns:
the number of bytes actually read.
Throws:
java.io.IOException

readFullyFrom

public void readFullyFrom(java.io.InputStream in,
                          int len)
                   throws java.io.IOException
Read the specified number of bytes from the input stream into the internal buffer. If the len passed is less than zero, then this method routes to readToEnd().

Throws:
java.io.EOFException - if the end of the stream is reached before len bytes are read.
java.io.IOException

readToEnd

public void readToEnd(java.io.InputStream in)
               throws java.io.IOException
Read from the specified InputStream into the internal buffer. The stream is read until it returns -1 indicating the end of the input stream.

Throws:
java.io.IOException

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Write the internal buffer in its entirity to the specified output stream.

Throws:
java.io.IOException

writeTo

public void writeTo(java.io.OutputStream out,
                    int offset,
                    int len)
             throws java.io.IOException
Write len bytes of the internal buffer starting at offset to the specified output stream.

Throws:
java.io.IOException

writeTo

public void writeTo(java.io.DataOutput out)
             throws java.io.IOException
Write the entire internal buffer to the specified output.

Throws:
java.io.IOException

writeTo

public void writeTo(java.io.DataOutput out,
                    int offset,
                    int len)
             throws java.io.IOException
Write len bytes of the internal buffer starting at offset to the specified output.

Throws:
java.io.IOException

write

public void write(int b)
Write the specified byte to the internal buffer.

Specified by:
write in interface java.io.DataOutput

write

public void write(byte[] buf)
Write the specified buffer to the internal buffer.

Specified by:
write in interface java.io.DataOutput

write

public void write(byte[] buf,
                  int offset,
                  int len)
Writes len bytes from the specified byte array starting at offset to the internal buffer.

Specified by:
write in interface java.io.DataOutput

writeBoolean

public void writeBoolean(boolean v)
Write a boolean value to the internal buffer.

Specified by:
writeBoolean in interface java.io.DataOutput

writeByte

public void writeByte(int v)
Write a 8 bit value to the internal buffer.

Specified by:
writeByte in interface java.io.DataOutput

writeShort

public void writeShort(int v)
Write a 16 bit value to the internal buffer.

Specified by:
writeShort in interface java.io.DataOutput

writeChar

public void writeChar(int v)
Write a unicode char to the internal buffer.

Specified by:
writeChar in interface java.io.DataOutput

writeInt

public void writeInt(int v)
Write a 32 bit value to the internal buffer.

Specified by:
writeInt in interface java.io.DataOutput

writeLong

public void writeLong(long v)
Write a 64 bit value to the internal buffer.

Specified by:
writeLong in interface java.io.DataOutput

writeFloat

public void writeFloat(float v)
Write a 32 bit float to the internal buffer.

Specified by:
writeFloat in interface java.io.DataOutput

writeDouble

public void writeDouble(double v)
Write a 64 bit double to the internal buffer.

Specified by:
writeDouble in interface java.io.DataOutput

writeBytes

public void writeBytes(java.lang.String s)
Write the specified String's bytes to the internal buffer.

Specified by:
writeBytes in interface java.io.DataOutput

writeChars

public void writeChars(java.lang.String s)
Write the specified String's characters to the internal buffer.

Specified by:
writeChars in interface java.io.DataOutput

writeUTF

public void writeUTF(java.lang.String s)
              throws java.io.UTFDataFormatException
Write the specified String's to the internal buffer using modified UTF-8 format.

Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.UTFDataFormatException

utfEncodedSize

public static int utfEncodedSize(java.lang.String string)
Utility which returns the number of bytes required to encode the given string with the modified UTF-8 format used by DataOutput.writeUTF().


utfEncodedSize

public static int utfEncodedSize(char c)
Utility which returns the number of bytes required to encode the given character as part of a string with the modified UTF-8 format used by DataOutput.writeUTF().


available

public int available()
Return the number of bytes available based on the current read position and the buffer length.


peek

public int peek()
         throws java.io.IOException
Peek at the next byte to read without actually changing the read position.

Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Read the next byte from the internal buffer.

Throws:
java.io.EOFException - if there are no more bytes available in the buffer.
java.io.IOException

read

public int read(byte[] buf)
         throws java.io.IOException
Read buf.length bytes from the internal buffer into the specified byte array.

Throws:
java.io.IOException

read

public int read(byte[] buf,
                int offset,
                int len)
         throws java.io.IOException
Read len bytes from the internal buffer into the specified byte array at offset.

Throws:
java.io.IOException

readFully

public void readFully(byte[] buf)
               throws java.io.IOException
Read buf.length bytes from the internal buffer into the specified byte array.

Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

readFully

public void readFully(byte[] buf,
                      int offset,
                      int len)
               throws java.io.IOException
Read len bytes from the internal buffer into the specified byte array at offset.

Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Skip n bytes in the internal buffer.

Specified by:
skipBytes in interface java.io.DataInput
Throws:
java.io.IOException

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Read a boolean from the internal buffer.

Specified by:
readBoolean in interface java.io.DataInput
Throws:
java.io.IOException

readByte

public byte readByte()
              throws java.io.IOException
Read a 8-bit signed byte value value from the internal buffer.

Specified by:
readByte in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Read a 8-bit unsigned byte value from the internal buffer.

Specified by:
readUnsignedByte in interface java.io.DataInput
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Read a 16-bit signed byte value from the internal buffer.

Specified by:
readShort in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Read a 16-bit unsigned byte value from the internal buffer.

Specified by:
readUnsignedShort in interface java.io.DataInput
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Read a unicode char from the internal buffer.

Specified by:
readChar in interface java.io.DataInput
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Read a 32-bit integer value from the internal buffer.

Specified by:
readInt in interface java.io.DataInput
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Read a 64-bit long value from the internal buffer.

Specified by:
readLong in interface java.io.DataInput
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
Read a 32-bit float value from the internal buffer.

Specified by:
readFloat in interface java.io.DataInput
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
Read a 64-bit double value from the internal buffer.

Specified by:
readDouble in interface java.io.DataInput
Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Always throw an UnsupportedOperationException.

Specified by:
readLine in interface java.io.DataInput
Throws:
java.io.IOException

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Read a UTF encoded string from the internal buffer.

Specified by:
readUTF in interface java.io.DataInput
Throws:
java.io.IOException

startsWith

public boolean startsWith(int b)
Return true if the current byte buffer starts with the specified byte.


startsWith

public boolean startsWith(byte[] b)
Return true if the current byte buffer starts with the specified byte array.


endsWith

public boolean endsWith(int b)
Return true if the current byte buffer ends with the specified byte.


endsWith

public boolean endsWith(byte[] b)
Return true if the current byte buffer ends with the specified byte array.


indexOf

public int indexOf(int b)
Get first the index of the specified byte or return -1 if the byte is not found.


indexOf

public int indexOf(int b,
                   int fromIndex)
Get first the index of the specified byte or return -1 if the byte is not found.

Parameters:
b - byte to search for.
fromIndex - the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than the length of this string, it has the same effect as if it were equal to the length. If it is negative, it has the same effect as if it 0.

indexOf

public int indexOf(byte[] b)
Get first the index of the specified byte array pattern or return -1 if the pattern is not found.


indexOf

public int indexOf(byte[] b,
                   int fromIndex)
Get first the index of the specified byte array pattern or return -1 if the pattern is not found.

Parameters:
b - byte array containing pattern to search for.
fromIndex - the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than the length of this string, it has the same effect as if it were equal to the length. If it is negative, it has the same effect as if it were 0.

lastIndexOf

public int lastIndexOf(int b)
Get last the index of the specified byte or return -1 if the byte is not found.


lastIndexOf

public int lastIndexOf(int b,
                       int fromIndex)
Get last the index of the specified byte or return -1 if the byte is not found.

Parameters:
b - byte array containing pattern to search for.
fromIndex - the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than the length of this string, it has the same effect as if it were equal to the length. If it is negative, it has the same effect as if it were -1: -1 is returned.

lastIndexOf

public int lastIndexOf(byte[] b)
Get last the index of the specified byte array pattern or return -1 if the pattern is not found.


lastIndexOf

public int lastIndexOf(byte[] b,
                       int fromIndex)
Get last the index of the specified byte array pattern or return -1 if the pattern is not found.

Parameters:
b - byte array containing pattern to search for.
fromIndex - the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than the length of this string, it has the same effect as if it were equal to the length. If it is negative, it has the same effect as if it were -1: -1 is returned.

setLength

public void setLength(int newLen)

setBuffer

public void setBuffer(byte[] newBuf)

dump

public void dump()
Dump the byte buffer to standard output.


dumpToString

public java.lang.String dumpToString()
Dump the byte buffer to a String.