br.usp.ime.jbase
Class Buffer

java.lang.Object
  |
  +--br.usp.ime.jbase.Buffer

public final class Buffer
extends java.lang.Object

Memory buffer with methods for reading/writing Java primitive types from/to a specified position of the buffer.

A Buffer has a constant-sized data area (a byte array) and offers methods that allow an application to

Most read methods are similar to the ones of the java.io.DataInput interface, with an additional parameter that specifies the Buffer position to read. Most write methods are similar to the ones of the java.io.DataOutput interface, with an additional parameter that specifies the Buffer position to write.


Constructor Summary
Buffer(byte[] buf)
          Constructor that takes a byte array to be used as data area.
Buffer(int size)
          Constructor that takes the size of the data area.
 
Method Summary
 byte[] data()
          Gets the data area.
static int getSizeUTF(java.lang.String s)
          Returns how many bytes the string s takes when written to a Buffer through a writeUTF call.
 void read(int pos, byte[] b)
          Reads b.length bytes from position pos of this Buffer into the array b.
 void read(int pos, byte[] b, int off, int len)
          Reads len bytes from position pos of this Buffer into offset off of b.
 boolean readBoolean(int pos)
          Reads a boolean value from position pos of this Buffer.
 byte readByte(int pos)
          Reads and returns one byte from position pos of this Buffer.
 char readChar(int pos)
          Reads two bytes starting at position pos of this Buffer and returns a char value.
 double readDouble(int pos)
          Reads eight bytes starting at position pos of this Buffer and returns a double value.
 float readFloat(int pos)
          Reads four bytes starting at position pos of this Buffer and returns a float value.
 int readInt(int pos)
          Reads four bytes starting at position pos of this Buffer and returns an int value.
 long readLong(int pos)
          Reads eight bytes starting at position pos of this Buffer and returns a long value.
 java.lang.Object readObject(int pos, IntHolder size)
          Reads an object from this Buffer, starting at position pos of the Buffer.
 short readShort(int pos)
          Reads two bytes starting at position pos of this Buffer and returns a short value.
 int readUnsignedByte(int pos)
          Reads one byte from position pos of this Buffer, zero-extends it an int in the range 0 through 255, and returns the result.
 int readUnsignedShort(int pos)
          Reads two bytes starting at position pos of this Buffer and returns an int in the range 0 through 65535.
 java.lang.String readUTF(int pos)
          Reads a UTF-encoded String starting at position pos of this Buffer.
 java.lang.String readUTF(int pos, IntHolder size)
          Reads a UTF-encoded String starting at position pos of this Buffer.
 int size()
          Gets the size of the data area.
 void write(int pos, byte[] b)
          Writes to this Buffer, starting at position pos, all the bytes in array b.
 void write(int pos, byte[] b, int off, int len)
          Writes to this Buffer, starting at position pos, len bytes copied from offset off of array b.
 void writeBoolean(int pos, boolean v)
          Writes a boolean value to position pos of this Buffer.
 void writeByte(int pos, int v)
          Writes to position pos of this Buffer the eight low-order bits of the parameter v.
 void writeBytes(int pos, java.lang.String s)
          Writes to this Buffer, starting at position pos, the low-order bytes of all chars of a string.
 void writeChar(int pos, int v)
          Writes to this Buffer, starting at position pos, two bytes that comprise a char value.
 void writeChars(int pos, java.lang.String s)
          Writes to this Buffer, starting at position pos, every character in the string s, in order, two bytes per character.
 void writeDouble(int pos, double v)
          Writes to this Buffer, starting at position pos, eight bytes that comprise a double value.
 void writeFloat(int pos, float v)
          Writes to this Buffer, starting at position pos, four bytes that comprise a float value.
 void writeInt(int pos, int v)
          Writes to this Buffer, starting at position pos, four bytes that comprise an int value.
 void writeLong(int pos, long v)
          Writes to this Buffer, starting at position pos, eight bytes that comprise a long value.
 int writeObject(int pos, int maxCount, java.lang.Object obj)
          Writes an object to this Buffer, starting at position pos of the Buffer and writing at most maxCount bytes.
 void writeShort(int pos, int v)
          Writes to this Buffer, starting at position pos, two bytes that comprise ashort value.
 int writeUTF(int pos, int maxCount, java.lang.String s)
          Similar to the writeUTF(int pos, String s) method, but with an upper limit on the number of bytes written.
 void writeUTF(int pos, java.lang.String s)
          Writes to this Buffer, starting at position pos, two bytes of length information, followed by the Java modified UTF-8 representation of every character in the string s.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Buffer

