Class SeekableStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class SeekableStream
    extends java.io.InputStream
    implements java.lang.AutoCloseable
    Used to wrap an non-seekable stream to make it seekable. Constructed with a supplier of the non-seekable stream (which could be a decompression stream or decryption stream). Multiple instances of this stream are then constructed and each starts at the beginning. Optionally supplied underlying seekable SeekableStream.RandomAccessInputStream stream. This is used if the supplier does not provided a fresh underlying stream each time. A seek is used to move the underlying stream to the correct position when switching between unseekable streams. In this case the supplier of non-seekable streams should wrap the innermost stream from the underlying seekable stream with a SeekableStream.UnclosableInputStream so that the non-seekable streams can be closed (to release resources) without closing the underlying seekable.
    • Constructor Summary

      Constructors 
      Constructor Description
      SeekableStream​(java.util.function.Supplier<java.io.InputStream> genstream, int cachesize)
      Creates a seekable stream out of a non-seekable stream.
      SeekableStream​(java.util.function.Supplier<java.io.InputStream> genstream, java.nio.channels.SeekableByteChannel underlying, int cachesize, long estlen)
      Creates a seekable stream out of a non-seekable stream.
      SeekableStream​(java.util.function.Supplier<java.io.InputStream> genstream, SeekableStream.RandomAccessInputStream underlying, int cachesize, long estlen)
      Creates a seekable stream out of a non-seekable stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes and cleans up the SeekableStream, including the non-seekable streams.
      long position()  
      int read()  
      int read​(byte[] buffer, int offset, int length)  
      void seek​(long pos)
      Move to a position in the seekable stream.
      long skip​(long length)  
      java.lang.String toString()  
      • Methods inherited from class java.io.InputStream

        available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, transferTo
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • SeekableStream

        public SeekableStream​(java.util.function.Supplier<java.io.InputStream> genstream,
                              SeekableStream.RandomAccessInputStream underlying,
                              int cachesize,
                              long estlen)
                       throws java.io.IOException
        Creates a seekable stream out of a non-seekable stream.
        Parameters:
        genstream - Supplier of a new non-seekable stream for the same resource, underlying. The streams are closed when no longer needed. Wrap the innermost stream with SeekableStream.UnclosableInputStream to avoid closing the underlying stream when one of the non-seekable streams is closed by this class.
        underlying - The underlying seekable stream. Avoids having to open a file multiple times if a seek can be used instead.
        cachesize - number of seekable streams to use
        estlen - estimate of length
        Throws:
        java.io.IOException
      • SeekableStream

        public SeekableStream​(java.util.function.Supplier<java.io.InputStream> genstream,
                              java.nio.channels.SeekableByteChannel underlying,
                              int cachesize,
                              long estlen)
                       throws java.io.IOException
        Creates a seekable stream out of a non-seekable stream.
        Parameters:
        genstream - Supplier of a new non-seekable stream for the same resource. The streams are closed when no longer needed. Wrap the innermost stream with SeekableStream.UnclosableInputStream to avoid closing the underlying stream when one of the non-seekable streams is closed by this class.
        underlying - The underlying seekable stream. Avoids having to open a file multiple times if a seek can be used instead.
        cachesize - number of seekable streams to use
        estlen - estimate of uncompressed length
        Throws:
        java.io.IOException
      • SeekableStream

        public SeekableStream​(java.util.function.Supplier<java.io.InputStream> genstream,
                              int cachesize)
                       throws java.io.IOException
        Creates a seekable stream out of a non-seekable stream.
        Parameters:
        genstream - Supplier of a new non-seekable stream for the same resource. The streams are closed when no longer needed.
        cachesize - number of seekable streams to use
        Throws:
        java.io.IOException
    • Method Detail

      • seek

        public void seek​(long pos)
                  throws java.io.IOException
        Move to a position in the seekable stream. Locates the stream closest before the seek point. Skip to the seek point. If no underlying stream is before the seek point, create another and discard an existing stream if required.
        Parameters:
        pos -
        Throws:
        java.io.IOException
      • position

        public long position()
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] buffer,
                        int offset,
                        int length)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long length)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes and cleans up the SeekableStream, including the non-seekable streams. Does not close the underlying stream or channel, this should be done explicitly.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object