ElementType
- Type of object allowed in this circular bufferpublic class CircularObjectBuffer<ElementType>
extends java.lang.Object
This class is thread safe.
CircularCharBuffer
,
CircularByteBuffer
Modifier and Type | Field and Description |
---|---|
protected boolean |
blockingWrite
True if a write to a full buffer should block until the buffer
has room, false if the write method should throw an IOException
|
protected ElementType[] |
buffer
The circular buffer.
|
protected boolean |
infinite
If this buffer is infinite (should resize itself when full)
|
static int |
INFINITE_SIZE
A buffer that will grow as things are added.
|
protected boolean |
inputDone
True when no more input is coming into this buffer.
|
protected int |
readPosition
Index of the first Object available to be read.
|
protected int |
writePosition
Index of the first Object available to be written.
|
Constructor and Description |
---|
CircularObjectBuffer()
Create a new buffer with a default capacity.
|
CircularObjectBuffer(boolean blockingWrite)
Create a new buffer with a default capacity and
given blocking behavior.
|
CircularObjectBuffer(int size)
Create a new buffer with given capacity.
|
CircularObjectBuffer(int size,
boolean blockingWrite)
Create a new buffer with the given capacity and
blocking behavior.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Make this buffer ready for reuse.
|
void |
done()
This method should be used by the producer to signal to the consumer
that the producer is done producing objects and that the consumer
should stop asking for objects once it has used up buffered objects.
|
int |
getAvailable()
Get number of Objects that are available to be read.
|
int |
getSize()
Get the capacity of this buffer.
|
int |
getSpaceLeft()
Get the number of Objects this buffer has free for
writing.
|
ElementType |
read()
Get a single Object from this buffer.
|
int |
read(ElementType[] buf)
Get Objects into an array from this buffer.
|
int |
read(ElementType[] buf,
int off,
int len)
Get Objects into a portion of an array from this buffer.
|
long |
skip(long n)
Skip Objects.
|
void |
write(ElementType o)
Add a single Object to this buffer.
|
void |
write(ElementType[] buf)
Fill this buffer with array of Objects.
|
void |
write(ElementType[] buf,
int off,
int len)
Fill this buffer with a portion of an array of Objects.
|
public static final int INFINITE_SIZE
protected ElementType[] buffer
The actual capacity of the buffer is one less than the actual length of the buffer so that an empty and a full buffer can be distinguished. An empty buffer will have the readPostion and the writePosition equal to each other. A full buffer will have the writePosition one less than the readPostion.
There are two important indexes into the buffer: The readPosition, and the writePosition. The Objects available to be read go from the readPosition to the writePosition, wrapping around the end of the buffer. The space available for writing goes from the write position to one less than the readPosition, wrapping around the end of the buffer.
protected volatile int readPosition
protected volatile int writePosition
protected volatile boolean infinite
protected boolean blockingWrite
protected boolean inputDone
public CircularObjectBuffer()
public CircularObjectBuffer(int size)
Note that the buffer may reserve some Objects for special purposes and capacity number of Objects may not be able to be written to the buffer.
Note that if the buffer is of INFINITE_SIZE it will neither block or throw exceptions, but rather grow without bound.
size
- desired capacity of the buffer in Objects or CircularObjectBuffer.INFINITE_SIZE.public CircularObjectBuffer(boolean blockingWrite)
blockingWrite
- true writing to a full buffer should block
until space is available, false if an exception should
be thrown instead.public CircularObjectBuffer(int size, boolean blockingWrite)
Note that the buffer may reserve some Objects for special purposes and capacity number of Objects may not be able to be written to the buffer.
Note that if the buffer is of INFINITE_SIZE it will neither block or throw exceptions, but rather grow without bound.
size
- desired capacity of the buffer in Objects or CircularObjectBuffer.INFINITE_SIZE.blockingWrite
- true writing to a full buffer should block
until space is available, false if an exception should
be thrown instead.public void clear()
public int getAvailable()
Note that the number of Objects available plus the number of Objects free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.
public int getSpaceLeft()
Note that the number of Objects available plus the number of Objects free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.
public int getSize()
Note that the number of Objects available plus the number of Objects free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.
public ElementType read() throws java.lang.InterruptedException
java.lang.InterruptedException
- if the thread is interrupted while waiting.public int read(ElementType[] buf) throws java.lang.InterruptedException
buf
- Destination buffer.java.lang.InterruptedException
- if the thread is interrupted while waiting.public int read(ElementType[] buf, int off, int len) throws java.lang.InterruptedException
buf
- Destination buffer.off
- Offset at which to start storing Objects.len
- Maximum number of Objects to read.java.lang.InterruptedException
- if the thread is interrupted while waiting.public long skip(long n) throws java.lang.InterruptedException, java.lang.IllegalArgumentException
n
- The number of Objects to skipjava.lang.IllegalArgumentException
- if n is negative.java.lang.InterruptedException
- if the thread is interrupted while waiting.public void done()
Once the producer has signaled that it is done, further write() invocations will cause an IllegalStateException to be thrown. Calling done() multiple times, however, has no effect.
public void write(ElementType[] buf) throws BufferOverflowException, java.lang.IllegalStateException, java.lang.InterruptedException
buf
- Array of Objects to be writtenBufferOverflowException
- if buffer does not allow blocking writes
and the buffer is full. If the exception is thrown, no data
will have been written since the buffer was set to be non-blocking.java.lang.IllegalStateException
- if done() has been called.java.lang.InterruptedException
- if the write is interrupted.public void write(ElementType[] buf, int off, int len) throws BufferOverflowException, java.lang.IllegalStateException, java.lang.InterruptedException
buf
- Array of Objectsoff
- Offset from which to start writing Objectslen
- - Number of Objects to writeBufferOverflowException
- if buffer does not allow blocking writes
and the buffer is full. If the exception is thrown, no data
will have been written since the buffer was set to be non-blocking.java.lang.IllegalStateException
- if done() has been called.java.lang.InterruptedException
- if the write is interrupted.public void write(ElementType o) throws BufferOverflowException, java.lang.IllegalStateException, java.lang.InterruptedException
o
- Object to be written.BufferOverflowException
- if buffer does not allow blocking writes
and the buffer is full. If the exception is thrown, no data
will have been written since the buffer was set to be non-blocking.java.lang.IllegalStateException
- if done() has been called.java.lang.InterruptedException
- if the write is interrupted.Copyright (c) 2001-2020 by Stephen Ostermiller