jist.swans.trans
Class CircularBuffer

java.lang.Object
  extended by jist.swans.trans.CircularBuffer

public class CircularBuffer
extends java.lang.Object

A class that implements circular byte array.

Since:
SWANS1.0
Version:
$Id: CircularBuffer.java,v 1.5 2004/04/06 16:07:51 barr Exp $
Author:
Kelwin Tamtoro <kt222@cs.cornell.edu>, Rimon Barr <barr+jist@cs.cornell.edu>

Field Summary
private  byte[] buffer
          Array to hold the bytes.
private  int curSize
          current size of the buffer.
private  int head
          points to first unread byte.
private  boolean isBufferEmpty
          Indicator that buffer is empty.
private  boolean isBufferFull
          Indicator that buffer is full.
private  int tail
          points to the byte after the last unread byte.
 
Constructor Summary
CircularBuffer(int size)
          Constructor.
 
Method Summary
 int getCurrentBufferSize()
          Returns the buffer size.
 int getFreeBufferSize()
          Returns the free space in buffer.
 int getTotalBytesInBuffer()
          Returns the number of bytes stored in buffer.
 boolean isBufferEmpty()
          Check if buffer is empty.
 boolean isBufferFull()
          Check if buffer is full.
 byte[] peekOneByte()
          Peek the next byte in buffer.
 boolean resizeBuffer(int newSize)
          Resize the buffer to the specified value.
 byte[] retrieveAllBytes()
          Retrieves all of the bytes stored in the buffer.
 byte[] retrieveBytes(int length)
          Retrieves the specific number of bytes.
 int storeBytes(byte[] data)
          Store the byte array into the buffer.
 int storeBytes(byte[] data, int offset, int length)
          Store the byte array from specific offset and length into the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

private byte[] buffer
Array to hold the bytes.


curSize

private int curSize
current size of the buffer.


head

private int head
points to first unread byte.


tail

private int tail
points to the byte after the last unread byte.


isBufferFull

private boolean isBufferFull
Indicator that buffer is full.


isBufferEmpty

private boolean isBufferEmpty
Indicator that buffer is empty.

Constructor Detail

CircularBuffer

public CircularBuffer(int size)
Constructor.

Parameters:
size - size of the buffer
Method Detail

storeBytes

public int storeBytes(byte[] data)
Store the byte array into the buffer.

Parameters:
data - byte array
Returns:
number of bytes actually stored

storeBytes

public int storeBytes(byte[] data,
                      int offset,
                      int length)
Store the byte array from specific offset and length into the buffer.

Parameters:
data - byte array
offset - offset in data to start reading
length - number of bytes to store
Returns:
number of bytes actually stored

retrieveBytes

public byte[] retrieveBytes(int length)
Retrieves the specific number of bytes.

Parameters:
length - number of bytes
Returns:
array of bytes retrieved from the buffer

retrieveAllBytes

public byte[] retrieveAllBytes()
Retrieves all of the bytes stored in the buffer.

Returns:
array of bytes retrieved from the buffer

peekOneByte

public byte[] peekOneByte()
Peek the next byte in buffer.

Returns:
array of one byte

getFreeBufferSize

public int getFreeBufferSize()
Returns the free space in buffer.

Returns:
free space size

getTotalBytesInBuffer

public int getTotalBytesInBuffer()
Returns the number of bytes stored in buffer.

Returns:
number of bytes in buffer

getCurrentBufferSize

public int getCurrentBufferSize()
Returns the buffer size.

Returns:
buffer size

resizeBuffer

public boolean resizeBuffer(int newSize)
Resize the buffer to the specified value. Returns false if the operation fails.

Parameters:
newSize - new size of the buffer
Returns:
operation result

isBufferFull

public boolean isBufferFull()
Check if buffer is full.

Returns:
true if buffer is full

isBufferEmpty

public boolean isBufferEmpty()
Check if buffer is empty.

Returns:
true if buffer is empty