Package org.agrona
Class BufferUtil
- java.lang.Object
-
- org.agrona.BufferUtil
-
public final class BufferUtil extends java.lang.Object
Common functions for buffer implementations.
-
-
Field Summary
Fields Modifier and Type Field Description static long
ARRAY_BASE_OFFSET
Byte array base offset.static long
BYTE_BUFFER_ADDRESS_FIELD_OFFSET
Offset of thejava.nio.Buffer#address
field.static long
BYTE_BUFFER_HB_FIELD_OFFSET
Offset of thejava.nio.ByteBuffer#hb
field.static long
BYTE_BUFFER_OFFSET_FIELD_OFFSET
Offset of thejava.nio.ByteBuffer#offset
field.static java.nio.ByteOrder
NATIVE_BYTE_ORDER
Native byte order.static byte[]
NULL_BYTES
UTF-8-encoded byte representation of the"null"
string.
-
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 directByteBuffer
that is aligned on a given alignment boundary.static byte[]
array(java.nio.ByteBuffer buffer)
Get the array from a read-onlyByteBuffer
similar toByteBuffer.array()
.static int
arrayOffset(java.nio.ByteBuffer buffer)
Get the array offset from a read-onlyByteBuffer
similar toByteBuffer.arrayOffset()
.static void
boundsCheck(byte[] buffer, long index, int length)
Bounds check the access range and throw aIndexOutOfBoundsException
if exceeded.static void
boundsCheck(java.nio.ByteBuffer buffer, long index, int length)
Bounds check the access range and throw aIndexOutOfBoundsException
if exceeded.static void
free(java.nio.ByteBuffer buffer)
Free directByteBuffer
by invokingCleaner
on it.static void
free(DirectBuffer buffer)
Free the underlying directByteBuffer
by invokingCleaner
on it.
-
-
-
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 thejava.nio.ByteBuffer#hb
field.
-
BYTE_BUFFER_OFFSET_FIELD_OFFSET
public static final long BYTE_BUFFER_OFFSET_FIELD_OFFSET
Offset of thejava.nio.ByteBuffer#offset
field.
-
BYTE_BUFFER_ADDRESS_FIELD_OFFSET
public static final long BYTE_BUFFER_ADDRESS_FIELD_OFFSET
Offset of thejava.nio.Buffer#address
field.
-
-
Method Detail
-
boundsCheck
public static void boundsCheck(byte[] buffer, long index, int length)
Bounds check the access range and throw aIndexOutOfBoundsException
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 aIndexOutOfBoundsException
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-onlyByteBuffer
similar toByteBuffer.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-onlyByteBuffer
similar toByteBuffer.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 directByteBuffer
that is aligned on a given alignment boundary.Note: Aligned buffers are a
ByteBuffer.slice()
so cannot be passed tofree(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 directByteBuffer
by invokingCleaner
on it. No op ifnull
or if the underlyingByteBuffer
non-direct.- Parameters:
buffer
- to be freed- See Also:
ByteBuffer.isDirect()
-
free
public static void free(java.nio.ByteBuffer buffer)
Free directByteBuffer
by invokingCleaner
on it. No op ifnull
or non-directByteBuffer
.- Parameters:
buffer
- to be freed- See Also:
ByteBuffer.isDirect()
-
-