edu.rice.cs.plt.io
Class ExpandingBuffer<T>

java.lang.Object
  extended by edu.rice.cs.plt.io.ExpandingBuffer<T>
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
ExpandingByteBuffer, ExpandingCharBuffer

public abstract class ExpandingBuffer<T>
extends java.lang.Object
implements java.io.Serializable

Abstraction of ExpandingByteBuffer and ExpandingCharBuffer to manage indices and bookeeping for these buffers from a single control point. In general, this class represents an expandable and thread safe buffer of elements of some type. T is the type of a sequence of these elements of fixed length BUFFER_SIZE. Subclasses are responsible for managing reading and writing, but need not interact directly with the expanding queue of Ts, nor with the indices used to manage this queue. Instead, the methods in this class provide the necessary tools. Synchronization should occur on the ExpandingBuffer object to prevent conflicts between threads before invoking any of this class's helper methods.

See Also:
Serialized Form

Field Summary
protected static int BUFFER_SIZE
           
 
Constructor Summary
ExpandingBuffer()
           
 
Method Summary
protected  int allocate()
          Allocate space in the buffer if none is available.
protected abstract  T allocateBuffer(int size)
          Create a fixed-size sub-buffer
protected  boolean deallocate()
          Deallocate the first buffer if it is no longer needed.
protected  int elementsInFirstBuffer()
          Determine the number of buffered elements located in _buffers.getFirst()
protected  T firstBuffer()
          Access the first buffer (assuming it exists)
protected  int firstIndex()
          Calculate the first valid index in firstBuffer() (assuming it exists)
 boolean isEmpty()
           
protected  T lastBuffer()
          Access the last buffer (assuming it exists)
protected  int lastIndex()
          Calculate the index after the last valid element in lastBuffer() (assuming it exists)
protected  void recordRead(long read)
          Adjust the indices after reading the given number of elements.
protected  void recordWrite(long written)
          Adjust the indices after writing the given number of elements.
 long size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_SIZE

protected static final int BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

ExpandingBuffer

public ExpandingBuffer()
Method Detail

allocateBuffer

protected abstract T allocateBuffer(int size)
Create a fixed-size sub-buffer


size

public long size()
Returns:
the size of the buffer

isEmpty

public boolean isEmpty()

allocate

protected int allocate()
Allocate space in the buffer if none is available. Ensures that there is room for at least one new element (and that _buffers is nonempty). Should be called before a write is attempted.

Returns:
The amount of space now available at the end of the buffer (> 0)

elementsInFirstBuffer

protected int elementsInFirstBuffer()
Determine the number of buffered elements located in _buffers.getFirst()


deallocate

protected boolean deallocate()
Deallocate the first buffer if it is no longer needed. Return true if deallocation took place. Should be called after a read occurs.


firstBuffer

protected T firstBuffer()
Access the first buffer (assuming it exists)


firstIndex

protected int firstIndex()
Calculate the first valid index in firstBuffer() (assuming it exists)


lastBuffer

protected T lastBuffer()
Access the last buffer (assuming it exists)


lastIndex

protected int lastIndex()
Calculate the index after the last valid element in lastBuffer() (assuming it exists)


recordWrite

protected void recordWrite(long written)
Adjust the indices after writing the given number of elements.


recordRead

protected void recordRead(long read)
Adjust the indices after reading the given number of elements.