Package org.agrona

Class BufferUtil

java.lang.Object
org.agrona.BufferUtil

public final class BufferUtil extends Object
Common functions for buffer implementations.
  • Field Details

    • NULL_BYTES

      public static final byte[] NULL_BYTES
      UTF-8-encoded byte representation of the "null" string.
    • NATIVE_BYTE_ORDER

      public static final ByteOrder NATIVE_BYTE_ORDER
      Native byte order.
    • ARRAY_BASE_OFFSET

      public static final long ARRAY_BASE_OFFSET
      Byte array base offset.
    • BYTE_BUFFER_HB_FIELD_OFFSET

      public static final long BYTE_BUFFER_HB_FIELD_OFFSET
      Offset of the java.nio.ByteBuffer#hb field.
    • BYTE_BUFFER_OFFSET_FIELD_OFFSET

      public static final long BYTE_BUFFER_OFFSET_FIELD_OFFSET
      Offset of the java.nio.ByteBuffer#offset field.
    • BYTE_BUFFER_ADDRESS_FIELD_OFFSET

      public static final long BYTE_BUFFER_ADDRESS_FIELD_OFFSET
      Offset of the java.nio.Buffer#address field.
  • Constructor Details

    • BufferUtil

      private BufferUtil()
  • Method Details

    • boundsCheck

      public static void boundsCheck(byte[] buffer, long index, int length)
      Bounds check the access range and throw a IndexOutOfBoundsException if exceeded.
      Parameters:
      buffer - to be checked.
      index - at which the access will begin.
      length - of the range accessed.
    • boundsCheck

      public static void boundsCheck(ByteBuffer buffer, long index, int length)
      Bounds check the access range and throw a IndexOutOfBoundsException if exceeded.
      Parameters:
      buffer - to be checked.
      index - at which the access will begin.
      length - of the range accessed.
    • address

      public static long address(ByteBuffer buffer)
      Get the address at which the underlying buffer storage begins.
      Parameters:
      buffer - that wraps the underlying storage.
      Returns:
      the memory address at which the buffer storage begins.
    • array

      public static byte[] array(ByteBuffer buffer)
      Get the array from a read-only ByteBuffer similar to ByteBuffer.array().
      Parameters:
      buffer - that wraps the underlying array.
      Returns:
      the underlying array.
    • arrayOffset

      public static int arrayOffset(ByteBuffer buffer)
      Get the array offset from a read-only ByteBuffer similar to ByteBuffer.arrayOffset().
      Parameters:
      buffer - that wraps the underlying array.
      Returns:
      the underlying array offset at which this ByteBuffer starts.
    • allocateDirectAligned

      public static ByteBuffer allocateDirectAligned(int capacity, int alignment)
      Allocate a new direct ByteBuffer that is aligned on a given alignment boundary.

      Note: Aligned buffers are a ByteBuffer.slice() so cannot be passed to free(ByteBuffer).

      Parameters:
      capacity - required for the buffer.
      alignment - boundary at which the buffer should begin.
      Returns:
      a new ByteBuffer with the required alignment.
      Throws:
      IllegalArgumentException - if the alignment is not a power of 2.
    • free

      public static void free(DirectBuffer buffer)
      Free the underlying direct ByteBuffer by invoking Cleaner on it. No op if null or if the underlying ByteBuffer non-direct.
      Parameters:
      buffer - to be freed
      See Also:
    • free

      public static void free(ByteBuffer buffer)
      Free direct ByteBuffer by invoking Cleaner on it. No op if null or non-direct ByteBuffer.
      Parameters:
      buffer - to be freed
      See Also: