uk.org.ogsadai.common
Class CircularBuffer

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

public class CircularBuffer
extends java.lang.Object

This is 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

Constructor Summary
CircularBuffer()
          Constructs a circular buffer with the default buffer size.
CircularBuffer(int size)
          Constructs a circular buffer with the specified buffer size.
 
Method Summary
 void close()
          Closes the circular buffer indicating that no more blocks will be written to it.
 java.lang.Object read()
          Reads an object from the buffer.
 void write(java.lang.Object block)
          Writes an object to the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CircularBuffer

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


CircularBuffer

public CircularBuffer(int size)
Constructs 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
Reads 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.
java.lang.InterruptedException

write

public void write(java.lang.Object block)
           throws java.lang.InterruptedException
Writes 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.
java.lang.InterruptedException

close

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