Package org.eclipse.mat.hprof
Class SeekableStream
- java.lang.Object
-
- java.io.InputStream
-
- org.eclipse.mat.hprof.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 seekableSeekableStream.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 aSeekableStream.UnclosableInputStream
so that the non-seekable streams can be closed (to release resources) without closing the underlying seekable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SeekableStream.RandomAccessInputStream
Used to wrap and indicate a underlying stream which has a position and has seek().static class
SeekableStream.UnclosableInputStream
Wraps anInputStream
so that close does not propagate to the underlying stream.
-
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 theSeekableStream
, 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()
-
-
-
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 withSeekableStream.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 useestlen
- 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 withSeekableStream.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 useestlen
- 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 classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] buffer, int offset, int length) throws java.io.IOException
- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
skip
public long skip(long length) throws java.io.IOException
- Overrides:
skip
in classjava.io.InputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Closes and cleans up theSeekableStream
, including the non-seekable streams. Does not close the underlying stream or channel, this should be done explicitly.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-