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 SummaryConstructorsConstructorDescriptionConstructs 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 SummaryModifier and TypeMethodDescriptionvoidadd(byte value) Adds a new byte to the buffer, which will eventually be returned by following invocations ofread().voidadd(byte[] targetBuffer, int offset, int length) Adds the given bytes to the buffer.voidclear()Removes all bytes from the buffer.intGets the number of bytes, that are currently present in the buffer.intgetSpace()Gets the number of bytes, that can currently be added to the buffer.booleanhasBytes()Tests whether the buffer is currently holding at least a single byte.booleanhasSpace()Tests whether there is currently room for a single byte in the buffer.booleanhasSpace(int count) Tests whether there is currently room for the given number of bytes in the buffer.booleanpeek(byte[] sourceBuffer, int offset, int length) Returns, whether the next bytes in the buffer are exactly those, given bysourceBuffer,offset, andlength.byteread()Returns the next byte from the buffer, removing it at the same time, so that following invocations won't return it again.voidread(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- 
CircularByteBufferpublic CircularByteBuffer()Constructs a new instance with a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE).
- 
CircularByteBufferConstructs a new instance with the given buffer size.- Parameters:
- size- the size of buffer to create
 
 
- 
- 
Method Details- 
addAdds 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. Use- hasSpace(), or- getSpace(), to prevent this exception.
 
- 
addAdds the given bytes to the buffer. This is the same as invokingadd(byte)for the bytes at offsetsoffset+0,offset+1, ...,offset+length-1of byte arraytargetBuffer.- Parameters:
- targetBuffer- the buffer to copy
- offset- start offset
- length- length to copy
- Throws:
- IllegalStateException- The buffer doesn't have sufficient space. Use- getSpace()to prevent this exception.
- IllegalArgumentException- Either of- offset, or- lengthis negative.
- NullPointerException- The byte array- targetBufferis null.
 
- 
clearRemoves all bytes from the buffer.
- 
getCurrentNumberOfBytesGets the number of bytes, that are currently present in the buffer.- Returns:
- the number of bytes
 
- 
getSpaceGets the number of bytes, that can currently be added to the buffer.- Returns:
- the number of bytes that can be added
 
- 
hasBytesTests whether the buffer is currently holding at least a single byte.- Returns:
- true whether the buffer is currently holding at least a single byte.
 
- 
hasSpaceTests 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:
 
- 
hasSpaceTests 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:
 
- 
peekReturns, 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 against
- offset- start offset
- length- length to compare
- Returns:
- True, if the next invocations of read()will return the bytes at offsetssourceBuffer+0,offset+1, ...,offset+length-1 of byte arraysourceBuffer.
- Throws:
- IllegalArgumentException- Either of- sourceBuffer, or- lengthis negative.
- NullPointerException- The byte array- sourceBufferis null.
 
- 
readReturns 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. Use- hasBytes(), or- getCurrentNumberOfBytes(), to prevent this exception.
 
- 
readReturns 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 array- targetBufferis null.
- IllegalArgumentException- Either of- targetOffset, or- lengthis negative, or the length of the byte array- targetBufferis too small.
- IllegalStateException- The buffer doesn't hold the given number of bytes. Use- getCurrentNumberOfBytes()to prevent this exception.
 
 
-