public class CircularByteBuffer
extends java.lang.Object
Using this class is a simpler alternative to using a PipedInputStream and a PipedOutputStream. PipedInputStreams and PipedOutputStreams 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.
CircularCharBuffer
,
CircularObjectBuffer
Modifier and Type | Class and Description |
---|---|
protected class |
CircularByteBuffer.CircularByteBufferInputStream
Class for reading from a circular byte buffer.
|
protected class |
CircularByteBuffer.CircularByteBufferOutputStream
Class for writing to a circular byte buffer.
|
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 byte[] |
buffer
The circular buffer.
|
protected java.io.InputStream |
in
The InputStream that can empty this 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 |
inputStreamClosed
true if the close() method has been called on the InputStream
|
protected int |
markPosition
Index of the first saved byte.
|
protected int |
markSize
Number of bytes that have to be saved
to support mark() and reset() on the InputStream.
|
protected java.io.OutputStream |
out
The OutputStream that can fill this buffer.
|
protected boolean |
outputStreamClosed
true if the close() method has been called on the OutputStream
|
protected int |
readPosition
Index of the first byte available to be read.
|
protected int |
writePosition
Index of the first byte available to be written.
|
Constructor and Description |
---|
CircularByteBuffer()
Create a new buffer with a default capacity.
|
CircularByteBuffer(boolean blockingWrite)
Create a new buffer with a default capacity and
given blocking behavior.
|
CircularByteBuffer(int size)
Create a new buffer with given capacity.
|
CircularByteBuffer(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.
|
int |
getAvailable()
Get number of bytes that are available to be read.
|
java.io.InputStream |
getInputStream()
Retrieve a InputStream that can be used to empty
this buffer.
|
java.io.OutputStream |
getOutputStream()
Retrieve a OutputStream that can be used to fill
this buffer.
|
int |
getSize()
Get the capacity of this buffer.
|
int |
getSpaceLeft()
Get the number of bytes this buffer has free for
writing.
|
public static final int INFINITE_SIZE
protected byte[] 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 InputStream has never been marked, the readPosition and the markPosition should always be the same. The bytes 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 bytes that have been saved to support a reset() of the InputStream go from markPosition to readPosition, wrapping around the end of the buffer.
protected volatile int readPosition
protected volatile int writePosition
protected volatile int markPosition
protected volatile int markSize
protected volatile boolean infinite
protected boolean blockingWrite
protected java.io.InputStream in
protected boolean inputStreamClosed
protected java.io.OutputStream out
protected boolean outputStreamClosed
public CircularByteBuffer()
public CircularByteBuffer(int size)
Note that the buffer may reserve some bytes for special purposes and capacity number of bytes 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 bytes or CircularByteBuffer.INFINITE_SIZE.public CircularByteBuffer(boolean blockingWrite)
blockingWrite
- true writing to a full buffer should block
until space is available, false if an exception should
be thrown instead.public CircularByteBuffer(int size, boolean blockingWrite)
Note that the buffer may reserve some bytes for special purposes and capacity number of bytes 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 bytes or CircularByteBuffer.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 java.io.OutputStream getOutputStream()
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.
public java.io.InputStream getInputStream()
This InputStream supports marks at the expense of the buffer size.
public int getAvailable()
Note that the number of bytes available plus the number of bytes 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 bytes available plus the number of bytes 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 bytes available plus the number of bytes free may not add up to the capacity of this buffer, as the buffer may reserve some space for other purposes.
Copyright (c) 2001-2020 by Stephen Ostermiller