org.eclipse.mat.hprof

## Class SeekableStream

- java.lang.Object
- - java.io.InputStream
  - - org.eclipse.mat.hprof.SeekableStream

All Implemented Interfaces:
  Closeable, AutoCloseable

    public class SeekableStream
    extends InputStream
    implements Closeable, 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.

### Nested Class Summary

Nested Classes
`static class` `SeekableStream.RandomAccessInputStream` Used to wrap and indicate a underlying stream which has a position and has seek().
`static class` `SeekableStream.UnclosableInputStream` Wraps an `InputStream` so that close does not propagate to the underlying stream.

### Constructor Summary

Constructors
Constructor and Description
`SeekableStream(Supplier<InputStream> genstream, int cachesize)` Creates a seekable stream out of a non-seekable stream.
`SeekableStream(Supplier<InputStream> genstream, SeekableByteChannel underlying, int cachesize, long estlen)` Creates a seekable stream out of a non-seekable stream.
`SeekableStream(Supplier<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
`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.
`String` `toString()`

### Methods inherited from class java.io.InputStream

`available, mark, markSupported, read, reset, skip`

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### SeekableStream

    public SeekableStream(Supplier<InputStream> genstream,
                          SeekableStream.RandomAccessInputStream underlying,
                          int cachesize,
                          long estlen)
                   throws 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 `UnclosableStream` 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:
  `IOException`

#### SeekableStream

    public SeekableStream(Supplier<InputStream> genstream,
                          SeekableByteChannel underlying,
                          int cachesize,
                          long estlen)
                   throws 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 `UnclosableStream` 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:
  `IOException`

#### SeekableStream

    public SeekableStream(Supplier<InputStream> genstream,
                          int cachesize)
                   throws 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:
  `IOException`

### Method Detail

#### seek

    public void seek(long pos)
              throws 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:
  `IOException`

#### position

    public long position()

#### read

    public int read()
             throws IOException

Specified by:
  `read` in class `InputStream`
Throws:
  `IOException`

#### read

    public int read(byte[] buffer,
                    int offset,
                    int length)
             throws IOException

Overrides:
  `read` in class `InputStream`
Throws:
  `IOException`

#### close

    public void close()
               throws 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 `Closeable`
Specified by:
  `close` in interface `AutoCloseable`
Overrides:
  `close` in class `InputStream`
Throws:
  `IOException`

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`