public Buffer(int size)
Constructor that takes the size of the data area.
Parameters:
size - length of byte array to be created and used as data area.

Buffer

public Buffer(byte[] buf)
Constructor that takes a byte array to be used as data area.
Parameters:
buf - byte array to be used as data area.
Method Detail

data

public byte[] data()
Gets the data area.
Returns:
this Buffer's byte array.

size

public int size()
Gets the size of the data area.
Returns:
the length of this Buffer's byte array.

read

public void read(int pos,
                 byte[] b,
                 int off,
                 int len)
Reads len bytes from position pos of this Buffer into offset off of b.
Parameters:
pos - the Buffer position where reading starts
b - the array that gets the bytes read
off - the position of b that gets the first byte read
len - the number of bytes to read.

read

public void read(int pos,
                 byte[] b)
Reads b.length bytes from position pos of this Buffer into the array b.
Parameters:
pos - the Buffer position where reading starts
b - the array that gets the bytes read

readBoolean

public boolean readBoolean(int pos)
Reads a boolean value from position pos of this Buffer.
Parameters:
pos - the Buffer position to read
Returns:
true if the byte at position pos is nonzero; false otherwise.

readByte

public byte readByte(int pos)
Reads and returns one byte from position pos of this Buffer. The byte is treated as a signed value in the range -128 through 127, inclusive.
Parameters:
pos - the Buffer position to read
Returns:
the byte read.

readChar

public char readChar(int pos)
Reads two bytes starting at position pos of this Buffer and returns a char value.
Parameters:
pos - the position of the first byte to read
Returns:
the char read.

readDouble

public double readDouble(int pos)
Reads eight bytes starting at position pos of this Buffer and returns a double value.
Parameters:
pos - the position of the first byte to read
Returns:
the double value read.

readFloat

public float readFloat(int pos)
Reads four bytes starting at position pos of this Buffer and returns a float value.
Parameters:
pos - the position of the first byte to read
Returns:
thefloat value read.

readInt

public int readInt(int pos)
Reads four bytes starting at position pos of this Buffer and returns an int value.
Parameters:
pos - the position of the first byte to read
Returns:
the int value read.

readLong

public long readLong(int pos)
Reads eight bytes starting at position pos of this Buffer and returns a long value.
Parameters:
pos - the position of the first byte to read
Returns:
the long read.

readShort

public short readShort(int pos)
Reads two bytes starting at position pos of this Buffer and returns a short value.
Parameters:
pos - the position of the first byte to read
Returns:
the short value read.

readUnsignedByte

public int readUnsignedByte(int pos)
Reads one byte from position pos of this Buffer, zero-extends it an int in the range 0 through 255, and returns the result.
Parameters:
pos - the position of the first byte to read
Returns:
an int value in the range 0 through 255.

readUnsignedShort

public int readUnsignedShort(int pos)
Reads two bytes starting at position pos of this Buffer and returns an int in the range 0 through 65535.
Parameters:
pos - the position of the first byte to read
Returns:
an int value in the range 0 through 65535.

readUTF

public java.lang.String readUTF(int pos,
                                IntHolder size)
                         throws java.io.UTFDataFormatException
Reads a UTF-encoded String starting at position pos of this Buffer. The String must have been written encoded in Java modified UTF-8 format, typically through a call to a writeUTF method.

For the specification of Java modified UTF-8 format, see the writeUTF method of the interface java.io.DataOutput and the readUTF method of the interface java.io.DataInput.

Parameters:
pos - the Buffer position where reading starts
size - if this parameter is non null, its value field will be updated with the number of bytes read from the buffer
Returns:
the String read
Throws:
java.io.UTFDataFormatException - if the bytes starting at position pos do not represent a valid UTF-8 encoding of a String.
See Also:
writeUTF(int pos, String s), writeUTF(int pos, int maxCount, String s), DataInput.readUTF(), DataOutput.writeUTF(String s).

