Package org.agrona

Class BufferUtil


  • public final class BufferUtil
    extends java.lang.Object
    Common functions for buffer implementations.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BufferUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long address​(java.nio.ByteBuffer buffer)
      Get the address at which the underlying buffer storage begins.
      static java.nio.ByteBuffer allocateDirectAligned​(int capacity, int alignment)
      Allocate a new direct ByteBuffer that is aligned on a given alignment boundary.
      static byte[] array​(java.nio.ByteBuffer buffer)
      Get the array from a read-only ByteBuffer similar to ByteBuffer.array().
      static int arrayOffset​(java.nio.ByteBuffer buffer)
      Get the array offset from a read-only ByteBuffer similar to ByteBuffer.arrayOffset().
      static void boundsCheck​(byte[] buffer, long index, int length)
      Bounds check the access range and throw a IndexOutOfBoundsException if exceeded.
      static void boundsCheck​(java.nio.ByteBuffer buffer, long index, int length)
      Bounds check the access range and throw a IndexOutOfBoundsException if exceeded.
      static void free​(java.nio.ByteBuffer buffer)
      Free direct ByteBuffer by invoking Cleaner on it.
      static void free​(DirectBuffer buffer)
      Free the underlying direct ByteBuffer by invoking Cleaner on it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NULL_BYTES

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

        public static final java.nio.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 Detail

      • BufferUtil

        private BufferUtil()
    • Method Detail

      • 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​(java.nio.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​(java.nio.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​(java.nio.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​(java.nio.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 java.nio.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:
        java.lang.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:
        ByteBuffer.isDirect()
      • free

        public static void free​(java.nio.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:
        ByteBuffer.isDirect()