Package org.metricshub.ipmi.core.common
Class ByteBuffer
java.lang.Object
org.metricshub.ipmi.core.common.ByteBuffer
ByteBuffer
is a wrapper for byte[], and allows to safely read and write from the buffer.
ByteBuffer
is thread safe and guarantees proper handling of subsequent reads and writes, if they are within availabla buffer size.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Returns max capacity of the buffer (number of total data that can be stored in the buffer).byte[]
read
(int numberOfBytes) Attempts to read given number of bytes from thisByteBuffer
.int
Returns remainig space in the buffer (number of bytes that can still be written to this buffer until it gets full).int
size()
Returns current size of the buffer (number of available data to read).int
write
(byte[] bytes) Attempts to write given byte array to thisByteBuffer
.
-
Constructor Details
-
ByteBuffer
public ByteBuffer(int capacity) Allocate newByteBuffer
with given size.- Parameters:
capacity
- max capacity of constructed buffer.
-
-
Method Details
-
write
public int write(byte[] bytes) Attempts to write given byte array to thisByteBuffer
. Writes as many bytes as it can, so that partial data from given array can be written if not available space for whole array is found in buffer.- Parameters:
bytes
- bytes to write- Returns:
- number of bytes that were actualy written
-
read
public byte[] read(int numberOfBytes) Attempts to read given number of bytes from thisByteBuffer
. If buffer currently contains less bytes than requested, this method reads only available number of bytes.- Parameters:
numberOfBytes
- requested number of bytes to read- Returns:
- actual bytes that could be read from this buffer.
-
size
public int size()Returns current size of the buffer (number of available data to read).- Returns:
- size of the buffer in bytes
-
capacity
public int capacity()Returns max capacity of the buffer (number of total data that can be stored in the buffer).- Returns:
- capacity of the buffer in bytes
-
remainingSpace
public int remainingSpace()Returns remainig space in the buffer (number of bytes that can still be written to this buffer until it gets full).- Returns:
- remaining free space in this buffer
-