public class UnsafeBuffer extends AbstractMutableDirectBuffer implements AtomicBuffer
ByteBuffer implementations, or an off Java heap memory address.
ByteOrder of a wrapped buffer is not applied to the UnsafeBuffer. UnsafeBuffers are
effectively stateless and can be used concurrently, the wrapping methods are an exception. 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.
Note: The wrap methods on this class are not thread safe. Concurrent access should only happen after a successful wrap.
| Modifier and Type | Field and Description |
|---|---|
static int |
ALIGNMENT |
static String |
DISABLE_BOUNDS_CHECKS_PROP_NAME |
static boolean |
SHOULD_BOUNDS_CHECK |
addressOffset, byteArray, capacitySTRICT_ALIGNMENT_CHECKS, STRICT_ALIGNMENT_CHECKS_PROP_NAMEDISABLE_ARRAY_CONTENT_PRINTOUT_PROP_NAME, STR_HEADER_LEN| Constructor and Description |
|---|
UnsafeBuffer()
Empty constructor for a reusable wrapper buffer.
|
UnsafeBuffer(byte[] buffer)
Attach a view to a
byte[] for providing direct access. |
UnsafeBuffer(byte[] buffer,
int offset,
int length)
Attach a view to a
byte[] for providing direct access. |
UnsafeBuffer(ByteBuffer buffer)
Attach a view to a
ByteBuffer for providing direct access, the ByteBuffer can be
heap based or direct. |
UnsafeBuffer(ByteBuffer buffer,
int offset,
int length)
Attach a view to a
ByteBuffer for providing direct access, the ByteBuffer can be
heap based or direct. |
UnsafeBuffer(DirectBuffer buffer)
Attach a view to an existing
DirectBuffer |
UnsafeBuffer(DirectBuffer buffer,
int offset,
int length)
Attach a view to an existing
DirectBuffer |
UnsafeBuffer(long address,
int length)
Attach a view to an off-heap memory region by address.
|
| Modifier and Type | Method and Description |
|---|---|
int |
addIntOrdered(int index,
int increment)
Add a value to a given index with ordered store semantics.
|
long |
addLongOrdered(int index,
long increment)
Add a value to a given index with ordered store semantics.
|
ByteBuffer |
byteBuffer()
Get the underlying
ByteBuffer if one exists. |
boolean |
compareAndSetInt(int index,
int expectedValue,
int updateValue)
Atomic compare and set of an int given an expected value.
|
boolean |
compareAndSetLong(int index,
long expectedValue,
long updateValue)
Atomic compare and set of a long given an expected value.
|
protected void |
ensureCapacity(int index,
int length) |
int |
getAndAddInt(int index,
int delta)
Atomically add a delta to a value at a location returning the previous contents.
|
long |
getAndAddLong(int index,
long delta)
Atomically add a delta to a value at a location returning the previous contents.
|
int |
getAndSetInt(int index,
int value)
Atomically exchange a value at a location returning the previous contents.
|
long |
getAndSetLong(int index,
long value)
Atomically exchange a value at a location returning the previous contents.
|
byte |
getByteVolatile(int index)
Get the value at a given index with volatile semantics.
|
char |
getCharVolatile(int index)
Get the value at a given index with volatile semantics.
|
int |
getIntVolatile(int index)
Get the value at a given index with volatile semantics.
|
long |
getLongVolatile(int index)
Get the value at a given index with volatile semantics.
|
short |
getShortVolatile(int index)
Get the value at a given index with volatile semantics.
|
boolean |
isExpandable()
Is this buffer expandable to accommodate putting data into it beyond the current capacity?
|
void |
putByteVolatile(int index,
byte value)
Put a value to a given index with volatile semantics.
|
void |
putCharVolatile(int index,
char value)
Put a value to a given index with volatile semantics.
|
void |
putIntOrdered(int index,
int value)
Put a value to a given index with ordered semantics.
|
void |
putIntVolatile(int index,
int value)
Put a value to a given index with volatile semantics.
|
void |
putLongOrdered(int index,
long value)
Put a value to a given index with ordered store semantics.
|
void |
putLongVolatile(int index,
long value)
Put a value to a given index with volatile semantics.
|
void |
putShortVolatile(int index,
short value)
Put a value to a given index with volatile semantics.
|
String |
toString() |
void |
verifyAlignment()
Verify that the underlying buffer is correctly aligned to prevent word tearing, other ordering issues and
the JVM crashes.
|
void |
wrap(byte[] buffer)
Attach a view to a
byte[] for providing direct access. |
void |
wrap(byte[] buffer,
int offset,
int length)
Attach a view to a
byte[] for providing direct access. |
void |
wrap(ByteBuffer buffer)
Attach a view to a
ByteBuffer for providing direct access, the ByteBuffer can be
heap based or direct. |
void |
wrap(ByteBuffer buffer,
int offset,
int length)
Attach a view to a
ByteBuffer for providing direct access. |
void |
wrap(DirectBuffer buffer)
Attach a view to an existing
DirectBuffer |
void |
wrap(DirectBuffer buffer,
int offset,
int length)
Attach a view to a
DirectBuffer for providing direct access. |
void |
wrap(long address,
int length)
Attach a view to an off-heap memory region by address.
|
int |
wrapAdjustment()
Get the adjustment in indices between an index in this buffer and the wrapped object.
|
addressOffset, boundsCheck, boundsCheck0, byteArray, capacity, checkLimit, 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, setMemoryclone, finalize, getClass, notify, notifyAll, wait, wait, waitputByte, 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, setMemoryaddressOffset, boundsCheck, byteArray, capacity, checkLimit, 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, parseIntAscii, parseLongAscii, parseNaturalIntAscii, parseNaturalLongAsciicompareTopublic static final int ALIGNMENT
AtomicBuffer.ALIGNMENT,
Constant Field Valuespublic static final String DISABLE_BOUNDS_CHECKS_PROP_NAME
public static final boolean SHOULD_BOUNDS_CHECK
DirectBuffer.SHOULD_BOUNDS_CHECKpublic UnsafeBuffer()
public UnsafeBuffer(byte[] buffer)
byte[] for providing direct access.buffer - to which the view is attached.wrap(byte[])public UnsafeBuffer(byte[] buffer,
int offset,
int length)
byte[] for providing direct access.buffer - to which the view is attached.offset - in bytes within the buffer to begin.length - in bytes of the buffer included in the view.wrap(byte[], int, int)public UnsafeBuffer(ByteBuffer buffer)
ByteBuffer for providing direct access, the ByteBuffer can be
heap based or direct.buffer - to which the view is attached.public UnsafeBuffer(ByteBuffer buffer, int offset, int length)
ByteBuffer for providing direct access, the ByteBuffer can be
heap based or direct.buffer - to which the view is attached.offset - in bytes within the buffer to begin.length - in bytes of the buffer included in the view.public UnsafeBuffer(DirectBuffer buffer)
DirectBufferbuffer - to which the view is attached.public UnsafeBuffer(DirectBuffer buffer, int offset, int length)
DirectBufferbuffer - to which the view is attached.offset - in bytes within the buffer to begin.length - in bytes of the buffer included in the view.public UnsafeBuffer(long address,
int length)
address - where the memory begins off-heaplength - of the buffer from the given addresspublic void wrap(byte[] buffer)
byte[] for providing direct access.wrap in interface DirectBufferbuffer - to which the view is attached.public void wrap(byte[] buffer,
int offset,
int length)
byte[] for providing direct access.wrap in interface DirectBufferbuffer - to which the view is attached.offset - in bytes at which the view begins.length - in bytes of the buffer included in the view.public void wrap(ByteBuffer buffer)
ByteBuffer for providing direct access, the ByteBuffer can be
heap based or direct. The ByteBuffer.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());
wrap in interface DirectBufferbuffer - to which the view is attached.public void wrap(ByteBuffer buffer, int offset, int length)
ByteBuffer for providing direct access.
The ByteBuffer.order() is not relevant for accessing the wrapped buffer.
wrap in interface DirectBufferbuffer - to which the view is attached.offset - in bytes at which the view begins.length - in bytes of the buffer included in the view.public void wrap(DirectBuffer buffer)
DirectBufferwrap in interface DirectBufferbuffer - to which the view is attached.public void wrap(DirectBuffer buffer, int offset, int length)
DirectBuffer for providing direct access.wrap in interface DirectBufferbuffer - to which the view is attached.offset - in bytes at which the view begins.length - in bytes of the buffer included in the view.public void wrap(long address,
int length)
wrap in interface DirectBufferaddress - where the memory begins off-heap.length - of the buffer from the given address.public ByteBuffer byteBuffer()
ByteBuffer 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().
byteBuffer in interface DirectBufferByteBuffer if one exists.public int wrapAdjustment()
ByteBuffer or a byte[].
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.
wrapAdjustment in interface DirectBufferDirectBuffer.byteArray(),
DirectBuffer.byteBuffer()public boolean isExpandable()
isExpandable in interface MutableDirectBufferpublic void verifyAlignment()
AtomicBuffer.putIntOrdered(int, int),
AtomicBuffer.putIntVolatile(int, int), AtomicBuffer.addIntOrdered(int, int), AtomicBuffer.getIntVolatile(int),
AtomicBuffer.getAndAddInt(int, int) or AtomicBuffer.getAndSetInt(int, int), must have the index aligned by four bytes
(e.g. 0, 4, 8, 12, 60 etc.).
Users are encouraged to call this method after constructing the AtomicBuffer instance in order to ensure
that the underlying buffer supports atomic access to long values.
Agrona provides an agent (org.agrona.agent.BufferAlignmentAgent) that checks the alignment of indexes
for all operations at runtime. The agent throws an exception if the unaligned access is detected.
Note: on some platforms unaligned atomic access can lead to the JVM crashes, e.g.:
# Java VM: OpenJDK 64-Bit Server VM (25.352-b08 mixed mode bsd-aarch64 compressed oops)
#
# siginfo: si_signo: 10 (SIGBUS), si_code: 1 (BUS_ADRALN)
verifyAlignment in interface AtomicBufferAtomicBuffer.ALIGNMENTpublic long getLongVolatile(int index)
getLongVolatile in interface AtomicBufferindex - in bytes from which to get.public void putLongVolatile(int index,
long value)
putLongVolatile in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public void putLongOrdered(int index,
long value)
putLongOrdered in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public long addLongOrdered(int index,
long increment)
addLongOrdered in interface AtomicBufferindex - in bytes for where to put.increment - by which the value at the index will be adjusted.public boolean compareAndSetLong(int index,
long expectedValue,
long updateValue)
compareAndSetLong in interface AtomicBufferindex - in bytes for where to put.expectedValue - at to be compared.updateValue - to be exchanged.public long getAndSetLong(int index,
long value)
getAndSetLong in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public long getAndAddLong(int index,
long delta)
getAndAddLong in interface AtomicBufferindex - in bytes for where to put.delta - to be added to the value at the index.public int getIntVolatile(int index)
getIntVolatile in interface AtomicBufferindex - in bytes from which to get.public void putIntVolatile(int index,
int value)
putIntVolatile in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public void putIntOrdered(int index,
int value)
putIntOrdered in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public int addIntOrdered(int index,
int increment)
addIntOrdered in interface AtomicBufferindex - in bytes for where to put.increment - by which the value at the index will be adjusted.public boolean compareAndSetInt(int index,
int expectedValue,
int updateValue)
compareAndSetInt in interface AtomicBufferindex - in bytes for where to put.expectedValue - at to be compared.updateValue - to be exchanged.public int getAndSetInt(int index,
int value)
getAndSetInt in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public int getAndAddInt(int index,
int delta)
getAndAddInt in interface AtomicBufferindex - in bytes for where to put.delta - to be added to the value at the index.public short getShortVolatile(int index)
getShortVolatile in interface AtomicBufferindex - in bytes from which to get.public void putShortVolatile(int index,
short value)
putShortVolatile in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public byte getByteVolatile(int index)
getByteVolatile in interface AtomicBufferindex - in bytes from which to get.public void putByteVolatile(int index,
byte value)
putByteVolatile in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.public char getCharVolatile(int index)
getCharVolatile in interface AtomicBufferindex - in bytes from which to get.public void putCharVolatile(int index,
char value)
putCharVolatile in interface AtomicBufferindex - in bytes for where to put.value - for at a given index.protected final void ensureCapacity(int index,
int length)
ensureCapacity in class AbstractMutableDirectBufferCopyright © 2014-2022 Real Logic Limited. All Rights Reserved.