Package org.agrona
Class BufferUtil
java.lang.Object
org.agrona.BufferUtil
Common functions for buffer implementations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
Byte array base offset.static final long
Offset of thejava.nio.Buffer#address
field.static final long
Offset of thejava.nio.ByteBuffer#hb
field.static final long
Offset of thejava.nio.ByteBuffer#offset
field.static final ByteOrder
Native byte order.static final byte[]
UTF-8-encoded byte representation of the"null"
string. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic long
address
(ByteBuffer buffer) Get the address at which the underlying buffer storage begins.static ByteBuffer
allocateDirectAligned
(int capacity, int alignment) Allocate a new directByteBuffer
that is aligned on a given alignment boundary.static byte[]
array
(ByteBuffer buffer) Get the array from a read-onlyByteBuffer
similar toByteBuffer.array()
.static int
arrayOffset
(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
(ByteBuffer buffer, long index, int length) Bounds check the access range and throw aIndexOutOfBoundsException
if exceeded.static void
free
(ByteBuffer buffer) Free directByteBuffer
by invokingCleaner
on it.static void
free
(DirectBuffer buffer) Free the underlying directByteBuffer
by invokingCleaner
on it.
-
Field Details
-
NULL_BYTES
public static final byte[] NULL_BYTESUTF-8-encoded byte representation of the"null"
string. -
NATIVE_BYTE_ORDER
Native byte order. -
ARRAY_BASE_OFFSET
public static final long ARRAY_BASE_OFFSETByte array base offset. -
BYTE_BUFFER_HB_FIELD_OFFSET
public static final long BYTE_BUFFER_HB_FIELD_OFFSETOffset of thejava.nio.ByteBuffer#hb
field. -
BYTE_BUFFER_OFFSET_FIELD_OFFSET
public static final long BYTE_BUFFER_OFFSET_FIELD_OFFSETOffset of thejava.nio.ByteBuffer#offset
field. -
BYTE_BUFFER_ADDRESS_FIELD_OFFSET
public static final long BYTE_BUFFER_ADDRESS_FIELD_OFFSETOffset of thejava.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 aIndexOutOfBoundsException
if exceeded.- Parameters:
buffer
- to be checked.index
- at which the access will begin.length
- of the range accessed.
-
boundsCheck
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
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
Get the array from a read-onlyByteBuffer
similar toByteBuffer.array()
.- Parameters:
buffer
- that wraps the underlying array.- Returns:
- the underlying array.
-
arrayOffset
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
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:
IllegalArgumentException
- if the alignment is not a power of 2.
-
free
Free the underlying directByteBuffer
by invokingCleaner
on it. No op ifnull
or if the underlyingByteBuffer
non-direct.- Parameters:
buffer
- to be freed- See Also:
-
free
- Parameters:
buffer
- to be freed- See Also:
-