@Internal public final class MemorySegment extends Object
The memory can be on-heap, off-heap direct or off-heap unsafe. This is transparently handled by this class.
This class fulfills conceptually a similar purpose as Java's ByteBuffer
. We
add this specialized class for various reasons:
Comments on the implementation: We make heavy use of operations that are supported by native instructions, to achieve a high efficiency. Multi byte types (int, long, float, double, ...) are read and written with "unsafe" native commands.
Note on efficiency: For best efficiency, we do not separate implementations of different memory types with inheritance, to avoid the overhead from looking for concrete implementations on invocations of abstract methods.
Modifier and Type | Field and Description |
---|---|
static String |
CHECK_MULTIPLE_FREE_PROPERTY
System property for activating multiple free segment check, for testing purpose.
|
Modifier and Type | Method and Description |
---|---|
int |
compare(MemorySegment seg2,
int offset1,
int offset2,
int len)
Compares two memory segment regions.
|
int |
compare(MemorySegment seg2,
int offset1,
int offset2,
int len1,
int len2)
Compares two memory segment regions with different length.
|
void |
copyFromUnsafe(int offset,
Object source,
int sourcePointer,
int numBytes)
Bulk copy method.
|
void |
copyTo(int offset,
MemorySegment target,
int targetOffset,
int numBytes)
Bulk copy method.
|
void |
copyToUnsafe(int offset,
Object target,
int targetPointer,
int numBytes)
Bulk copy method.
|
boolean |
equalTo(MemorySegment seg2,
int offset1,
int offset2,
int length)
Equals two memory segment regions.
|
void |
free()
Frees this memory segment.
|
void |
get(DataOutput out,
int offset,
int length) |
byte |
get(int index)
Reads the byte at the given position.
|
void |
get(int index,
byte[] dst)
Bulk get method.
|
void |
get(int index,
byte[] dst,
int offset,
int length)
Bulk get method.
|
void |
get(int offset,
ByteBuffer target,
int numBytes)
Bulk get method.
|
long |
getAddress()
Returns the memory address of off-heap memory segments.
|
byte[] |
getArray()
Returns the byte array of on-heap memory segments.
|
boolean |
getBoolean(int index)
Reads one byte at the given position and returns its boolean representation.
|
char |
getChar(int index)
Reads a char value from the given position, in the system's native byte order.
|
char |
getCharBigEndian(int index)
Reads a character value (16 bit, 2 bytes) from the given position, in big-endian byte order.
|
char |
getCharLittleEndian(int index)
Reads a character value (16 bit, 2 bytes) from the given position, in little-endian byte
order.
|
double |
getDouble(int index)
Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in
the system's native byte order.
|
double |
getDoubleBigEndian(int index)
Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in
big endian byte order.
|
double |
getDoubleLittleEndian(int index)
Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in
little endian byte order.
|
float |
getFloat(int index)
Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in
the system's native byte order.
|
float |
getFloatBigEndian(int index)
Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in
big endian byte order.
|
float |
getFloatLittleEndian(int index)
Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in
little endian byte order.
|
byte[] |
getHeapMemory()
Get the heap byte array object.
|
int |
getInt(int index)
Reads an int value (32bit, 4 bytes) from the given position, in the system's native byte
order.
|
int |
getIntBigEndian(int index)
Reads an int value (32bit, 4 bytes) from the given position, in big-endian byte order.
|
int |
getIntLittleEndian(int index)
Reads an int value (32bit, 4 bytes) from the given position, in little-endian byte order.
|
long |
getLong(int index)
Reads a long value (64bit, 8 bytes) from the given position, in the system's native byte
order.
|
long |
getLongBigEndian(int index)
Reads a long integer value (64bit, 8 bytes) from the given position, in big endian byte
order.
|
long |
getLongLittleEndian(int index)
Reads a long integer value (64bit, 8 bytes) from the given position, in little endian byte
order.
|
Object |
getOwner()
Gets the owner of this memory segment.
|
short |
getShort(int index)
Reads a short integer value (16 bit, 2 bytes) from the given position, composing them into a
short value according to the current byte order.
|
short |
getShortBigEndian(int index)
Reads a short integer value (16 bit, 2 bytes) from the given position, in big-endian byte
order.
|
short |
getShortLittleEndian(int index)
Reads a short integer value (16 bit, 2 bytes) from the given position, in little-endian byte
order.
|
boolean |
isFreed()
Checks whether the memory segment was freed.
|
boolean |
isOffHeap()
Checks whether this memory segment is backed by off-heap memory.
|
void |
processAsByteBuffer(java.util.function.Consumer<ByteBuffer> processConsumer)
Supplies a
ByteBuffer that represents this entire segment to the given process
consumer. |
<T> T |
processAsByteBuffer(java.util.function.Function<ByteBuffer,T> processFunction)
Applies the given process function on a
ByteBuffer that represents this entire
segment. |
void |
put(DataInput in,
int offset,
int length)
Bulk put method.
|
void |
put(int index,
byte b)
Writes the given byte into this buffer at the given position.
|
void |
put(int index,
byte[] src)
Bulk put method.
|
void |
put(int index,
byte[] src,
int offset,
int length)
Bulk put method.
|
void |
put(int offset,
ByteBuffer source,
int numBytes)
Bulk put method.
|
void |
putBoolean(int index,
boolean value)
Writes one byte containing the byte value into this buffer at the given position.
|
void |
putChar(int index,
char value)
Writes a char value to the given position, in the system's native byte order.
|
void |
putCharBigEndian(int index,
char value)
Writes the given character (16 bit, 2 bytes) to the given position in big-endian byte order.
|
void |
putCharLittleEndian(int index,
char value)
Writes the given character (16 bit, 2 bytes) to the given position in little-endian byte
order.
|
void |
putDouble(int index,
double value)
Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position
in the system's native byte order.
|
void |
putDoubleBigEndian(int index,
double value)
Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position
in big endian byte order.
|
void |
putDoubleLittleEndian(int index,
double value)
Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position
in little endian byte order.
|
void |
putFloat(int index,
float value)
Writes the given single-precision float value (32bit, 4 bytes) to the given position in the
system's native byte order.
|
void |
putFloatBigEndian(int index,
float value)
Writes the given single-precision float value (32bit, 4 bytes) to the given position in big
endian byte order.
|
void |
putFloatLittleEndian(int index,
float value)
Writes the given single-precision float value (32bit, 4 bytes) to the given position in
little endian byte order.
|
void |
putInt(int index,
int value)
Writes the given int value (32bit, 4 bytes) to the given position in the system's native byte
order.
|
void |
putIntBigEndian(int index,
int value)
Writes the given int value (32bit, 4 bytes) to the given position in big endian byte order.
|
void |
putIntLittleEndian(int index,
int value)
Writes the given int value (32bit, 4 bytes) to the given position in little endian byte
order.
|
void |
putLong(int index,
long value)
Writes the given long value (64bit, 8 bytes) to the given position in the system's native
byte order.
|
void |
putLongBigEndian(int index,
long value)
Writes the given long value (64bit, 8 bytes) to the given position in big endian byte order.
|
void |
putLongLittleEndian(int index,
long value)
Writes the given long value (64bit, 8 bytes) to the given position in little endian byte
order.
|
void |
putShort(int index,
short value)
Writes the given short value into this buffer at the given position, using the native byte
order of the system.
|
void |
putShortBigEndian(int index,
short value)
Writes the given short integer value (16 bit, 2 bytes) to the given position in big-endian
byte order.
|
void |
putShortLittleEndian(int index,
short value)
Writes the given short integer value (16 bit, 2 bytes) to the given position in little-endian
byte order.
|
int |
size()
Gets the size of the memory segment, in bytes.
|
void |
swapBytes(byte[] tempBuffer,
MemorySegment seg2,
int offset1,
int offset2,
int len)
Swaps bytes between two memory segments, using the given auxiliary buffer.
|
ByteBuffer |
wrap(int offset,
int length)
Wraps the chunk of the underlying memory located between offset and offset +
length in a NIO ByteBuffer.
|
public static final String CHECK_MULTIPLE_FREE_PROPERTY
public int size()
@VisibleForTesting public boolean isFreed()
public void free()
After this operation has been called, no further operations are possible on the memory segment and will fail. The actual memory (heap or off-heap) will only be released after this memory segment object has become garbage collected.
public boolean isOffHeap()
public byte[] getArray()
IllegalStateException
- if the memory segment does not represent on-heap memorypublic long getAddress()
IllegalStateException
- if the memory segment does not represent off-heap memorypublic ByteBuffer wrap(int offset, int length)
offset
- The offset in the memory segment.length
- The number of bytes to be wrapped as a buffer.IndexOutOfBoundsException
- Thrown, if offset is negative or larger than the memory
segment size, or if the offset plus the length is larger than the segment size.@Nullable public Object getOwner()
public byte get(int index)
index
- The position from which the byte will be readIndexOutOfBoundsException
- Thrown, if the index is negative, or larger or equal to the
size of the memory segment.public void put(int index, byte b)
index
- The index at which the byte will be written.b
- The byte value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger or equal to the
size of the memory segment.public void get(int index, byte[] dst)
index
- The position at which the first byte will be read.dst
- The memory into which the memory will be copied.IndexOutOfBoundsException
- Thrown, if the index is negative, or too large that the
data between the index and the memory segment end is not enough to fill the destination
array.public void put(int index, byte[] src)
index
- The index in the memory segment array, where the data is put.src
- The source array to copy the data from.IndexOutOfBoundsException
- Thrown, if the index is negative, or too large such that
the array size exceed the amount of memory between the index and the memory segment's
end.public void get(int index, byte[] dst, int offset, int length)
index
- The position at which the first byte will be read.dst
- The memory into which the memory will be copied.offset
- The copying offset in the destination memory.length
- The number of bytes to be copied.IndexOutOfBoundsException
- Thrown, if the index is negative, or too large that the
requested number of bytes exceed the amount of memory between the index and the memory
segment's end.public void put(int index, byte[] src, int offset, int length)
index
- The position in the memory segment array, where the data is put.src
- The source array to copy the data from.offset
- The offset in the source array where the copying is started.length
- The number of bytes to copy.IndexOutOfBoundsException
- Thrown, if the index is negative, or too large such that
the array portion to copy exceed the amount of memory between the index and the memory
segment's end.public boolean getBoolean(int index)
index
- The position from which the memory will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 1.public void putBoolean(int index, boolean value)
index
- The position at which the memory will be written.value
- The char value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 1.public char getChar(int index)
index
- The position from which the memory will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public char getCharLittleEndian(int index)
getChar(int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and getChar(int)
is the
preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public char getCharBigEndian(int index)
getChar(int)
. For most cases (such as transient storage in memory or serialization
for I/O and network), it suffices to know that the byte order in which the value is written
is the same as the one in which it is read, and getChar(int)
is the preferable
choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public void putChar(int index, char value)
index
- The position at which the memory will be written.value
- The char value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public void putCharLittleEndian(int index, char value)
putChar(int, char)
. For most cases (such as transient storage in memory
or serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and putChar(int, char)
is the preferable choice.index
- The position at which the value will be written.value
- The char value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public void putCharBigEndian(int index, char value)
putChar(int, char)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and putChar(int, char)
is the preferable choice.index
- The position at which the value will be written.value
- The char value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public short getShort(int index)
index
- The position from which the memory will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public short getShortLittleEndian(int index)
getShort(int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and getShort(int)
is
the preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public short getShortBigEndian(int index)
getShort(int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and getShort(int)
is
the preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public void putShort(int index, short value)
index
- The position at which the value will be written.value
- The short value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public void putShortLittleEndian(int index, short value)
putShort(int, short)
. For most cases (such as transient storage in
memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and putShort(int, short)
is the preferable choice.index
- The position at which the value will be written.value
- The short value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public void putShortBigEndian(int index, short value)
putShort(int, short)
. For most cases (such as transient storage in
memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and putShort(int, short)
is the preferable choice.index
- The position at which the value will be written.value
- The short value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 2.public int getInt(int index)
index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public int getIntLittleEndian(int index)
getInt(int)
. For most cases (such as transient storage in memory or serialization
for I/O and network), it suffices to know that the byte order in which the value is written
is the same as the one in which it is read, and getInt(int)
is the preferable
choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public int getIntBigEndian(int index)
getInt(int)
. For most cases (such as transient storage in memory or serialization
for I/O and network), it suffices to know that the byte order in which the value is written
is the same as the one in which it is read, and getInt(int)
is the preferable
choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public void putInt(int index, int value)
index
- The position at which the value will be written.value
- The int value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public void putIntLittleEndian(int index, int value)
putInt(int, int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and putInt(int, int)
is
the preferable choice.index
- The position at which the value will be written.value
- The int value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public void putIntBigEndian(int index, int value)
putInt(int, int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and putInt(int, int)
is
the preferable choice.index
- The position at which the value will be written.value
- The int value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public long getLong(int index)
index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public long getLongLittleEndian(int index)
getLong(int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and getLong(int)
is the
preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public long getLongBigEndian(int index)
getLong(int)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and getLong(int)
is the
preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void putLong(int index, long value)
index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void putLongLittleEndian(int index, long value)
putLong(int, long)
. For most cases (such as transient storage in memory
or serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and putLong(int, long)
is the preferable choice.index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void putLongBigEndian(int index, long value)
putLong(int, long)
. For most cases (such as transient storage in memory or
serialization for I/O and network), it suffices to know that the byte order in which the
value is written is the same as the one in which it is read, and putLong(int, long)
is the preferable choice.index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public float getFloat(int index)
index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public float getFloatLittleEndian(int index)
getFloat(int)
. For most cases (such as transient storage
in memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and getFloat(int)
is the preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public float getFloatBigEndian(int index)
getFloat(int)
. For most cases (such as transient storage in
memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and getFloat(int)
is the preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public void putFloat(int index, float value)
index
- The position at which the value will be written.value
- The float value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public void putFloatLittleEndian(int index, float value)
putFloat(int, float)
. For most cases (such as transient
storage in memory or serialization for I/O and network), it suffices to know that the byte
order in which the value is written is the same as the one in which it is read, and putFloat(int, float)
is the preferable choice.index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public void putFloatBigEndian(int index, float value)
putFloat(int, float)
. For most cases (such as transient storage
in memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and putFloat(int, float)
is the preferable choice.index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 4.public double getDouble(int index)
index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public double getDoubleLittleEndian(int index)
getDouble(int)
. For most cases (such as transient storage
in memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and getDouble(int)
is the preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public double getDoubleBigEndian(int index)
getDouble(int)
. For most cases (such as transient storage in
memory or serialization for I/O and network), it suffices to know that the byte order in
which the value is written is the same as the one in which it is read, and getDouble(int)
is the preferable choice.index
- The position from which the value will be read.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void putDouble(int index, double value)
index
- The position at which the memory will be written.value
- The double value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void putDoubleLittleEndian(int index, double value)
putDouble(int, double)
. For most cases (such as
transient storage in memory or serialization for I/O and network), it suffices to know that
the byte order in which the value is written is the same as the one in which it is read, and
putDouble(int, double)
is the preferable choice.index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void putDoubleBigEndian(int index, double value)
putDouble(int, double)
. For most cases (such as transient
storage in memory or serialization for I/O and network), it suffices to know that the byte
order in which the value is written is the same as the one in which it is read, and putDouble(int, double)
is the preferable choice.index
- The position at which the value will be written.value
- The long value to be written.IndexOutOfBoundsException
- Thrown, if the index is negative, or larger than the
segment size minus 8.public void get(DataOutput out, int offset, int length) throws IOException
IOException
public void put(DataInput in, int offset, int length) throws IOException
in
- The DataInput to get the data from.offset
- The position in the memory segment to copy the chunk to.length
- The number of bytes to get.IOException
- Thrown, if the DataInput encountered a problem upon reading, such as an
End-Of-File.public void get(int offset, ByteBuffer target, int numBytes)
numBytes
bytes from this memory segment, starting at position
offset
to the target ByteBuffer
. The bytes will be put into the target buffer
starting at the buffer's current position. If this method attempts to write more bytes than
the target byte buffer has remaining (with respect to Buffer.remaining()
), this
method will cause a BufferOverflowException
.offset
- The position where the bytes are started to be read from in this memory
segment.target
- The ByteBuffer to copy the bytes to.numBytes
- The number of bytes to copy.IndexOutOfBoundsException
- If the offset is invalid, or this segment does not contain
the given number of bytes (starting from offset), or the target byte buffer does not have
enough space for the bytes.ReadOnlyBufferException
- If the target buffer is read-only.public void put(int offset, ByteBuffer source, int numBytes)
numBytes
bytes from the given ByteBuffer
, into this
memory segment. The bytes will be read from the target buffer starting at the buffer's
current position, and will be written to this memory segment starting at offset
. If
this method attempts to read more bytes than the target byte buffer has remaining (with
respect to Buffer.remaining()
), this method will cause a BufferUnderflowException
.offset
- The position where the bytes are started to be written to in this memory
segment.source
- The ByteBuffer to copy the bytes from.numBytes
- The number of bytes to copy.IndexOutOfBoundsException
- If the offset is invalid, or the source buffer does not
contain the given number of bytes, or this segment does not have enough space for the
bytes (counting from offset).public void copyTo(int offset, MemorySegment target, int targetOffset, int numBytes)
numBytes
bytes from this memory segment, starting at
position offset
to the target memory segment. The bytes will be put into the target
segment starting at position targetOffset
.offset
- The position where the bytes are started to be read from in this memory
segment.target
- The memory segment to copy the bytes to.targetOffset
- The position in the target memory segment to copy the chunk to.numBytes
- The number of bytes to copy.IndexOutOfBoundsException
- If either of the offsets is invalid, or the source segment
does not contain the given number of bytes (starting from offset), or the target segment
does not have enough space for the bytes (counting from targetOffset).public void copyToUnsafe(int offset, Object target, int targetPointer, int numBytes)
numBytes
bytes to target unsafe object and pointer. NOTE:
This is an unsafe method, no check here, please be careful.offset
- The position where the bytes are started to be read from in this memory
segment.target
- The unsafe memory to copy the bytes to.targetPointer
- The position in the target unsafe memory to copy the chunk to.numBytes
- The number of bytes to copy.IndexOutOfBoundsException
- If the source segment does not contain the given number of
bytes (starting from offset).public void copyFromUnsafe(int offset, Object source, int sourcePointer, int numBytes)
numBytes
bytes from source unsafe object and pointer. NOTE:
This is an unsafe method, no check here, please be careful.offset
- The position where the bytes are started to be write in this memory segment.source
- The unsafe memory to copy the bytes from.sourcePointer
- The position in the source unsafe memory to copy the chunk from.numBytes
- The number of bytes to copy.IndexOutOfBoundsException
- If this segment can not contain the given number of bytes
(starting from offset).public int compare(MemorySegment seg2, int offset1, int offset2, int len)
seg2
- Segment to compare this segment withoffset1
- Offset of this segment to start comparingoffset2
- Offset of seg2 to start comparinglen
- Length of the compared memory regionpublic int compare(MemorySegment seg2, int offset1, int offset2, int len1, int len2)
seg2
- Segment to compare this segment withoffset1
- Offset of this segment to start comparingoffset2
- Offset of seg2 to start comparinglen1
- Length of this memory region to comparelen2
- Length of seg2 to comparepublic void swapBytes(byte[] tempBuffer, MemorySegment seg2, int offset1, int offset2, int len)
tempBuffer
- The auxiliary buffer in which to put data during triangle swap.seg2
- Segment to swap bytes withoffset1
- Offset of this segment to start swappingoffset2
- Offset of seg2 to start swappinglen
- Length of the swapped memory regionpublic boolean equalTo(MemorySegment seg2, int offset1, int offset2, int length)
seg2
- Segment to equal this segment withoffset1
- Offset of this segment to start equalingoffset2
- Offset of seg2 to start equalinglength
- Length of the equaled memory regionpublic byte[] getHeapMemory()
public <T> T processAsByteBuffer(java.util.function.Function<ByteBuffer,T> processFunction)
ByteBuffer
that represents this entire
segment.
Note: The ByteBuffer
passed into the process function is temporary and could
become invalid after the processing. Thus, the process function should not try to keep any
reference of the ByteBuffer
.
processFunction
- to be applied to the segment as ByteBuffer
.public void processAsByteBuffer(java.util.function.Consumer<ByteBuffer> processConsumer)
ByteBuffer
that represents this entire segment to the given process
consumer.
Note: The ByteBuffer
passed into the process consumer is temporary and could
become invalid after the processing. Thus, the process consumer should not try to keep any
reference of the ByteBuffer
.
processConsumer
- to accept the segment as ByteBuffer
.Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.