Package org.agrona
Class BufferUtil
java.lang.Object
org.agrona.BufferUtil
Common functions for buffer implementations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longByte array base offset.static final longOffset of thejava.nio.Buffer#addressfield.static final longOffset of thejava.nio.ByteBuffer#hbfield.static final longOffset of thejava.nio.ByteBuffer#offsetfield.static final ByteOrderNative byte order.static final byte[]UTF-8-encoded byte representation of the"null"string. -
Method Summary
Modifier and TypeMethodDescriptionstatic longaddress(ByteBuffer buffer) Get the address at which the underlying buffer storage begins.static ByteBufferallocateDirectAligned(int capacity, int alignment) Allocate a new directByteBufferthat is aligned on a given alignment boundary.static byte[]array(ByteBuffer buffer) Get the array from a read-onlyByteBuffersimilar toByteBuffer.array().static intarrayOffset(ByteBuffer buffer) Get the array offset from a read-onlyByteBuffersimilar toByteBuffer.arrayOffset().static voidboundsCheck(byte[] buffer, long index, int length) Bounds check the access range and throw aIndexOutOfBoundsExceptionif exceeded.static voidboundsCheck(ByteBuffer buffer, long index, int length) Bounds check the access range and throw aIndexOutOfBoundsExceptionif exceeded.static voidfree(ByteBuffer buffer) Free directByteBufferby invokingCleaneron it.static voidfree(DirectBuffer buffer) Free the underlying directByteBufferby invokingCleaneron 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#hbfield. -
BYTE_BUFFER_OFFSET_FIELD_OFFSET
public static final long BYTE_BUFFER_OFFSET_FIELD_OFFSETOffset of thejava.nio.ByteBuffer#offsetfield. -
BYTE_BUFFER_ADDRESS_FIELD_OFFSET
public static final long BYTE_BUFFER_ADDRESS_FIELD_OFFSETOffset of thejava.nio.Buffer#addressfield.
-
-
Method Details
-
boundsCheck
public static void boundsCheck(byte[] buffer, long index, int length) Bounds check the access range and throw aIndexOutOfBoundsExceptionif 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 aIndexOutOfBoundsExceptionif 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-onlyByteBuffersimilar toByteBuffer.array().- Parameters:
buffer- that wraps the underlying array.- Returns:
- the underlying array.
-
arrayOffset
Get the array offset from a read-onlyByteBuffersimilar toByteBuffer.arrayOffset().- Parameters:
buffer- that wraps the underlying array.- Returns:
- the underlying array offset at which this ByteBuffer starts.
-
allocateDirectAligned
Allocate a new directByteBufferthat 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
ByteBufferwith the required alignment. - Throws:
IllegalArgumentException- if the alignment is not a power of 2.
-
free
Free the underlying directByteBufferby invokingCleaneron it. No op ifnullor if the underlyingByteBuffernon-direct.- Parameters:
buffer- to be freed- See Also:
-
free
- Parameters:
buffer- to be freed- See Also:
-