Class ByteBufferRandomAccessSource

  • All Implemented Interfaces:
    RandomAccessSource

    class ByteBufferRandomAccessSource
    extends java.lang.Object
    implements RandomAccessSource
    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()
    Since:
    5.3.5
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.ByteBuffer byteBuffer
      Internal cache of memory mapped buffers
    • 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
      void close()
      Closes this source.
      int get​(long position)
      Gets a byte at the specified position
      int get​(long position, byte[] bytes, int off, int len)
      Gets an array at the specified position.
      long length()
      • Methods inherited from class java.lang.Object

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

      • byteBuffer

        private final java.nio.ByteBuffer byteBuffer
        Internal cache of memory mapped buffers
    • 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

      • get

        public int get​(long position)
                throws java.io.IOException
        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 RandomAccessSource
        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
        Throws:
        java.io.IOException
      • get

        public int get​(long position,
                       byte[] bytes,
                       int off,
                       int len)
                throws java.io.IOException
        Gets an array at the specified position. If the number of bytes requested cannot be read, the bytes that can be read will be placed in bytes and the number actually read will be returned.

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

        Specified by:
        get in interface RandomAccessSource
        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
        Throws:
        java.io.IOException
      • length

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

        public void close()
                   throws java.io.IOException
        Description copied from interface: RandomAccessSource
        Closes this source. The underlying data structure or source (if any) will also be closed
        Specified by:
        close in interface RandomAccessSource
        Throws:
        java.io.IOException
        See Also:
        Cleans the mapped bytebuffers and closes the channel
      • 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