Class MemoryChunk


  • @NotThreadSafe
    public class MemoryChunk
    extends java.lang.Object
    This class represents chunk of memory residing in off-heap region managed by MemoryPoolAssigner, which is backed by ByteBuffer.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static long BYTE_ARRAY_BASE_OFFSET  
      protected static sun.misc.Unsafe UNSAFE  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      MemoryChunk duplicate()
      Makes the duplicated instance of this MemoryChunk.
      byte get​(int index)
      Reads the byte at the given index.
      void get​(int index, byte[] dst)
      Copies the data of the MemoryChunk from the specified position to target byte array.
      void get​(int index, byte[] dst, int offset, int length)
      Bulk get method using nk.the specified index in the MemoryChunk.
      java.nio.ByteBuffer getBuffer()
      Gets the ByteBuffer from this MemoryChunk.
      char getChar​(int index)
      Reads a char value from the given position.
      double getDouble​(int index)
      Reads a double value from the given position, in the system's native byte order.
      float getFloat​(int index)
      Reads a float value from the given position, in the system's native byte order.
      int getInt​(int index)
      Reads an int value from the given position, in the system's native byte order.
      long getLong​(int index)
      Reads a long value from the given position.
      short getShort​(int index)
      Reads a short integer value from the given position, composing them into a short value according to the current byte order.
      void put​(int index, byte b)
      Writes the given byte into this buffer at the given index.
      void put​(int index, byte[] src)
      Copies all the data from the source byte array into the MemoryChunk beginning at the specified position.
      void put​(int index, byte[] src, int offset, int length)
      Bulk put method using the specified index in the MemoryChunk.
      void putChar​(int index, char value)
      Writes a char value to the given position.
      void putDouble​(int index, double value)
      Writes the given double value to the given position in the system's native byte order.
      void putFloat​(int index, float value)
      Writes the given float value to the given position in the system's native byte order.
      void putInt​(int index, int value)
      Writes the given int value to the given position in the system's native byte order.
      void putLong​(int index, long value)
      Writes the given long value to the given position in the system's native 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 release()
      Frees this MemoryChunk.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNSAFE

        protected static final sun.misc.Unsafe UNSAFE
      • BYTE_ARRAY_BASE_OFFSET

        protected static final long BYTE_ARRAY_BASE_OFFSET
    • Method Detail

      • getBuffer

        public final java.nio.ByteBuffer getBuffer()
        Gets the ByteBuffer from this MemoryChunk.
        Returns:
        ByteBuffer
      • duplicate

        public final MemoryChunk duplicate()
        Makes the duplicated instance of this MemoryChunk.
        Returns:
        the MemoryChunk with the same content of the caller instance
      • release

        public final void release()
        Frees this MemoryChunk. No further operation possible after calling this method.
      • get

        public final byte get​(int index)
        Reads the byte at the given index.
        Parameters:
        index - from which the byte will be read
        Returns:
        the byte at the given position
      • put

        public final void put​(int index,
                              byte b)
        Writes the given byte into this buffer at the given index.
        Parameters:
        index - The position at which the byte will be written.
        b - The byte value to be written.
      • get

        public final void get​(int index,
                              byte[] dst)
        Copies the data of the MemoryChunk from the specified position to target byte array.
        Parameters:
        index - The position at which the first byte will be read.
        dst - The memory into which the memory will be copied.
      • put

        public final void put​(int index,
                              byte[] src)
        Copies all the data from the source byte array into the MemoryChunk beginning at the specified position.
        Parameters:
        index - the position in MemoryChunk to start copying the data.
        src - the source byte array that holds the data to copy.
      • get

        public final void get​(int index,
                              byte[] dst,
                              int offset,
                              int length)
        Bulk get method using nk.the specified index in the MemoryChunk.
        Parameters:
        index - the index in the MemoryChunk to start copying the data.
        dst - the target byte array to copy the data from MemoryChunk.
        offset - the offset in the destination byte array.
        length - the number of bytes to be copied.
      • put

        public final void put​(int index,
                              byte[] src,
                              int offset,
                              int length)
        Bulk put method using the specified index in the MemoryChunk.
        Parameters:
        index - the index in the MemoryChunk to start copying the data.
        src - the source byte array that holds the data to be copied to MemoryChunk.
        offset - the offset in the source byte array.
        length - the number of bytes to be copied.
      • getChar

        public final char getChar​(int index)
        Reads a char value from the given position.
        Parameters:
        index - The position from which the memory will be read.
        Returns:
        The char value at the given position.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus CHAR_SIZE.
      • putChar

        public final void putChar​(int index,
                                  char value)
        Writes a char value to the given position.
        Parameters:
        index - The position at which the memory will be written.
        value - The char value to be written.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus CHAR_SIZE.
      • getShort

        public final short getShort​(int index)
        Reads a short integer value 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:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus SHORT_SIZE.
      • putShort

        public final 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:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus SHORT_SIZE.
      • getInt

        public final int getInt​(int index)
        Reads an int value from the given position, in the system's native byte order.
        Parameters:
        index - The position from which the value will be read.
        Returns:
        The int value at the given position.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus INT_SIZE.
      • putInt

        public final void putInt​(int index,
                                 int value)
        Writes the given int value to the given position in the system's native byte order.
        Parameters:
        index - The position at which the value will be written.
        value - The int value to be written.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus INT_SIZE.
      • getLong

        public final long getLong​(int index)
        Reads a long value from the given position.
        Parameters:
        index - The position from which the value will be read.
        Returns:
        The long value at the given position.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus LONG_SIZE.
      • putLong

        public final void putLong​(int index,
                                  long value)
        Writes the given long value to the given position in the system's native byte order.
        Parameters:
        index - The position at which the value will be written.
        value - The long value to be written.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus LONG_SIZE.
      • getFloat

        public final float getFloat​(int index)
        Reads a float value from the given position, in the system's native byte order.
        Parameters:
        index - The position from which the value will be read.
        Returns:
        The float value at the given position.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus size of float.
      • putFloat

        public final void putFloat​(int index,
                                   float value)
        Writes the given float value to the given position in the system's native byte order.
        Parameters:
        index - The position at which the value will be written.
        value - The float value to be written.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus size of float.
      • getDouble

        public final double getDouble​(int index)
        Reads a double value from the given position, in the system's native byte order.
        Parameters:
        index - The position from which the value will be read.
        Returns:
        The double value at the given position.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus size of double.
      • putDouble

        public final void putDouble​(int index,
                                    double value)
        Writes the given double 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 double value to be written.
        Throws:
        java.lang.IndexOutOfBoundsException - If the index is negative, or larger then the chunk size minus size of double.