|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.org.ogsadai.common.CircularBuffer
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.
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 |
private static final java.lang.String COPYRIGHT_NOTICE
private static final int DEFAULT_SIZE
private final java.lang.Object[] mBuffer
private volatile int mReadIndex
private volatile int mWriteIndex
private boolean mOpen
Constructor Detail |
public CircularBuffer()
public CircularBuffer(int size)
size
- The number of blocks that the buffer can contain.Method Detail |
public java.lang.Object read() throws java.lang.InterruptedException
null
block is returned.
Object
or
null
if the buffer is closed.
java.lang.InterruptedException
- if the thread is interrupted.public void write(java.lang.Object block) throws java.lang.InterruptedException
block
- The block of data.
java.lang.InterruptedException
- if the thread is interrupted.
java.lang.IllegalStateException
- if the buffer has been closed.public void close()
private int freeSpace()
private int blocksAvailable()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |