pt.tumba.ngram.compression
Class ByteBuffer

java.lang.Object
  extended by pt.tumba.ngram.compression.ByteBuffer

public final class ByteBuffer
extends java.lang.Object

Stores a queue of bytes in a buffer with a maximum size. New bytes are added to the tail of the queue, and if the size exceeds the maximum, bytes are removed from the front of the queue. Used to model a sliding window of a fixed width over a stream of bytes presented a byte at a time. The bytes in the current window are accessed through an array of bytes, an offset and a length.

For instance, with a maximum length of 2, beginning with an empty buffer and adding bytes 1, 2, 3, and 4 in that order leads to queues {1}, {1,2}, {2,3} and {3,4}.

Author:
Bruno Martins

Field Summary
(package private)  byte[] _bytes
          Array of bytes used to buffer incoming bytes.
(package private)  int _length
          Number of bytes in the context.
private  int _maxWidth
          Maximum number of bytes in queue before adding pushes one off.
(package private)  int _offset
          Offset of first byte of current context in buffer.
private static int BUFFER_SIZE_MULTIPLIER
          Number of contexts that fit in the buffer without shifting.
 
Constructor Summary
ByteBuffer(int maxWidth)
          Construct a context buffer of given maximum size.
 
Method Summary
 void buffer(byte b)
          Add a byte to the end of the context, removing first element if necessary.
 byte[] bytes()
          Current array of bytes backing this byte buffer.
 int length()
          Current length of this buffer.
private  int maxIndex()
          The maximum index in the buffer.
private  int nextFreeIndex()
          Index in the buffer for next element.
 int offset()
          Current offset of this buffer into the byte array.
private  void tampDown()
          Moves bytes in context down to start of buffer.
 java.lang.String toString()
          Return a string representation of this context using the current localization to convert bytes to characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_bytes

final byte[] _bytes
Array of bytes used to buffer incoming bytes.


_maxWidth

private final int _maxWidth
Maximum number of bytes in queue before adding pushes one off.


_offset

int _offset
Offset of first byte of current context in buffer.


_length

int _length
Number of bytes in the context. Maximum will be given during construction.


BUFFER_SIZE_MULTIPLIER

private static final int BUFFER_SIZE_MULTIPLIER
Number of contexts that fit in the buffer without shifting.

See Also:
Constant Field Values
Constructor Detail

ByteBuffer

public ByteBuffer(int maxWidth)
Construct a context buffer of given maximum size.

Parameters:
maxWidth - Maximum number of bytes in a context.
Method Detail

bytes

public byte[] bytes()
Current array of bytes backing this byte buffer. The returned bytes are not a copy and should not be modified.

Returns:
Array of bytes backing this buffer.

offset

public int offset()
Current offset of this buffer into the byte array.

Returns:
Offset of the buffer into the byte array.

length

public int length()
Current length of this buffer.

Returns:
Length of this buffer.

buffer

public void buffer(byte b)
Add a byte to the end of the context, removing first element if necessary.

Parameters:
b - Byte to push onto the tail of the context.

toString

public java.lang.String toString()
Return a string representation of this context using the current localization to convert bytes to characters.

Overrides:
toString in class java.lang.Object
Returns:
String representation of this context.

nextFreeIndex

private int nextFreeIndex()
Index in the buffer for next element. May point beyond the maximum index if there is no more space.

Returns:
Index for next element.

maxIndex

private int maxIndex()
The maximum index in the buffer.

Parameters:
Index - of last element in the buffer.

tampDown

private void tampDown()
Moves bytes in context down to start of buffer.