Class CircularByteBuffer
- java.lang.Object
-
- org.apache.commons.io.input.buffer.CircularByteBuffer
-
public class CircularByteBuffer extends java.lang.Object
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 theCircularBufferInputStream
.
-
-
Constructor Summary
Constructors Constructor Description CircularByteBuffer()
Creates a new instance with a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE
).CircularByteBuffer(int size)
Creates a new instance with the given buffer size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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
getCurrentNumberOfBytes()
Returns the number of bytes, that are currently present in the buffer.int
getSpace()
Returns the number of bytes, that can currently be added to the buffer.boolean
hasBytes()
Returns, whether the buffer is currently holding, at least, a single byte.boolean
hasSpace()
Returns, whether there is currently room for a single byte in the buffer.boolean
hasSpace(int count)
Returns, 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 Detail
-
CircularByteBuffer
public CircularByteBuffer(int size)
Creates a new instance with the given buffer size.- Parameters:
size
- the size of buffer to create
-
CircularByteBuffer
public CircularByteBuffer()
Creates a new instance with a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE
).
-
-
Method Detail
-
read
public byte 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:
java.lang.IllegalStateException
- The buffer is empty. UsehasBytes()
, orgetCurrentNumberOfBytes()
, to prevent this exception.
-
read
public 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.- 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:
java.lang.NullPointerException
- The byte arraypBuffer
is null.java.lang.IllegalArgumentException
- Either ofpOffset
, orlength
is negative, or the length of the byte arraytargetBuffer
is too small.java.lang.IllegalStateException
- The buffer doesn't hold the given number of bytes. UsegetCurrentNumberOfBytes()
to prevent this exception.
-
add
public void add(byte value)
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:
java.lang.IllegalStateException
- The buffer is full. UsehasSpace()
, orgetSpace()
, to prevent this exception.
-
peek
public boolean peek(byte[] sourceBuffer, int offset, int length)
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:
java.lang.IllegalArgumentException
- Either ofpOffset
, orlength
is negative.java.lang.NullPointerException
- The byte arraypBuffer
is null.
-
add
public void add(byte[] targetBuffer, int offset, int length)
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:
java.lang.IllegalStateException
- The buffer doesn't have sufficient space. UsegetSpace()
to prevent this exception.java.lang.IllegalArgumentException
- Either ofoffset
, orlength
is negative.java.lang.NullPointerException
- The byte arraypBuffer
is null.
-
hasSpace
public boolean hasSpace()
Returns, whether there is currently room for a single byte in the buffer. Same ashasSpace(1)
.- Returns:
- true if there is space for a byte
- See Also:
hasSpace(int)
,getSpace()
-
hasSpace
public boolean hasSpace(int count)
Returns, whether there is currently room for the given number of bytes in the buffer.- Parameters:
count
- the byte count- Returns:
- true if there is space for the given number of bytes
- See Also:
hasSpace()
,getSpace()
-
hasBytes
public boolean hasBytes()
Returns, whether the buffer is currently holding, at least, a single byte.- Returns:
- true if the buffer is not empty
-
getSpace
public int getSpace()
Returns the number of bytes, that can currently be added to the buffer.- Returns:
- the number of bytes that can be added
-
getCurrentNumberOfBytes
public int getCurrentNumberOfBytes()
Returns the number of bytes, that are currently present in the buffer.- Returns:
- the number of bytes
-
clear
public void clear()
Removes all bytes from the buffer.
-
-