Class ExpandableDirectByteBuffer
- All Implemented Interfaces:
Comparable<DirectBuffer>,DirectBuffer,MutableDirectBuffer
MutableDirectBuffer that is backed by a direct ByteBuffer. 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_BUFFER_LENGTH. Get operations will throw
a IndexOutOfBoundsException if past current capacity.
ByteOrder of a wrapped buffer is not applied to the ExpandableDirectByteBuffer;
To control ByteOrder use the appropriate method with the ByteOrder overload.
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 intInitial capacity of the buffer from which it will expand.static final intMaximum length to which the underlying buffer can grow.Fields inherited from class org.agrona.AbstractMutableDirectBuffer
addressOffset, byteArray, capacityFields 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 anExpandableDirectByteBufferwith an initial length ofINITIAL_CAPACITY.ExpandableDirectByteBuffer(int initialCapacity) Create anExpandableDirectByteBufferwith a provided initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Get the underlyingbyte[]if one exists.Get the underlyingByteBufferif one exists.voidcheckLimit(int limit) Check that a given limit is not greater than the capacity of a buffer from a given offset.protected final voidensureCapacity(int index, int length) A hook to ensure the underlying buffer has enough capacity for writing data into the buffer.booleanIs this buffer expandable to accommodate putting data into it beyond the current capacity?toString()voidwrap(byte[] buffer) Attach a view to abyte[]for providing direct access.voidwrap(byte[] buffer, int offset, int length) Attach a view to abyte[]for providing direct access.voidwrap(long address, int length) Attach a view to an off-heap memory region by address.voidwrap(ByteBuffer buffer) Attach a view to aByteBufferfor providing direct access, theByteBuffercan be heap based or direct.voidwrap(ByteBuffer buffer, int offset, int length) Attach a view to aByteBufferfor providing direct access.voidwrap(DirectBuffer buffer) Attach a view to an existingDirectBuffer.voidwrap(DirectBuffer buffer, int offset, int length) Attach a view to aDirectBufferfor providing direct access.intGet the adjustment in indices between an index in this buffer and the wrapped object.Methods inherited from class org.agrona.AbstractMutableDirectBuffer
addressOffset, boundsCheck, boundsCheck0, 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_BUFFER_LENGTH
public static final int MAX_BUFFER_LENGTHMaximum length to which the underlying buffer can grow.- See Also:
-
INITIAL_CAPACITY
public static final int INITIAL_CAPACITYInitial capacity of the buffer from which it will expand.- See Also:
-
-
Constructor Details
-
ExpandableDirectByteBuffer
public ExpandableDirectByteBuffer()Create anExpandableDirectByteBufferwith an initial length ofINITIAL_CAPACITY. -
ExpandableDirectByteBuffer
public ExpandableDirectByteBuffer(int initialCapacity) Create anExpandableDirectByteBufferwith a provided initial capacity.- Parameters:
initialCapacity- of the backing array.
-
-
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 aByteBufferfor providing direct access, theByteBuffercan 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 aByteBufferfor 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 aDirectBufferfor 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.
-
byteArray
public byte[] byteArray()Get the underlyingbyte[]if one exists.NB: there may not be a one-to-one mapping between indices on this buffer and the underlying
byte[], seeDirectBuffer.wrapAdjustment().- Specified by:
byteArrayin interfaceDirectBuffer- Overrides:
byteArrayin classAbstractMutableDirectBuffer- Returns:
- the underlying
byte[]if one exists. - See Also:
-
byteBuffer
Get the underlyingByteBufferif 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
ByteBufferif 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 aByteBufferor 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:
checkLimitin interfaceDirectBuffer- Overrides:
checkLimitin classAbstractMutableDirectBuffer- Parameters:
limit- up to which access is required.
-
toString
-
ensureCapacity
protected final void ensureCapacity(int index, int length) Description copied from class:AbstractMutableDirectBufferA hook to ensure the underlying buffer has enough capacity for writing data into the buffer.- Specified by:
ensureCapacityin classAbstractMutableDirectBuffer- Parameters:
index- at which write occurs.length- in bytes.
-