readUTF

public java.lang.String readUTF(int pos)
                         throws java.io.UTFDataFormatException
Reads a UTF-encoded String starting at position pos of this Buffer. The String must have been written encoded in Java modified UTF-8 format, typically through a call to a writeUTF method.

For the specification of Java modified UTF-8 format, see the writeUTF method of the interface java.io.DataOutput and the readUTF method of the interface java.io.DataInput.

Parameters:
pos - the Buffer position where reading starts
Returns:
the String read
Throws:
java.io.UTFDataFormatException - if the bytes starting at position pos do not represent a valid UTF-8 encoding of a String.
See Also:
writeUTF(int pos, String s), writeUTF(int pos, int maxCount, String s), DataInput.readUTF(), DataOutput.writeUTF(String s).

readObject

public java.lang.Object readObject(int pos,
                                   IntHolder size)
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException
Reads an object from this Buffer, starting at position pos of the Buffer. The object must have been written by a call to writeObject(int pos, int maxCount, Object obj).
Parameters:
pos - the Buffer position where reading starts
size - if this parameter is non null, its value field will be updated with the number of bytes read from the buffer
Returns:
the object read
Throws:
java.lang.ClassNotFoundException - class of a serialized object cannot be found
java.io.InvalidClassException - something is wrong with a class used by serialization
java.io.StreamCorruptedException - control information in the stream is inconsistent
java.io.OptionalDataException - primitive data was found in the stream instead of an object
See Also:
writeObject(int pos, int maxCount, Object obj).

write

public void write(int pos,
                  byte[] b,
                  int off,
                  int len)
Writes to this Buffer, starting at position pos, len bytes copied from offset off of array b.
Parameters:
pos - the Buffer position where writing starts
b - the array from which the bytes are copied
off - the position of b from which the first byte is copied
len - the number of bytes to write.

write

public void write(int pos,
                  byte[] b)
Writes to this Buffer, starting at position pos, all the bytes in array b.
Parameters:
pos - the Buffer position where writing starts
b - the array from which the bytes are copied.

writeBoolean

public void writeBoolean(int pos,
                         boolean v)
Writes a boolean value to position pos of this Buffer. If the parameter v is true, the value (byte)1 is written; if v is false, the value (byte)0 is written. The byte written by this method may be read by the readBoolean method, which will then return a boolean equal to v.
Parameters:
pos - the Buffer position to write
n - the boolean value read.

writeByte

public void writeByte(int pos,
                      int v)
Writes to position pos of this Buffer the eight low-order bits of the parameter v. The 24 high-order bits of v are ignored. The byte written by this method may be read by the readByte method, which will then return a byte equal to (byte)v.
Parameters:
pos - the Buffer position to write
v - the byte value to be written.

writeBytes

public void writeBytes(int pos,
                       java.lang.String s)
Writes to this Buffer, starting at position pos, the low-order bytes of all chars of a string. For every character in the string s, taken in order, one byte is written to this Buffer. If s is null, a NullPointerException is thrown.

If s.length is zero, then no bytes are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length-1]. For each character, one byte is written, the low-order byte, in exactly the manner of the writeByte method . The high-order eight bits of each character in the string are ignored.

Parameters:
pos - the Buffer position where writing starts
s - the string with the bytes to be written.

writeChar

public void writeChar(int pos,
                      int v)
Writes to this Buffer, starting at position pos, two bytes that comprise a char value. The bytes written by this method may be read by the readChar method, which will then return a char equal to (char)v.
Parameters:
pos - the Buffer position where writing starts
v - the char value to be written.

writeChars

public void writeChars(int pos,
                       java.lang.String s)
Writes to this Buffer, starting at position pos, every character in the string s, in order, two bytes per character. If s is null, a NullPointerException is thrown.

If s.length is zero, then no bytes are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length-1]. For each character, two bytes are actually written, in exactly the manner of the writeChar method.

Parameters:
pos - the Buffer position where writing starts
s - the string to be written.

writeDouble

public void writeDouble(int pos,
                        double v)
