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

Interface SeekableSource

All Superinterfaces:
AutoCloseable, Channel, Closeable, ReadableByteChannel
All Known Subinterfaces:
OffsettableSeekableSource
All Known Implementing Classes:
BaseSeekableSource, BufferedSeekableSource, ByteArraySeekableSource, FileChannelSeekableSource, MemoryMappedSeekableSource

public interface SeekableSource extends ReadableByteChannel
Readable source that provides random access capabilities.
Author:
Andrea Vacondio
  • Method Details

    • id

      String id()
      Returns:
      the unique id for the source.
    • position

      long position() throws IOException
      Returns:
      the current source position as a positive long
      Throws:
      IOException
    • position

      SeekableSource position(long position) throws IOException
      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:
      position - a non-negative long for the new position
      Returns:
      this source
      Throws:
      IOException
    • size

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

      int read() throws IOException
      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
    • view

      SeekableSource view(long startingPosition, long length) throws IOException
      Parameters:
      startingPosition -
      length -
      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:
      IOException - if something goes wrong while creating the view
    • back

      default SeekableSource back(long offset) throws IOException
      Skips backward the given number of bytes moving back the source position
      Parameters:
      offset - the number of bytes to skip back.
      Returns:
      this source
      Throws:
      IOException
    • back

      default SeekableSource back() throws IOException
      Skips backward moving back the source position of one byte
      Returns:
      this source
      Throws:
      IOException
      See Also:
    • forward

      default SeekableSource forward(long offset) throws IOException
      Skips the given number of bytes moving forward the source position
      Parameters:
      offset - the number of bytes to skip .
      Returns:
      this source
      Throws:
      IOException
    • peek

      default int peek() throws IOException
      Reads the next byte and sets the position back by one.
      Returns:
      the next byte or -1 if there is no more data.
      Throws:
      IOException
      See Also:
    • peekBack

      default int peekBack() throws IOException
      Reads the previous byte and sets the position back where it was.
      Returns:
      the previous byte or -1 if we are at the beginning of the source.
      Throws:
      IOException
      See Also:
    • asInputStream

      default InputStream asInputStream()
      Creates an InputStream from this SeekableSource.
      Returns:
      the input stream wrapping the given SeekableSource
    • reset

      default void reset()
      Resets stream back to beginning
    • requireOpen

      void requireOpen() throws IOException
      Throws:
      IllegalStateException - if the source is closed
      IOException
    • asNewInputStream

      default InputStream asNewInputStream()
      Creates an InputStream from this SeekableSource resetting the stream to the beginning.
      Returns:
      the input stream wrapping the given SeekableSource