Class ByteBuffer

java.lang.Object
org.metricshub.ipmi.core.common.ByteBuffer

public class ByteBuffer extends Object
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
    Constructor
    Description
    ByteBuffer(int capacity)
    Allocate new ByteBuffer with given size.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    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 this ByteBuffer.
    int
    Returns remainig space in the buffer (number of bytes that can still be written to this buffer until it gets full).
    int
    Returns current size of the buffer (number of available data to read).
    int
    write(byte[] bytes)
    Attempts to write given byte array to this ByteBuffer.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteBuffer

      public ByteBuffer(int capacity)
      Allocate new ByteBuffer 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 this ByteBuffer. 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 this ByteBuffer. 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