Writes to this Buffer, starting at position pos, eight bytes that comprise a double value. The bytes written by this method may be read by the readDouble method, which will then return a double equal to v.
Parameters:
pos - the Buffer position where writing starts
v - the double value to be written.

writeFloat

public void writeFloat(int pos,
                       float v)
Writes to this Buffer, starting at position pos, four bytes that comprise a float value. The bytes written by this method may be read by the readFloat method, which will then return a float equal to v.
Parameters:
pos - the Buffer position where writing starts
v - the float value to be written.

writeInt

public void writeInt(int pos,
                     int v)
Writes to this Buffer, starting at position pos, four bytes that comprise an int value. The bytes written by this method may be read by the readInt method, which will then return an int equal to v.
Parameters:
pos - the Buffer position where writing starts
v - the int value to be written.

writeLong

public void writeLong(int pos,
                      long v)
Writes to this Buffer, starting at position pos, eight bytes that comprise a long value. The bytes written by this method may be read by the readLong method, which will then return a long equal to v.
Parameters:
pos - the Buffer position where writing starts
v - the long value to be written.

writeShort

public void writeShort(int pos,
                       int v)
Writes to this Buffer, starting at position pos, two bytes that comprise ashort value. The bytes written by this method may be read by the readShort method, which will then return a short equal to (short)v.
Parameters:
pos - the Buffer position where writing starts
v - the short value to be written.

writeUTF

public void writeUTF(int pos,
                     java.lang.String s)
              throws java.io.UTFDataFormatException
Writes to this Buffer, starting at position pos, two bytes of length information, followed by the Java modified UTF-8 representation of every character in the string s. If s is null, a NullPointerException is thrown.

Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character. The bytes written by this method may be read by the readUTF method, which will then return a String equal to s.

For the specification of Java modified UTF-8 format, see the writeUTF method of the interface java.io.DataOutput and the readUTF method of the interface java.io.DataInput.

Parameters:
pos - the Buffer position where writing starts
s - the string value to be written.
Throws:
java.io.UTFDataFormatException - if the UTF lenght information does not fit in two bytes (string too long)
See Also:
writeUTF(int pos, int maxCount, String s), readUTF(int pos), readUTF(int pos, IntHolder size), DataInput.readUTF(), DataOutput.writeUTF(String s).

writeUTF

public int writeUTF(int pos,
                    int maxCount,
                    java.lang.String s)
Similar to the writeUTF(int pos, String s) method, but with an upper limit on the number of bytes written. Writes to this Buffer a sequence of at most maxCount bytes, starting at position pos: two bytes of length information, followed by the Java modified UTF-8 representation of every character in the string s. Returns the number of bytes written (including the bytes of lenght information), or zero if this number would exceed maxCount. If s is null, a NullPointerException is thrown.

Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character. The bytes written by this method may be read by the readUTF method, which will then return a String equal to s.

For the specification of Java modified UTF-8 format, see the writeUTF method of the interface java.io.DataOutput and the readUTF method of the interface java.io.DataInput.

Parameters:
pos - the Buffer position where writing starts
maxCount - an upper limit on the number of bytes written (if maxCount is greater than 65535, an upper limit of 65535 is assumed)
s - the string value to be written
Returns:
the number of bytes written
See Also:
writeUTF(int pos, String s), readUTF(int pos), readUTF(int pos, IntHolder size), DataInput.readUTF(), DataOutput.writeUTF(String s).

writeObject

public int writeObject(int pos,
                       int maxCount,
                       java.lang.Object obj)
                throws java.io.IOException
Writes an object to this Buffer, starting at position pos of the Buffer and writing at most maxCount bytes.
Parameters:
pos - the Buffer position where writing starts
maxCount - an upper limit on the number of bytes written
obj - the object to be written
Returns:
the number of bytes written
Throws:
java.io.IOException - if it would take more than maxCount bytes to write the object to the Buffer
See Also:
readObject(int pos, IntHolder size).

getSizeUTF

public static int getSizeUTF(java.lang.String s)
Returns how many bytes the string s takes when written to a Buffer through a writeUTF call.
Parameters:
s - a string
Returns:
the size of the string in Java modified UTF-8 representation, including the two bytes of lenght information
See Also:
writeUTF(int pos, String s), writeUTF(int pos, int maxCount, String s).