Class ByteBufferRandomAccessSource

  • All Implemented Interfaces:
    IRandomAccessSource

    class ByteBufferRandomAccessSource
    extends java.lang.Object
    implements IRandomAccessSource
    A RandomAccessSource that is based on an underlying ByteBuffer. This class takes steps to ensure that the byte buffer is completely freed from memory during close() if unmapping functionality is enabled
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static boolean allowUnmapping
      A flag to allow unmapping hack for cleaning mapped buffer
      private java.nio.ByteBuffer byteBuffer
      Internal cache of memory mapped buffers
      private static BufferCleaner CLEANER
      Reference to a BufferCleaner that does unmapping; null if not supported.
      static boolean UNMAP_SUPPORTED
      true, if this platform supports unmapping mmapped files.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean clean​(java.nio.ByteBuffer buffer)
      invokes the clean method on the ByteBuffer's cleaner
      private static boolean cleanByUnmapping​(java.nio.ByteBuffer buffer)  
      void close()
      Closes this source.
      static void disableByteBufferMemoryUnmapping()
      Disables ByteBuffer memory unmapping hack
      static void enableByteBufferMemoryUnmapping()
      Enables ByteBuffer memory unmapping hack
      int get​(long position)
      Gets a byte at the specified position
      int get​(long position, byte[] bytes, int off, int len)
      Read an array of bytes of specified length from the specified position of source to the buffer applying the offset.
      long length()
      Gets the length of the source
      • Methods inherited from class java.lang.Object

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

      • allowUnmapping

        private static boolean allowUnmapping
        A flag to allow unmapping hack for cleaning mapped buffer
      • byteBuffer

        private final java.nio.ByteBuffer byteBuffer
        Internal cache of memory mapped buffers
      • UNMAP_SUPPORTED

        public static final boolean UNMAP_SUPPORTED
        true, if this platform supports unmapping mmapped files.
      • CLEANER

        private static final BufferCleaner CLEANER
        Reference to a BufferCleaner that does unmapping; null if not supported.
    • Constructor Detail

      • ByteBufferRandomAccessSource

        public ByteBufferRandomAccessSource​(java.nio.ByteBuffer byteBuffer)
        Constructs a new ByteBufferRandomAccessSource based on the specified ByteBuffer
        Parameters:
        byteBuffer - the buffer to use as the backing store
    • Method Detail

      • enableByteBufferMemoryUnmapping

        public static void enableByteBufferMemoryUnmapping()
        Enables ByteBuffer memory unmapping hack
      • disableByteBufferMemoryUnmapping

        public static void disableByteBufferMemoryUnmapping()
        Disables ByteBuffer memory unmapping hack
      • get

        public int get​(long position)
        Gets a byte at the specified position

        Note: Because ByteBuffers don't support long indexing, the position must be a valid positive int

        Specified by:
        get in interface IRandomAccessSource
        Parameters:
        position - the position to read the byte from - must be less than Integer.MAX_VALUE
        Returns:
        the byte, or -1 if EOF is reached
      • get

        public int get​(long position,
                       byte[] bytes,
                       int off,
                       int len)
        Read an array of bytes of specified length from the specified position of source to the buffer applying the offset. If the number of bytes requested cannot be read, all the possible bytes will be read to the buffer, and the number of actually read bytes will be returned.

        Note: Because ByteBuffers don't support long indexing, the position must be a valid positive int

        Specified by:
        get in interface IRandomAccessSource
        Parameters:
        position - the position to read the byte from - must be less than Integer.MAX_VALUE
        bytes - output buffer
        off - offset into the output buffer where results will be placed
        len - the number of bytes to read
        Returns:
        the number of bytes actually read, or -1 if the file is at EOF
      • length

        public long length()
        Gets the length of the source
        Specified by:
        length in interface IRandomAccessSource
        Returns:
        the length of this source
      • close

        public void close()
                   throws java.io.IOException
        Description copied from interface: IRandomAccessSource
        Closes this source. The underlying data structure or source (if any) will also be closed
        Specified by:
        close in interface IRandomAccessSource
        Throws:
        java.io.IOException - in case of any reading error.
        See Also:
        Cleans the mapped bytebuffers and closes the channel if unmapping functionality is enabled
      • clean

        private static boolean clean​(java.nio.ByteBuffer buffer)
        invokes the clean method on the ByteBuffer's cleaner
        Parameters:
        buffer - ByteBuffer
        Returns:
        boolean true on success
      • cleanByUnmapping

        private static boolean cleanByUnmapping​(java.nio.ByteBuffer buffer)