uk.org.ogsadai.common
Class CircularBuffer

java.lang.Object
  |
  +--uk.org.ogsadai.common.CircularBuffer

public class CircularBuffer
extends java.lang.Object

A thread-safe, fixed-sized, circular buffer that can be written to by a producer and read from by a consumer. The producer can close the buffer to indicate that no more blocks will be written.

Author:
The OGSA-DAI Project Team

Field Summary
private static java.lang.String COPYRIGHT_NOTICE
           
private static int DEFAULT_SIZE
           
private  java.lang.Object[] mBuffer
           
private  boolean mOpen
           
private  int mReadIndex
           
private  int mWriteIndex
           
 
Constructor Summary
CircularBuffer()
          Construct a circular buffer with the default buffer size.
CircularBuffer(int size)
          Construct a circular buffer with the specified buffer size.
 
Method Summary
private  int blocksAvailable()
          Return the number of blocks that are available to be read from the buffer.
 void close()
          Close the circular buffer indicating that no more blocks will be written to it.
private  int freeSpace()
          Return the number of free spaces in the buffer available for writing to.
 java.lang.Object read()
          Read an object from the buffer.
 void write(java.lang.Object block)
          Write an object to the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
See Also:
Constant Field Values

DEFAULT_SIZE

private static final int DEFAULT_SIZE
See Also:
Constant Field Values

mBuffer

private final java.lang.Object[] mBuffer

mReadIndex

private volatile int mReadIndex

mWriteIndex

private volatile int mWriteIndex

mOpen

private boolean mOpen
Constructor Detail

CircularBuffer

public CircularBuffer()
Construct a circular buffer with the default buffer size.


CircularBuffer

public CircularBuffer(int size)
Construct a circular buffer with the specified buffer size.

Parameters:
size - The number of blocks that the buffer can contain.
Method Detail

read

public java.lang.Object read()
                      throws java.lang.InterruptedException
Read an object from the buffer. If no blocks are available to read the method blocks until a block becomes available or the buffer is closed. If the buffer is closed then a null block is returned.

Returns:
The next block Object or null if the buffer is closed.
Throws:
java.lang.InterruptedException - if the thread is interrupted.

write

public void write(java.lang.Object block)
           throws java.lang.InterruptedException
Write an object to the buffer. If the buffer is full then the method blocks until space becomes available to write the block.

Parameters:
block - The block of data.
Throws:
java.lang.InterruptedException - if the thread is interrupted.
java.lang.IllegalStateException - if the buffer has been closed.

close

public void close()
Close the circular buffer indicating that no more blocks will be written to it.


freeSpace

private int freeSpace()
Return the number of free spaces in the buffer available for writing to.

Returns:
number of free spaces.

blocksAvailable

private int blocksAvailable()
Return the number of blocks that are available to be read from the buffer. Should always be called from within a synchronized block of another method.

Returns:
The number of characters remaining to be read.