Class RandomAccessBuffer

java.lang.Object
org.apache.pdfbox.io.RandomAccessBuffer
All Implemented Interfaces:
Closeable, AutoCloseable, Cloneable, RandomAccess, RandomAccessRead, RandomAccessWrite

public class RandomAccessBuffer extends Object implements RandomAccess, Cloneable
An implementation of the RandomAccess interface to store data in memory. The data will be stored in chunks organized in an ArrayList.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private List<byte[]>
     
    private int
     
    private int
     
    private int
     
    private byte[]
     
    private int
     
    private static final int
     
    private long
     
    private long
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Default constructor.
     
    RandomAccessBuffer(byte[] input)
    Create a random access buffer using the given byte array.
    private
    RandomAccessBuffer(int definedChunkSize)
    Default constructor.
     
    Create a random access buffer of the given input stream by copying the data.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns an estimate of the number of bytes that can be read.
    private void
    Ensure that the RandomAccessBuffer is not closed
    void
    Clears all data of the buffer.
     
    void
    private void
    create a new buffer chunk and adjust all pointers and indices.
    long
    Returns offset of next byte to be returned by a read method.
    boolean
    Returns true if this stream has been closed.
    boolean
    A simple test to see if we are at the end of the data.
    long
    The total number of bytes that are available.
    private void
    switch to the next buffer chunk and reset the buffer pointer.
    int
    This will peek at the next byte.
    int
    Read a single byte of data.
    int
    read(byte[] b)
    Read a buffer of data.
    int
    read(byte[] b, int offset, int length)
    Read a buffer of data.
    byte[]
    readFully(int length)
    Reads a given number of bytes.
    private int
    readRemainingBytes(byte[] b, int offset, int length)
     
    void
    rewind(int bytes)
    Seek backwards the given number of bytes.
    void
    seek(long position)
    Seek to a position in the data.
    void
    write(byte[] b)
    Write a buffer of data to the stream.
    void
    write(byte[] b, int offset, int length)
    Write a buffer of data to the stream.
    void
    write(int b)
    Write a byte to the stream.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_CHUNK_SIZE

      private static final int DEFAULT_CHUNK_SIZE
      See Also:
    • chunkSize

      private int chunkSize
    • bufferList

      private List<byte[]> bufferList
    • currentBuffer

      private byte[] currentBuffer
    • pointer

      private long pointer
    • currentBufferPointer

      private int currentBufferPointer
    • size

      private long size
    • bufferListIndex

      private int bufferListIndex
    • bufferListMaxIndex

      private int bufferListMaxIndex
  • Constructor Details

    • RandomAccessBuffer

      public RandomAccessBuffer()
      Default constructor.
    • RandomAccessBuffer

      private RandomAccessBuffer(int definedChunkSize)
      Default constructor.
    • RandomAccessBuffer

      public RandomAccessBuffer(byte[] input)
      Create a random access buffer using the given byte array.
      Parameters:
      input - the byte array to be read
    • RandomAccessBuffer

      public RandomAccessBuffer(InputStream input) throws IOException
      Create a random access buffer of the given input stream by copying the data.
      Parameters:
      input - the input stream to be read
      Throws:
      IOException - if something went wrong while copying the data
  • Method Details

    • clone

      public RandomAccessBuffer clone()
      Overrides:
      clone in class Object
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • clear

      public void clear()
      Clears all data of the buffer.
      Specified by:
      clear in interface RandomAccessWrite
    • seek

      public void seek(long position) throws IOException
      Seek to a position in the data.
      Specified by:
      seek in interface RandomAccessRead
      Parameters:
      position - The position to seek to.
      Throws:
      IOException - If there is an error while seeking.
    • getPosition

      public long getPosition() throws IOException
      Returns offset of next byte to be returned by a read method.
      Specified by:
      getPosition in interface RandomAccessRead
      Returns:
      offset of next byte which will be returned with next RandomAccessRead.read() (if no more bytes are left it returns a value >= length of source)
      Throws:
      IOException
    • read

      public int read() throws IOException
      Read a single byte of data.
      Specified by:
      read in interface RandomAccessRead
      Returns:
      The byte of data that is being read.
      Throws:
      IOException - If there is an error while reading the data.
    • read

      public int read(byte[] b, int offset, int length) throws IOException
      Read a buffer of data.
      Specified by:
      read in interface RandomAccessRead
      Parameters:
      b - The buffer to write the data to.
      offset - Offset into the buffer to start writing.
      length - The amount of data to attempt to read.
      Returns:
      The number of bytes that were actually read.
      Throws:
      IOException - If there was an error while reading the data.
    • readRemainingBytes

      private int readRemainingBytes(byte[] b, int offset, int length)
    • length

      public long length() throws IOException
      The total number of bytes that are available.
      Specified by:
      length in interface RandomAccessRead
      Returns:
      The number of bytes available.
      Throws:
      IOException - If there is an IO error while determining the length of the data stream.
    • write

      public void write(int b) throws IOException
      Write a byte to the stream.
      Specified by:
      write in interface RandomAccessWrite
      Parameters:
      b - The byte to write.
      Throws:
      IOException - If there is an IO error while writing.
    • write

      public void write(byte[] b) throws IOException
      Write a buffer of data to the stream.
      Specified by:
      write in interface RandomAccessWrite
      Parameters:
      b - The buffer to get the data from.
      Throws:
      IOException - If there is an error while writing the data.
    • write

      public void write(byte[] b, int offset, int length) throws IOException
      Write a buffer of data to the stream.
      Specified by:
      write in interface RandomAccessWrite
      Parameters:
      b - The buffer to get the data from.
      offset - An offset into the buffer to get the data from.
      length - The length of data to write.
      Throws:
      IOException - If there is an error while writing the data.
    • expandBuffer

      private void expandBuffer() throws IOException
      create a new buffer chunk and adjust all pointers and indices.
      Throws:
      IOException
    • nextBuffer

      private void nextBuffer() throws IOException
      switch to the next buffer chunk and reset the buffer pointer.
      Throws:
      IOException
    • checkClosed

      private void checkClosed() throws IOException
      Ensure that the RandomAccessBuffer is not closed
      Throws:
      IOException - if the RandomAccessBuffer is already closed
    • isClosed

      public boolean isClosed()
      Returns true if this stream has been closed.
      Specified by:
      isClosed in interface RandomAccessRead
    • isEOF

      public boolean isEOF() throws IOException
      A simple test to see if we are at the end of the data.
      Specified by:
      isEOF in interface RandomAccessRead
      Returns:
      true if we are at the end of the data.
      Throws:
      IOException - If there is an error reading the next byte.
    • available

      public int available() throws IOException
      Returns an estimate of the number of bytes that can be read.
      Specified by:
      available in interface RandomAccessRead
      Returns:
      the number of bytes that can be read
      Throws:
      IOException - if this random access has been closed
    • peek

      public int peek() throws IOException
      This will peek at the next byte.
      Specified by:
      peek in interface RandomAccessRead
      Returns:
      The next byte on the stream, leaving it as available to read.
      Throws:
      IOException - If there is an error reading the next byte.
    • rewind

      public void rewind(int bytes) throws IOException
      Seek backwards the given number of bytes.
      Specified by:
      rewind in interface RandomAccessRead
      Parameters:
      bytes - the number of bytes to be seeked backwards
      Throws:
      IOException - If there is an error while seeking
    • readFully

      public byte[] readFully(int length) throws IOException
      Reads a given number of bytes.
      Specified by:
      readFully in interface RandomAccessRead
      Parameters:
      length - the number of bytes to be read
      Returns:
      a byte array containing the bytes just read
      Throws:
      IOException - if an I/O error occurs while reading data
    • read

      public int read(byte[] b) throws IOException
      Read a buffer of data.
      Specified by:
      read in interface RandomAccessRead
      Parameters:
      b - The buffer to write the data to.
      Returns:
      The number of bytes that were actually read.
      Throws:
      IOException - If there was an error while reading the data.