public class CachingInputStream extends InputStream implements Seekable
InputStream
that provides in-memory
caching of the input data. This allows it to provide a seek(long)
method, which lets the user use an InputStream
like a
RandomAccessFile
(with appropriate caveats about memory
footprint, security, and performance).
This class has not been tested with very long input streams. It might choke.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
cache
The byte cache itself.
|
protected InputStream |
in
The underlying input stream whose data we're caching
|
protected int |
ptr
The 0-based index into cache of the _next_ byte to return.
|
protected int |
validLen
A count of the number of bytes in
cache that contain
data read from the stream. |
Constructor and Description |
---|
CachingInputStream(InputStream in) |
Modifier and Type | Method and Description |
---|---|
protected void |
expandCache(int additionalBytes)
Expands the cache to hold some number of
additionalBytes . |
int |
read() |
int |
read(byte[] b,
int start,
int len) |
void |
seek(long pos)
Moves the pointer in the inputstream such that the byte starting at
pos are returned by the next read. |
long |
skip(long num) |
available, close, mark, markSupported, read, reset
protected byte[] cache
protected int ptr
protected int validLen
cache
that contain
data read from the stream.protected InputStream in
public CachingInputStream(InputStream in)
public void seek(long pos) throws IOException
Seekable
pos
are returned by the next read.seek
in interface Seekable
pos
- the position to which to seekIOException
- when there's an I/O problempublic int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int start, int len) throws IOException
read
in class InputStream
IOException
public long skip(long num) throws IOException
skip
in class InputStream
IOException
protected void expandCache(int additionalBytes)
additionalBytes
.
Expansion is done multiplicatively for efficiency. Immediately after
calling this method, you must fill the additional bytes from the stream
because this method also updates validLen.Copyright © 2014 BioJava. All rights reserved.