Class 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 String CHECK_MULTIPLE_FREE_PROPERTY
      System property for activating multiple free segment check, for testing purpose.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      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.
      void get​(DataOutput out, int offset, int length)  
      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.
      ByteBuffer getOffHeapBuffer()
      Returns the off-heap buffer of memory segments.
      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​(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 put​(DataInput in, int offset, int length)
      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.
    • 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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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 than 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.
        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.
      • 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. Copies 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.
        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. Copies 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.
        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. Copies 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.
        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. Copies numBytes bytes 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. Copies numBytes bytes 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 with
        offset1 - Offset of this segment to start comparing
        offset2 - Offset of seg2 to start comparing
        len - 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 with
        offset1 - Offset of this segment to start comparing
        offset2 - Offset of seg2 to start comparing
        len1 - Length of this memory region to compare
        len2 - 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 with
        offset1 - Offset of this segment to start swapping
        offset2 - Offset of seg2 to start swapping
        len - 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 with
        offset1 - Offset of this segment to start equaling
        offset2 - Offset of seg2 to start equaling
        length - 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 a 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.

        Parameters:
        processFunction - to be applied to the segment as ByteBuffer.
        Returns:
        the value that the process function returns.
      • processAsByteBuffer

        public void processAsByteBuffer​(java.util.function.Consumer<ByteBuffer> processConsumer)
        Supplies a 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.

        Parameters:
        processConsumer - to accept the segment as ByteBuffer.