Class CircularByteBuffer
java.lang.Object
org.apache.commons.io.input.buffer.CircularByteBuffer
A buffer, which doesn't need reallocation of byte arrays, because it
reuses a single byte array. This works particularly well, if reading
from the buffer takes place at the same time than writing to. Such is the
case, for example, when using the buffer within a filtering input stream,
like the
CircularBufferInputStream
.- Since:
- 2.7
-
Constructor Summary
ConstructorDescriptionConstructs a new instance with a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE
).CircularByteBuffer
(int size) Constructs a new instance with the given buffer size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(byte value) Adds a new byte to the buffer, which will eventually be returned by following invocations ofread()
.void
add
(byte[] targetBuffer, int offset, int length) Adds the given bytes to the buffer.void
clear()
Removes all bytes from the buffer.int
Gets the number of bytes, that are currently present in the buffer.int
getSpace()
Gets the number of bytes, that can currently be added to the buffer.boolean
hasBytes()
Tests whether the buffer is currently holding at least a single byte.boolean
hasSpace()
Tests whether there is currently room for a single byte in the buffer.boolean
hasSpace
(int count) Tests whether there is currently room for the given number of bytes in the buffer.boolean
peek
(byte[] sourceBuffer, int offset, int length) Returns, whether the next bytes in the buffer are exactly those, given bysourceBuffer
,offset
, andlength
.byte
read()
Returns the next byte from the buffer, removing it at the same time, so that following invocations won't return it again.void
read
(byte[] targetBuffer, int targetOffset, int length) Returns the given number of bytes from the buffer by storing them in the given byte array at the given offset.
-
Constructor Details
-
CircularByteBuffer
public CircularByteBuffer()Constructs a new instance with a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE
). -
CircularByteBuffer
Constructs a new instance with the given buffer size.- Parameters:
size
- the size of buffer to create
-
-
Method Details
-
add
Adds a new byte to the buffer, which will eventually be returned by following invocations ofread()
.- Parameters:
value
- The byte, which is being added to the buffer.- Throws:
IllegalStateException
- The buffer is full. UsehasSpace()
, orgetSpace()
, to prevent this exception.
-
add
Adds the given bytes to the buffer. This is the same as invokingadd(byte)
for the bytes at offsetsoffset+0
,offset+1
, ...,offset+length-1
of byte arraytargetBuffer
.- Parameters:
targetBuffer
- the buffer to copyoffset
- start offsetlength
- length to copy- Throws:
IllegalStateException
- The buffer doesn't have sufficient space. UsegetSpace()
to prevent this exception.IllegalArgumentException
- Either ofoffset
, orlength
is negative.NullPointerException
- The byte arraypBuffer
is null.
-
clear
Removes all bytes from the buffer. -
getCurrentNumberOfBytes
Gets the number of bytes, that are currently present in the buffer.- Returns:
- the number of bytes
-
getSpace
Gets the number of bytes, that can currently be added to the buffer.- Returns:
- the number of bytes that can be added
-
hasBytes
Tests whether the buffer is currently holding at least a single byte.- Returns:
- true whether the buffer is currently holding at least a single byte.
-
hasSpace
Tests whether there is currently room for a single byte in the buffer. Same ashasSpace(1)
.- Returns:
- true whether there is currently room for a single byte in the buffer.
- See Also:
-
hasSpace
Tests whether there is currently room for the given number of bytes in the buffer.- Parameters:
count
- the byte count- Returns:
- true whether there is currently room for the given number of bytes in the buffer.
- See Also:
-
peek
Returns, whether the next bytes in the buffer are exactly those, given bysourceBuffer
,offset
, andlength
. No bytes are being removed from the buffer. If the result is true, then the following invocations ofread()
are guaranteed to return exactly those bytes.- Parameters:
sourceBuffer
- the buffer to compare againstoffset
- start offsetlength
- length to compare- Returns:
- True, if the next invocations of
read()
will return the bytes at offsetspOffset
+0,pOffset
+1, ...,pOffset
+length
-1 of byte arraypBuffer
. - Throws:
IllegalArgumentException
- Either ofpOffset
, orlength
is negative.NullPointerException
- The byte arraypBuffer
is null.
-
read
Returns the next byte from the buffer, removing it at the same time, so that following invocations won't return it again.- Returns:
- The byte, which is being returned.
- Throws:
IllegalStateException
- The buffer is empty. UsehasBytes()
, orgetCurrentNumberOfBytes()
, to prevent this exception.
-
read
Returns the given number of bytes from the buffer by storing them in the given byte array at the given offset.- Parameters:
targetBuffer
- The byte array, where to add bytes.targetOffset
- The offset, where to store bytes in the byte array.length
- The number of bytes to return.- Throws:
NullPointerException
- The byte arraypBuffer
is null.IllegalArgumentException
- Either ofpOffset
, orlength
is negative, or the length of the byte arraytargetBuffer
is too small.IllegalStateException
- The buffer doesn't hold the given number of bytes. UsegetCurrentNumberOfBytes()
to prevent this exception.
-