Module org.sejda.io
Package org.sejda.io

Class SeekableSourceView

java.lang.Object
org.sejda.io.BaseSeekableSource
org.sejda.io.SeekableSourceView
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, ReadableByteChannel, SeekableSource

class SeekableSourceView extends BaseSeekableSource
SeekableSource representing a view over a portion of a parent SeekableSource. A view becomes invalid if the parent SeekableSource is closed. The view works on a thread local copy of the parent source so the parent position is not modified when a read method is called on the view.
  • Field Details

    • startingPosition

      private final long startingPosition
    • length

      private final long length
    • currentPosition

      private long currentPosition
    • supplier

      private final Supplier<? extends SeekableSource> supplier
  • Constructor Details

    • SeekableSourceView

      public SeekableSourceView(Supplier<? extends SeekableSource> supplier, String id, long startingPosition, long length)
  • Method Details

    • position

      public long position()
      Returns:
      the current source position as a positive long
    • position

      public SeekableSource position(long newPosition) throws IOException
      Description copied from interface: SeekableSource
      Sets the source position. Setting the position to a value that is greater than the source's size is legal but does not change the size of the source. A later attempt to read bytes at such a position will immediately return an end-of-file indication.
      Parameters:
      newPosition - a non-negative long for the new position
      Returns:
      this source
      Throws:
      IOException
    • size

      public long size()
      Returns:
      The source size, measured in bytes
    • read

      public int read(ByteBuffer dst) throws IOException
      Throws:
      IOException
    • read

      public int read() throws IOException
      Description copied from interface: SeekableSource
      Reads a byte of data from this source. The byte is returned as an integer in the range 0 to 255 ( 0x00-0xff ).
      Returns:
      the next byte of data, or -1 if there is no more data.
      Throws:
      IOException
    • hasAvailable

      private boolean hasAvailable()
    • available

      private long available()
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel
      Specified by:
      close in interface Closeable
      Overrides:
      close in class BaseSeekableSource
      Throws:
      IOException
    • requireOpen

      public void requireOpen() throws IOException
      Specified by:
      requireOpen in interface SeekableSource
      Overrides:
      requireOpen in class BaseSeekableSource
      Throws:
      IOException
    • view

      public SeekableSource view(long startingPosition, long length)
      Cannot create a view of a view. This method throws RuntimeException.
      Returns:
      a readable view of a portion of this SeekableSource. Reading from the view doesn't affect the SeekableSource position. Closing the SeekableSource makes all the views unreadable but closing the view has no effect on the SeekableSource. A view may or may not work on a thread bound copy of the SeekableSource so as a general rule it should not be created and handed to other threads.
      Throws:
      RuntimeException