| |
|
| java.lang.Object com.Ostermiller.util.CircularCharBuffer
CircularCharBuffer | public class CircularCharBuffer (Code) | | Implements the Circular Buffer producer/consumer model for characters.
More information about this class is available from ostermiller.org.
Using this class is a simpler alternative to using a PipedReader
and a PipedWriter. PipedReaders and PipedWriters don't support the
mark operation, don't allow you to control buffer sizes that they use,
and have a more complicated API that requires instantiating two
classes and connecting them.
This class is thread safe.
See Also: CircularByteBuffer See Also: CircularObjectBuffer author: Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities since: ostermillerutils 1.00.00 |
Inner Class :protected class CircularCharBufferReader extends Reader | |
Inner Class :protected class CircularCharBufferWriter extends Writer | |
Field Summary | |
final public static int | INFINITE_SIZE A buffer that will grow as things are added. | protected boolean | blockingWrite | protected char[] | buffer The circular 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. | protected volatile boolean | infinite | protected volatile int | markPosition Index of the first saved character. | protected volatile int | markSize Number of characters that have to be saved
to support mark() and reset() on the Reader. | protected volatile int | readPosition Index of the first character available to be read. | protected Reader | reader The Reader that can empty this buffer. | protected boolean | readerClosed | protected volatile int | writePosition Index of the first character available to be written. | protected Writer | writer The Writer that can fill this buffer. | protected boolean | writerClosed |
Constructor Summary | |
public | CircularCharBuffer() Create a new buffer with a default capacity. | public | CircularCharBuffer(int size) Create a new buffer with given capacity. | public | CircularCharBuffer(boolean blockingWrite) Create a new buffer with a default capacity and
given blocking behavior. | public | CircularCharBuffer(int size, boolean blockingWrite) Create a new buffer with the given capacity and
blocking behavior. |
Method Summary | |
public void | clear() Make this buffer ready for reuse. | public int | getAvailable() Get number of characters that are available to be read. | public Reader | getReader() Retrieve a Reader that can be used to empty
this buffer. | public int | getSize() Get the capacity of this buffer. | public int | getSpaceLeft() Get the number of characters this buffer has free for
writing. | public Writer | getWriter() Retrieve a Writer that can be used to fill
this buffer.
Write methods may throw a BufferOverflowException if
the buffer is not large enough. |
INFINITE_SIZE | final public static int INFINITE_SIZE(Code) | | A buffer that will grow as things are added.
since: ostermillerutils 1.00.00 |
blockingWrite | protected boolean blockingWrite(Code) | | True if a write to a full buffer should block until the buffer
has room, false if the write method should throw an IOException
since: ostermillerutils 1.00.00 |
buffer | protected char[] buffer(Code) | | The circular 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 markPostion and the
writePosition equal to each other. A full buffer will have
the writePosition one less than the markPostion.
There are three important indexes into the buffer:
The readPosition, the writePosition, and the markPosition.
If the Reader has never been marked, the readPosition and
the markPosition should always be the same. The characters
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 markPosition,
wrapping around the end of the buffer. The characters that have
been saved to support a reset() of the Reader go from markPosition
to readPosition, wrapping around the end of the buffer.
since: ostermillerutils 1.00.00 |
infinite | protected volatile boolean infinite(Code) | | If this buffer is infinite (should resize itself when full)
since: ostermillerutils 1.00.00 |
markPosition | protected volatile int markPosition(Code) | | Index of the first saved character. (To support stream marking.)
since: ostermillerutils 1.00.00 |
markSize | protected volatile int markSize(Code) | | Number of characters that have to be saved
to support mark() and reset() on the Reader.
since: ostermillerutils 1.00.00 |
readPosition | protected volatile int readPosition(Code) | | Index of the first character available to be read.
since: ostermillerutils 1.00.00 |
reader | protected Reader reader(Code) | | The Reader that can empty this buffer.
since: ostermillerutils 1.00.00 |
readerClosed | protected boolean readerClosed(Code) | | true if the close() method has been called on the Reader
since: ostermillerutils 1.00.00 |
writePosition | protected volatile int writePosition(Code) | | Index of the first character available to be written.
since: ostermillerutils 1.00.00 |
writer | protected Writer writer(Code) | | The Writer that can fill this buffer.
since: ostermillerutils 1.00.00 |
writerClosed | protected boolean writerClosed(Code) | | true if the close() method has been called on the writer
since: ostermillerutils 1.00.00 |
CircularCharBuffer | public CircularCharBuffer()(Code) | | Create a new buffer with a default capacity.
Writing to a full buffer will block until space
is available rather than throw an exception.
since: ostermillerutils 1.00.00 |
CircularCharBuffer | public CircularCharBuffer(int size)(Code) | | Create a new buffer with given capacity.
Writing to a full buffer will block until space
is available rather than throw an exception.
Note that the buffer may reserve some characters for
special purposes and capacity number of characters 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.
Parameters: size - desired capacity of the buffer in characters or CircularCharBuffer.INFINITE_SIZE since: ostermillerutils 1.00.00 |
CircularCharBuffer | public CircularCharBuffer(boolean blockingWrite)(Code) | | Create a new buffer with a default capacity and
given blocking behavior.
Parameters: blockingWrite - true writing to a full buffer should blockuntil space is available, false if an exception shouldbe thrown instead. since: ostermillerutils 1.00.00 |
CircularCharBuffer | public CircularCharBuffer(int size, boolean blockingWrite)(Code) | | Create a new buffer with the given capacity and
blocking behavior.
Note that the buffer may reserve some characters for
special purposes and capacity number of characters may
not be able to be written to the buffer.
Note that if the buffer is of CircularCharBuffer.INFINITE_SIZE it will
neither block or throw exceptions, but rather grow
without bound.
Parameters: size - desired capacity of the buffer in characters or CircularCharBuffer.INFINITE_SIZE Parameters: blockingWrite - true writing to a full buffer should blockuntil space is available, false if an exception shouldbe thrown instead. since: ostermillerutils 1.00.00 |
clear | public void clear()(Code) | | Make this buffer ready for reuse. The contents of the buffer
will be cleared and the streams associated with this buffer
will be reopened if they had been closed.
since: ostermillerutils 1.00.00 |
getAvailable | public int getAvailable()(Code) | | Get number of characters that are available to be read.
Note that the number of characters available plus
the number of characters free may not add up to the
capacity of this buffer, as the buffer may reserve some
space for other purposes.
the size in characters of this buffer since: ostermillerutils 1.00.00 |
getReader | public Reader getReader()(Code) | | Retrieve a Reader that can be used to empty
this buffer.
This Reader supports marks at the expense
of the buffer size.
the consumer for this buffer. since: ostermillerutils 1.00.00 |
getSize | public int getSize()(Code) | | Get the capacity of this buffer.
Note that the number of characters available plus
the number of characters free may not add up to the
capacity of this buffer, as the buffer may reserve some
space for other purposes.
the size in characters of this buffer since: ostermillerutils 1.00.00 |
getSpaceLeft | public int getSpaceLeft()(Code) | | Get the number of characters this buffer has free for
writing.
Note that the number of characters available plus
the number of characters free may not add up to the
capacity of this buffer, as the buffer may reserve some
space for other purposes.
the available space in characters of this buffer since: ostermillerutils 1.00.00 |
getWriter | public Writer getWriter()(Code) | | Retrieve a Writer that can be used to fill
this buffer.
Write methods may throw a BufferOverflowException if
the buffer is not large enough. A large enough buffer
size must be chosen so that this does not happen or
the caller must be prepared to catch the exception and
try again once part of the buffer has been consumed.
the producer for this buffer. since: ostermillerutils 1.00.00 |
|
|
|