Class ExpandableArrayBuffer
- All Implemented Interfaces:
Comparable<DirectBuffer>
,DirectBuffer
,MutableDirectBuffer
MutableDirectBuffer
that is backed by an array. When values are put into the buffer beyond its
current length, then it will be expanded to accommodate the resulting position for the value.
Put operations will expand the capacity as necessary up to MAX_ARRAY_LENGTH
. Get operations will throw
a IndexOutOfBoundsException
if past current capacity.
Note: this class has a natural ordering that is inconsistent with equals. Types may be different but equal on buffer contents.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Initial capacity of the buffer from which it will expand as necessary.static final int
Maximum length to which the underlying buffer can grow.Fields inherited from class org.agrona.AbstractMutableDirectBuffer
addressOffset, byteArray, capacity
Fields inherited from interface org.agrona.DirectBuffer
DISABLE_ARRAY_CONTENT_PRINTOUT_PROP_NAME, DISABLE_BOUNDS_CHECKS_PROP_NAME, SHOULD_BOUNDS_CHECK, STR_HEADER_LEN
-
Constructor Summary
ConstructorsConstructorDescriptionCreate anExpandableArrayBuffer
with an initial length ofINITIAL_CAPACITY
.ExpandableArrayBuffer
(int initialCapacity) Create anExpandableArrayBuffer
with a provided initial length. -
Method Summary
Modifier and TypeMethodDescriptionGet the underlyingByteBuffer
if one exists.private static int
calculateExpansion
(int currentLength, long requiredLength) void
checkLimit
(int limit) Check that a given limit is not greater than the capacity of a buffer from a given offset.protected final void
ensureCapacity
(int index, int length) boolean
Is this buffer expandable to accommodate putting data into it beyond the current capacity?toString()
void
wrap
(byte[] buffer) Attach a view to abyte[]
for providing direct access.void
wrap
(byte[] buffer, int offset, int length) Attach a view to abyte[]
for providing direct access.void
wrap
(long address, int length) Attach a view to an off-heap memory region by address.void
wrap
(ByteBuffer buffer) Attach a view to aByteBuffer
for providing direct access, theByteBuffer
can be heap based or direct.void
wrap
(ByteBuffer buffer, int offset, int length) Attach a view to aByteBuffer
for providing direct access.void
wrap
(DirectBuffer buffer) Attach a view to an existingDirectBuffer
void
wrap
(DirectBuffer buffer, int offset, int length) Attach a view to aDirectBuffer
for providing direct access.int
Get the adjustment in indices between an index in this buffer and the wrapped object.Methods inherited from class org.agrona.AbstractMutableDirectBuffer
addressOffset, boundsCheck, boundsCheck0, byteArray, capacity, compareTo, equals, getByte, getBytes, getBytes, getBytes, getBytes, getBytes, getChar, getChar, getDouble, getDouble, getFloat, getFloat, getInt, getInt, getLong, getLong, getShort, getShort, getStringAscii, getStringAscii, getStringAscii, getStringAscii, getStringAscii, getStringAscii, getStringUtf8, getStringUtf8, getStringUtf8, getStringWithoutLengthAscii, getStringWithoutLengthAscii, getStringWithoutLengthUtf8, hashCode, parseIntAscii, parseLongAscii, parseNaturalIntAscii, parseNaturalLongAscii, putByte, putBytes, putBytes, putBytes, putBytes, putBytes, putChar, putChar, putDouble, putDouble, putFloat, putFloat, putInt, putInt, putIntAscii, putLong, putLong, putLongAscii, putNaturalIntAscii, putNaturalIntAsciiFromEnd, putNaturalLongAscii, putNaturalPaddedIntAscii, putShort, putShort, putStringAscii, putStringAscii, putStringAscii, putStringAscii, putStringUtf8, putStringUtf8, putStringUtf8, putStringUtf8, putStringWithoutLengthAscii, putStringWithoutLengthAscii, putStringWithoutLengthAscii, putStringWithoutLengthAscii, putStringWithoutLengthUtf8, setMemory
-
Field Details
-
MAX_ARRAY_LENGTH
public static final int MAX_ARRAY_LENGTHMaximum length to which the underlying buffer can grow. Some JVMs store state in the last few bytes.- See Also:
-
INITIAL_CAPACITY
public static final int INITIAL_CAPACITYInitial capacity of the buffer from which it will expand as necessary.- See Also:
-
-
Constructor Details
-
ExpandableArrayBuffer
public ExpandableArrayBuffer()Create anExpandableArrayBuffer
with an initial length ofINITIAL_CAPACITY
. -
ExpandableArrayBuffer
public ExpandableArrayBuffer(int initialCapacity) Create anExpandableArrayBuffer
with a provided initial length.- Parameters:
initialCapacity
- of the buffer.
-
-
Method Details
-
wrap
public void wrap(byte[] buffer) Attach a view to abyte[]
for providing direct access.- Parameters:
buffer
- to which the view is attached.
-
wrap
public void wrap(byte[] buffer, int offset, int length) Attach a view to abyte[]
for providing direct access.- Parameters:
buffer
- to which the view is attached.offset
- in bytes at which the view begins.length
- in bytes of the buffer included in the view.
-
wrap
Attach a view to aByteBuffer
for providing direct access, theByteBuffer
can be heap based or direct. TheByteBuffer.order()
is not relevant for accessing the wrapped buffer.When using this method to wrap the view of the ByteBuffer the entire ByteBuffer gets wrapped between index 0 and capacity. If you want to just wrap the ByteBuffer between the position and the limit then you should use the
DirectBuffer.wrap(ByteBuffer, int, int)
method, eg:directBuffer.wrap(byteBuffer, byteBuffer.position(), byteBuffer.remaining());
- Parameters:
buffer
- to which the view is attached.
-
wrap
Attach a view to aByteBuffer
for providing direct access.The
ByteBuffer.order()
is not relevant for accessing the wrapped buffer.- Parameters:
buffer
- to which the view is attached.offset
- in bytes at which the view begins.length
- in bytes of the buffer included in the view.
-
wrap
Attach a view to an existingDirectBuffer
- Parameters:
buffer
- to which the view is attached.
-
wrap
Attach a view to aDirectBuffer
for providing direct access.- Parameters:
buffer
- to which the view is attached.offset
- in bytes at which the view begins.length
- in bytes of the buffer included in the view.
-
wrap
public void wrap(long address, int length) Attach a view to an off-heap memory region by address.- Parameters:
address
- where the memory begins off-heap.length
- of the buffer from the given address.
-
byteBuffer
Get the underlyingByteBuffer
if one exists.NB: there may not be a one-to-one mapping between indices on this buffer and the underlying byte[], see
DirectBuffer.wrapAdjustment()
.- Returns:
- the underlying
ByteBuffer
if one exists.
-
isExpandable
public boolean isExpandable()Is this buffer expandable to accommodate putting data into it beyond the current capacity?- Returns:
- true is the underlying storage can expand otherwise false.
-
wrapAdjustment
public int wrapAdjustment()Get the adjustment in indices between an index in this buffer and the wrapped object. The wrapped object might be aByteBuffer
or abyte[]
.You only need to use this adjustment if you plan to perform operations on the underlying byte array or byte buffer that rely on their indices.
- Returns:
- the adjustment in indices between an index in this buffer and the wrapped object.
- See Also:
-
checkLimit
public void checkLimit(int limit) Check that a given limit is not greater than the capacity of a buffer from a given offset.Can be overridden in a DirectBuffer subclass to enable an extensible buffer or handle retry after a flush.
- Specified by:
checkLimit
in interfaceDirectBuffer
- Overrides:
checkLimit
in classAbstractMutableDirectBuffer
- Parameters:
limit
- up to which access is required.
-
toString
-
ensureCapacity
protected final void ensureCapacity(int index, int length) - Specified by:
ensureCapacity
in classAbstractMutableDirectBuffer
-
calculateExpansion
private static int calculateExpansion(int currentLength, long requiredLength)
-