Class MemorySegment
- java.lang.Object
-
- org.apache.flink.core.memory.MemorySegment
-
@Internal public final class MemorySegment extends Object
This class represents a piece of memory managed by Flink.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:- It offers additional binary compare, swap, and copy methods.
- It uses collapsed checks for range check and memory segment disposal.
- It offers absolute positioning methods for bulk put/get methods, to guarantee thread safe use.
- It offers explicit big-endian / little-endian access methods, rather than tracking internally a byte order.
- It transparently and efficiently moves data between on-heap and off-heap variants.
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.
-
-
Field Summary
Fields Modifier and Type Field Description static StringCHECK_MULTIPLE_FREE_PROPERTYSystem property for activating multiple free segment check, for testing purpose.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompare(MemorySegment seg2, int offset1, int offset2, int len)Compares two memory segment regions.intcompare(MemorySegment seg2, int offset1, int offset2, int len1, int len2)Compares two memory segment regions with different length.voidcopyFromUnsafe(int offset, Object source, int sourcePointer, int numBytes)Bulk copy method.voidcopyTo(int offset, MemorySegment target, int targetOffset, int numBytes)Bulk copy method.voidcopyToUnsafe(int offset, Object target, int targetPointer, int numBytes)Bulk copy method.booleanequalTo(MemorySegment seg2, int offset1, int offset2, int length)Equals two memory segment regions.voidfree()Frees this memory segment.byteget(int index)Reads the byte at the given position.voidget(int index, byte[] dst)Bulk get method.voidget(int index, byte[] dst, int offset, int length)Bulk get method.voidget(int offset, ByteBuffer target, int numBytes)Bulk get method.voidget(DataOutput out, int offset, int length)longgetAddress()Returns the memory address of off-heap memory segments.byte[]getArray()Returns the byte array of on-heap memory segments.booleangetBoolean(int index)Reads one byte at the given position and returns its boolean representation.chargetChar(int index)Reads a char value from the given position, in the system's native byte order.chargetCharBigEndian(int index)Reads a character value (16 bit, 2 bytes) from the given position, in big-endian byte order.chargetCharLittleEndian(int index)Reads a character value (16 bit, 2 bytes) from the given position, in little-endian byte order.doublegetDouble(int index)Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in the system's native byte order.doublegetDoubleBigEndian(int index)Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in big endian byte order.doublegetDoubleLittleEndian(int index)Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in little endian byte order.floatgetFloat(int index)Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in the system's native byte order.floatgetFloatBigEndian(int index)Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in big endian byte order.floatgetFloatLittleEndian(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.intgetInt(int index)Reads an int value (32bit, 4 bytes) from the given position, in the system's native byte order.intgetIntBigEndian(int index)Reads an int value (32bit, 4 bytes) from the given position, in big-endian byte order.intgetIntLittleEndian(int index)Reads an int value (32bit, 4 bytes) from the given position, in little-endian byte order.longgetLong(int index)Reads a long value (64bit, 8 bytes) from the given position, in the system's native byte order.longgetLongBigEndian(int index)Reads a long integer value (64bit, 8 bytes) from the given position, in big endian byte order.longgetLongLittleEndian(int index)Reads a long integer value (64bit, 8 bytes) from the given position, in little endian byte order.ByteBuffergetOffHeapBuffer()Returns the off-heap buffer of memory segments.ObjectgetOwner()Gets the owner of this memory segment.shortgetShort(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.shortgetShortBigEndian(int index)Reads a short integer value (16 bit, 2 bytes) from the given position, in big-endian byte order.shortgetShortLittleEndian(int index)Reads a short integer value (16 bit, 2 bytes) from the given position, in little-endian byte order.booleanisFreed()Checks whether the memory segment was freed.booleanisOffHeap()Checks whether this memory segment is backed by off-heap memory.voidprocessAsByteBuffer(java.util.function.Consumer<ByteBuffer> processConsumer)Supplies aByteBufferthat represents this entire segment to the given process consumer.<T> TprocessAsByteBuffer(java.util.function.Function<ByteBuffer,T> processFunction)Applies the given process function on aByteBufferthat represents this entire segment.voidput(int index, byte b)Writes the given byte into this buffer at the given position.voidput(int index, byte[] src)Bulk put method.voidput(int index, byte[] src, int offset, int length)Bulk put method.voidput(int offset, ByteBuffer source, int numBytes)Bulk put method.voidput(DataInput in, int offset, int length)Bulk put method.voidputBoolean(int index, boolean value)Writes one byte containing the byte value into this buffer at the given position.voidputChar(int index, char value)Writes a char value to the given position, in the system's native byte order.voidputCharBigEndian(int index, char value)Writes the given character (16 bit, 2 bytes) to the given position in big-endian byte order.voidputCharLittleEndian(int index, char value)Writes the given character (16 bit, 2 bytes) to the given position in little-endian byte order.voidputDouble(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.voidputDoubleBigEndian(int index, double value)Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position in big endian byte order.voidputDoubleLittleEndian(int index, double value)Writes the given double-precision floating-point value (64bit, 8 bytes) to the given position in little endian byte order.voidputFloat(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.voidputFloatBigEndian(int index, float value)Writes the given single-precision float value (32bit, 4 bytes) to the given position in big endian byte order.voidputFloatLittleEndian(int index, float value)Writes the given single-precision float value (32bit, 4 bytes) to the given position in little endian byte order.voidputInt(int index, int value)Writes the given int value (32bit, 4 bytes) to the given position in the system's native byte order.voidputIntBigEndian(int index, int value)Writes the given int value (32bit, 4 bytes) to the given position in big endian byte order.voidputIntLittleEndian(int index, int value)Writes the given int value (32bit, 4 bytes) to the given position in little endian byte order.voidputLong(int index, long value)Writes the given long value (64bit, 8 bytes) to the given position in the system's native byte order.voidputLongBigEndian(int index, long value)Writes the given long value (64bit, 8 bytes) to the given position in big endian byte order.voidputLongLittleEndian(int index, long value)Writes the given long value (64bit, 8 bytes) to the given position in little endian byte order.voidputShort(int index, short value)Writes the given short value into this buffer at the given position, using the native byte order of the system.voidputShortBigEndian(int index, short value)Writes the given short integer value (16 bit, 2 bytes) to the given position in big-endian byte order.voidputShortLittleEndian(int index, short value)Writes the given short integer value (16 bit, 2 bytes) to the given position in little-endian byte order.intsize()Gets the size of the memory segment, in bytes.voidswapBytes(byte[] tempBuffer, MemorySegment seg2, int offset1, int offset2, int len)Swaps bytes between two memory segments, using the given auxiliary buffer.ByteBufferwrap(int offset, int length)Wraps the chunk of the underlying memory located between offset and offset + length in a NIO ByteBuffer.
-
-
-
Field Detail
-
CHECK_MULTIPLE_FREE_PROPERTY
public static final String CHECK_MULTIPLE_FREE_PROPERTY
System property for activating multiple free segment check, for testing purpose.- See Also:
- Constant Field Values
-
-
Method Detail
-
size
public int size()
Gets the size of the memory segment, in bytes.- Returns:
- The size of the memory segment.
-
isFreed
@VisibleForTesting public boolean isFreed()
Checks whether the memory segment was freed.- Returns:
- true, if the memory segment has been freed, false otherwise.
-
free
public void free()
Frees this memory segment.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.
-
isOffHeap
public boolean isOffHeap()
Checks whether this memory segment is backed by off-heap memory.- Returns:
- true, if the memory segment is backed by off-heap memory, false if it is backed by heap memory.
-
getArray
public byte[] getArray()
Returns the byte array of on-heap memory segments.- Returns:
- underlying byte array
- Throws:
IllegalStateException- if the memory segment does not represent on-heap memory
-
getOffHeapBuffer
public ByteBuffer getOffHeapBuffer()
Returns the off-heap buffer of memory segments.- Returns:
- underlying off-heap buffer
- Throws:
IllegalStateException- if the memory segment does not represent off-heap buffer
-
getAddress
public long getAddress()
Returns the memory address of off-heap memory segments.- Returns:
- absolute memory address outside the heap
- Throws:
IllegalStateException- if the memory segment does not represent off-heap memory
-
wrap
public ByteBuffer wrap(int offset, int length)
Wraps the chunk of the underlying memory located between offset and offset + length in a NIO ByteBuffer. The ByteBuffer has the full segment as capacity and the offset and length parameters set the buffers position and limit.- Parameters:
offset- The offset in the memory segment.length- The number of bytes to be wrapped as a buffer.- Returns:
- A ByteBuffer backed by the specified portion of the memory segment.
- Throws:
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.
-
getOwner
@Nullable public Object getOwner()
Gets the owner of this memory segment. Returns null, if the owner was not set.- Returns:
- The owner of the memory segment, or null, if it does not have an owner.
-
get
public byte get(int index)
Reads the byte at the given position.- Parameters:
index- The position from which the byte will be read- Returns:
- The byte at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger or equal to the size of the memory segment.
-
put
public void put(int index, byte b)Writes the given byte into this buffer at the given position.- Parameters:
index- The index at which the byte will be written.b- The byte value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger or equal to the size of the memory segment.
-
get
public void get(int index, byte[] dst)Bulk get method. Copies dst.length memory from the specified position to the destination memory.- Parameters:
index- The position at which the first byte will be read.dst- The memory into which the memory will be copied.- Throws:
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.
-
put
public void put(int index, byte[] src)Bulk put method. Copies src.length memory from the source memory into the memory segment beginning at the specified position.- Parameters:
index- The index in the memory segment array, where the data is put.src- The source array to copy the data from.- Throws:
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.
-
get
public void get(int index, byte[] dst, int offset, int length)Bulk get method. Copies length memory from the specified position to the destination memory, beginning at the given offset.- Parameters:
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.- Throws:
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.
-
put
public void put(int index, byte[] src, int offset, int length)Bulk put method. Copies length memory starting at position offset from the source memory into the memory segment starting at the specified index.- Parameters:
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.- Throws:
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.
-
getBoolean
public boolean getBoolean(int index)
Reads one byte at the given position and returns its boolean representation.- Parameters:
index- The position from which the memory will be read.- Returns:
- The boolean value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 1.
-
putBoolean
public void putBoolean(int index, boolean value)Writes one byte containing the byte value into this buffer at the given position.- Parameters:
index- The position at which the memory will be written.value- The char value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 1.
-
getChar
public char getChar(int index)
Reads a char value from the given position, in the system's native byte order.- Parameters:
index- The position from which the memory will be read.- Returns:
- The char value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
getCharLittleEndian
public char getCharLittleEndian(int index)
Reads a character value (16 bit, 2 bytes) from the given position, in little-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetChar(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, andgetChar(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The character value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
getCharBigEndian
public char getCharBigEndian(int index)
Reads a character value (16 bit, 2 bytes) from the given position, in big-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetChar(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, andgetChar(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The character value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
putChar
public void putChar(int index, char value)Writes a char value to the given position, in the system's native byte order.- Parameters:
index- The position at which the memory will be written.value- The char value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
putCharLittleEndian
public void putCharLittleEndian(int index, char value)Writes the given character (16 bit, 2 bytes) to the given position in little-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thanputChar(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, andputChar(int, char)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The char value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
putCharBigEndian
public void putCharBigEndian(int index, char value)Writes the given character (16 bit, 2 bytes) to the given position in big-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thanputChar(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, andputChar(int, char)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The char value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
getShort
public 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.- Parameters:
index- The position from which the memory will be read.- Returns:
- The short value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
getShortLittleEndian
public short getShortLittleEndian(int index)
Reads a short integer value (16 bit, 2 bytes) from the given position, in little-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetShort(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, andgetShort(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The short value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
getShortBigEndian
public short getShortBigEndian(int index)
Reads a short integer value (16 bit, 2 bytes) from the given position, in big-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetShort(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, andgetShort(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The short value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
putShort
public 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.- Parameters:
index- The position at which the value will be written.value- The short value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
putShortLittleEndian
public 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. This method's speed depends on the system's native byte order, and it is possibly slower thanputShort(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, andputShort(int, short)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The short value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
putShortBigEndian
public 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. This method's speed depends on the system's native byte order, and it is possibly slower thanputShort(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, andputShort(int, short)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The short value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 2.
-
getInt
public int getInt(int index)
Reads an int value (32bit, 4 bytes) from the given position, in the system's native byte order. This method offers the best speed for integer reading and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The int value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
getIntLittleEndian
public int getIntLittleEndian(int index)
Reads an int value (32bit, 4 bytes) from the given position, in little-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetInt(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, andgetInt(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The int value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
getIntBigEndian
public int getIntBigEndian(int index)
Reads an int value (32bit, 4 bytes) from the given position, in big-endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetInt(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, andgetInt(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The int value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
putInt
public 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. This method offers the best speed for integer writing and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position at which the value will be written.value- The int value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
putIntLittleEndian
public void putIntLittleEndian(int index, int value)Writes the given int value (32bit, 4 bytes) to the given position in little endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thanputInt(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, andputInt(int, int)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The int value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
putIntBigEndian
public void putIntBigEndian(int index, int value)Writes the given int value (32bit, 4 bytes) to the given position in big endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thanputInt(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, andputInt(int, int)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The int value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
getLong
public long getLong(int index)
Reads a long value (64bit, 8 bytes) from the given position, in the system's native byte order. This method offers the best speed for long integer reading and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
getLongLittleEndian
public long getLongLittleEndian(int index)
Reads a long integer value (64bit, 8 bytes) from the given position, in little endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetLong(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, andgetLong(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
getLongBigEndian
public long getLongBigEndian(int index)
Reads a long integer value (64bit, 8 bytes) from the given position, in big endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetLong(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, andgetLong(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
putLong
public 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. This method offers the best speed for long integer writing and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
putLongLittleEndian
public void putLongLittleEndian(int index, long value)Writes the given long value (64bit, 8 bytes) to the given position in little endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thanputLong(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, andputLong(int, long)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
putLongBigEndian
public void putLongBigEndian(int index, long value)Writes the given long value (64bit, 8 bytes) to the given position in big endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thanputLong(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, andputLong(int, long)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
getFloat
public 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. This method offers the best speed for float reading and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The float value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
getFloatLittleEndian
public float getFloatLittleEndian(int index)
Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in little endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetFloat(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, andgetFloat(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
getFloatBigEndian
public float getFloatBigEndian(int index)
Reads a single-precision floating point value (32bit, 4 bytes) from the given position, in big endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetFloat(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, andgetFloat(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
putFloat
public 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. This method offers the best speed for float writing and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position at which the value will be written.value- The float value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
putFloatLittleEndian
public 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. This method's speed depends on the system's native byte order, and it is possibly slower thanputFloat(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, andputFloat(int, float)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
putFloatBigEndian
public 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. This method's speed depends on the system's native byte order, and it is possibly slower thanputFloat(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, andputFloat(int, float)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 4.
-
getDouble
public 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. This method offers the best speed for double reading and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The double value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
getDoubleLittleEndian
public double getDoubleLittleEndian(int index)
Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in little endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetDouble(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, andgetDouble(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
getDoubleBigEndian
public double getDoubleBigEndian(int index)
Reads a double-precision floating point value (64bit, 8 bytes) from the given position, in big endian byte order. This method's speed depends on the system's native byte order, and it is possibly slower thangetDouble(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, andgetDouble(int)is the preferable choice.- Parameters:
index- The position from which the value will be read.- Returns:
- The long value at the given position.
- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
putDouble
public 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. This method offers the best speed for double writing and should be used unless a specific byte order is required. In most cases, 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 (such as transient storage in memory, or serialization for I/O and network), making this method the preferable choice.- Parameters:
index- The position at which the memory will be written.value- The double value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
putDoubleLittleEndian
public 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. This method's speed depends on the system's native byte order, and it is possibly slower thanputDouble(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, andputDouble(int, double)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
putDoubleBigEndian
public 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. This method's speed depends on the system's native byte order, and it is possibly slower thanputDouble(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, andputDouble(int, double)is the preferable choice.- Parameters:
index- The position at which the value will be written.value- The long value to be written.- Throws:
IndexOutOfBoundsException- Thrown, if the index is negative, or larger than the segment size minus 8.
-
get
public void get(DataOutput out, int offset, int length) throws IOException
- Throws:
IOException
-
put
public void put(DataInput in, int offset, int length) throws IOException
Bulk put method. Copies length memory from the given DataInput to the memory starting at position offset.- Parameters:
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.- Throws:
IOException- Thrown, if the DataInput encountered a problem upon reading, such as an End-Of-File.
-
get
public void get(int offset, ByteBuffer target, int numBytes)Bulk get method. CopiesnumBytesbytes from this memory segment, starting at positionoffsetto the targetByteBuffer. 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 toBuffer.remaining()), this method will cause aBufferOverflowException.- Parameters:
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.- Throws:
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.
-
put
public void put(int offset, ByteBuffer source, int numBytes)Bulk put method. CopiesnumBytesbytes from the givenByteBuffer, 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 atoffset. If this method attempts to read more bytes than the target byte buffer has remaining (with respect toBuffer.remaining()), this method will cause aBufferUnderflowException.- Parameters:
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.- Throws:
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).
-
copyTo
public void copyTo(int offset, MemorySegment target, int targetOffset, int numBytes)Bulk copy method. CopiesnumBytesbytes from this memory segment, starting at positionoffsetto the target memory segment. The bytes will be put into the target segment starting at positiontargetOffset.- Parameters:
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.- Throws:
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).
-
copyToUnsafe
public void copyToUnsafe(int offset, Object target, int targetPointer, int numBytes)Bulk copy method. CopiesnumBytesbytes to target unsafe object and pointer. NOTE: This is an unsafe method, no check here, please be careful.- Parameters:
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.- Throws:
IndexOutOfBoundsException- If the source segment does not contain the given number of bytes (starting from offset).
-
copyFromUnsafe
public void copyFromUnsafe(int offset, Object source, int sourcePointer, int numBytes)Bulk copy method. CopiesnumBytesbytes from source unsafe object and pointer. NOTE: This is an unsafe method, no check here, please be careful.- Parameters:
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.- Throws:
IndexOutOfBoundsException- If this segment can not contain the given number of bytes (starting from offset).
-
compare
public int compare(MemorySegment seg2, int offset1, int offset2, int len)
Compares two memory segment regions.- Parameters:
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 region- Returns:
- 0 if equal, -1 if seg1 < seg2, 1 otherwise
-
compare
public int compare(MemorySegment seg2, int offset1, int offset2, int len1, int len2)
Compares two memory segment regions with different length.- Parameters:
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 compare- Returns:
- 0 if equal, -1 if seg1 < seg2, 1 otherwise
-
swapBytes
public void swapBytes(byte[] tempBuffer, MemorySegment seg2, int offset1, int offset2, int len)Swaps bytes between two memory segments, using the given auxiliary buffer.- Parameters:
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 region
-
equalTo
public boolean equalTo(MemorySegment seg2, int offset1, int offset2, int length)
Equals two memory segment regions.- Parameters:
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 region- Returns:
- true if equal, false otherwise
-
getHeapMemory
public byte[] getHeapMemory()
Get the heap byte array object.- Returns:
- Return non-null if the memory is on the heap, and return null if the memory if off the heap.
-
processAsByteBuffer
public <T> T processAsByteBuffer(java.util.function.Function<ByteBuffer,T> processFunction)
Applies the given process function on aByteBufferthat represents this entire segment.Note: The
ByteBufferpassed 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 theByteBuffer.- Parameters:
processFunction- to be applied to the segment asByteBuffer.- Returns:
- the value that the process function returns.
-
processAsByteBuffer
public void processAsByteBuffer(java.util.function.Consumer<ByteBuffer> processConsumer)
Supplies aByteBufferthat represents this entire segment to the given process consumer.Note: The
ByteBufferpassed 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 theByteBuffer.- Parameters:
processConsumer- to accept the segment asByteBuffer.
-
-