Class ByteBufferRandomAccessSource

java.lang.Object
com.itextpdf.text.io.ByteBufferRandomAccessSource
All Implemented Interfaces:
RandomAccessSource

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

    • byteBuffer

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

    • get

      public int get(long position) throws 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:
      IOException
    • get

      public int get(long position, byte[] bytes, int off, int len) throws 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:
      IOException
    • length

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

      public void close() throws 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:
      IOException
      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