edu.arizona.cs.mbel
Class ByteBuffer

java.lang.Object
  extended byedu.arizona.cs.mbel.ByteBuffer

public class ByteBuffer
extends java.lang.Object

This class is used for temporary storage and manipulation of a stream of bytes. All of the signature classes parse themselves out of ByteBuffers, and the emitter makes large use of them for storing intermediate chunks of the final module file. This class is vaguely modelled after java.nio.ByteBuffer, but it has some extra functionality that makes it specifically useful to MBEL. The concept behind a ByteBuffer is that it is an infinitely large 0-based array of bytes. There is a current byte index pointer given by getPosition(). The user can also set the current position using setPosition(int). Setting the position to a very high value will not grow the underlying array, but writing to it will (i.e. space is not allocated until needed). Both reading and writing advance the current position pointer by the specified amount. Since one main use of the ByteBuffer is as a convenient way to write an array of bytes, the ByteBuffer maintains the index of the greatest position that has been written to. Everything up to this point is returned when you call toByteArray().


Constructor Summary
ByteBuffer(byte[] init)
          Makes a ByteBuffer that is initially intended to be read from, not written to.
ByteBuffer(int capacity)
          Makes a ByteBuffer that is initially intended for writing.
 
Method Summary
 void back()
          Decrements the current position by 1 (if >0).
 void concat(ByteBuffer buf)
          Concatenates the given ByteBuffer to the end of this ByteBuffer, starting from 0 and ending at buf.getLast(), inclusive.
 byte get()
          Returns the byte stored in this ByteBuffer at the current position.
 int getCapacity()
          Returns the current maximum capacity of the underlying array.
 long getDWORD()
          Returns a 4-byte unsigned little-endian integer, starting at the current position.
 int getLast()
          Returns the index of the last position written to (initially -1).
 int getPosition()
          Returns the index of the current position in the ByteBuffer.
 int getWORD()
          Returns a 2-byte unsigned little-endian integer, starting at the current position.
 void pad(int align)
          Pads this ByteBuffer with 0s up to the next multiple of 'align'.
 byte peek()
          Returns the byte stored in this ByteBuffer at the current position.
 void put(byte data)
          Writes a single byte to this ByteBuffer, at the current position.
 void put(byte[] data)
          Writes the given byte array to this ByteBuffer, starting at the current position.
 void put(int b)
          Writes the lowest 8 bits of the given int to this ByteBuffer, at the current position.
 void putDWORD(long uint32)
          Writes an unsigned little-endian int32 to this ByteBuffer, starting at the current position.
 void putINT16(int int16)
          Writes a signed little-endian int16 to this ByteBuffer, starting at the current position.
 void putINT32(int int32)
          Writes a signed little-endian int32 to this ByteBuffer, starting at the current position.
 void putINT64(long int64)
          Writes a signed little-endian int64 to this ByteBuffer, starting at the current position.
 void putR4(float r4)
          Writes a float32 to this ByteBuffer, starting at the current position.
 void putR8(double r8)
          Writes a float64 to this ByteBuffer, starting at the current position.
 void putTOKEN(long token)
          Writes metadata token encoded in a long to this ByteBuffer, starting at the current position.
 void putWORD(int uint16)
          Writes an unsigned little-endian int16 to this ByteBuffer, starting at the current position.
 void setPosition(int pos)
          Sets the current position of this ByteBuffer.
 byte[] toByteArray()
          Returns the contents of this ByteBuffer as a single contiguous byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteBuffer

public ByteBuffer(byte[] init)
Makes a ByteBuffer that is initially intended to be read from, not written to. This constructor sets the current position to 0.

Parameters:
init - the initial bytes to put into the array, starting from index 0 (if null, and empty ByteBuffer is created)

ByteBuffer

public ByteBuffer(int capacity)
Makes a ByteBuffer that is initially intended for writing. The current position pointer is set to 0.

Parameters:
capacity - the minimum capacity of this ByteBuffer (for convenience)
Method Detail

getCapacity

public int getCapacity()
Returns the current maximum capacity of the underlying array. This value is not an absolute maximum, because ByteBuffers grow as needed.


getPosition

public int getPosition()
Returns the index of the current position in the ByteBuffer. This value will be >=0.


getLast

public int getLast()
Returns the index of the last position written to (initially -1).


get

public byte get()
Returns the byte stored in this ByteBuffer at the current position. Advances the current position by 1. (note: if the current position is past the end of any user-written data, this will return 0)


getWORD

public int getWORD()
Returns a 2-byte unsigned little-endian integer, starting at the current position. Advances the current position by 2 (see get())


getDWORD

public long getDWORD()
Returns a 4-byte unsigned little-endian integer, starting at the current position. Advances the current position by 4 (see get())


peek

public byte peek()
Returns the byte stored in this ByteBuffer at the current position. This method does not advance the current position. (note: if the current position is past the end of any user-written data, this will return 0)


setPosition

public void setPosition(int pos)
Sets the current position of this ByteBuffer.

Parameters:
pos - the new position (if <0, ignored)

back

public void back()
Decrements the current position by 1 (if >0).


toByteArray

public byte[] toByteArray()
Returns the contents of this ByteBuffer as a single contiguous byte array. This returns everything from byte 0 to byte getLast(), inclusive.


pad

public void pad(int align)
Pads this ByteBuffer with 0s up to the next multiple of 'align'.

Parameters:
align - the value to align to (if <=0, ignored)

put

public void put(int b)
Writes the lowest 8 bits of the given int to this ByteBuffer, at the current position. Advances the current position by 1.


put

public void put(byte data)
Writes a single byte to this ByteBuffer, at the current position. Advances the current position by 1.


put

public void put(byte[] data)
Writes the given byte array to this ByteBuffer, starting at the current position. Advances the current position by data.length.

Parameters:
data - the bytes to write in this buffer (if null, writes nothing)

putINT16

public void putINT16(int int16)
Writes a signed little-endian int16 to this ByteBuffer, starting at the current position. Advances the current position by 2.


putWORD

public void putWORD(int uint16)
Writes an unsigned little-endian int16 to this ByteBuffer, starting at the current position. Advances the current position by 2.


putINT32

public void putINT32(int int32)
Writes a signed little-endian int32 to this ByteBuffer, starting at the current position. Advances the current position by 4.


putDWORD

public void putDWORD(long uint32)
Writes an unsigned little-endian int32 to this ByteBuffer, starting at the current position. Advances the current position by 4.


putTOKEN

public void putTOKEN(long token)
Writes metadata token encoded in a long to this ByteBuffer, starting at the current position. Advances the current position by 4.


putINT64

public void putINT64(long int64)
Writes a signed little-endian int64 to this ByteBuffer, starting at the current position. Advances the current position by 8.


putR4

public void putR4(float r4)
Writes a float32 to this ByteBuffer, starting at the current position. Advances the current position by 4.


putR8

public void putR8(double r8)
Writes a float64 to this ByteBuffer, starting at the current position. Advances the current position by 8.


concat

public void concat(ByteBuffer buf)
Concatenates the given ByteBuffer to the end of this ByteBuffer, starting from 0 and ending at buf.getLast(), inclusive.

Parameters:
buf - the ByteBuffer to concatenate to the end of this one (if null, ignored)