Class ByteBufferRandomAccessSource

java.lang.Object
com.itextpdf.io.source.ByteBufferRandomAccessSource
All Implemented Interfaces:
IRandomAccessSource

class ByteBufferRandomAccessSource extends 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 Details

    • allowUnmapping

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

      private final 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 Details

    • ByteBufferRandomAccessSource

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

    • 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 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:
      IOException - in case of any reading error.
      See Also:
    • clean

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

      private static boolean cleanByUnmapping(ByteBuffer buffer)