Package org.biojava.utils.io
Class CachingInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.biojava.utils.io.CachingInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Seekable
public class CachingInputStream extends InputStream implements Seekable
A wrapper aroundInputStreamthat provides in-memory caching of the input data. This allows it to provide aseek(long)method, which lets the user use anInputStreamlike aRandomAccessFile(with appropriate caveats about memory footprint, security, and performance).This class has not been tested with very long input streams. It might choke.
- Author:
- Rhett Sutphin (UI CBCB)
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]cacheThe byte cache itself.protected InputStreaminThe underlying input stream whose data we're cachingprotected intptrThe 0-based index into cache of the _next_ byte to return.protected intvalidLenA count of the number of bytes incachethat contain data read from the stream.
-
Constructor Summary
Constructors Constructor Description CachingInputStream(InputStream in)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidexpandCache(int additionalBytes)Expands the cache to hold some number ofadditionalBytes.intread()intread(byte[] b, int start, int len)voidseek(long pos)Moves the pointer in the inputstream such that the byte starting atposare returned by the next read.longskip(long num)-
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, transferTo
-
-
-
-
Field Detail
-
cache
protected byte[] cache
The byte cache itself.
-
ptr
protected int ptr
The 0-based index into cache of the _next_ byte to return. If ptr == validLen, data must be read from the stream into the cache.
-
validLen
protected int validLen
A count of the number of bytes incachethat contain data read from the stream.
-
in
protected InputStream in
The underlying input stream whose data we're caching
-
-
Constructor Detail
-
CachingInputStream
public CachingInputStream(InputStream in)
-
-
Method Detail
-
seek
public void seek(long pos) throws IOException
Description copied from interface:SeekableMoves the pointer in the inputstream such that the byte starting atposare returned by the next read.- Specified by:
seekin interfaceSeekable- Parameters:
pos- the position to which to seek- Throws:
IOException- when there's an I/O problem
-
read
public int read() throws IOException
- Specified by:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b, int start, int len) throws IOException
- Overrides:
readin classInputStream- Throws:
IOException
-
skip
public long skip(long num) throws IOException
- Overrides:
skipin classInputStream- Throws:
IOException
-
expandCache
protected void expandCache(int additionalBytes)
Expands the cache to hold some number ofadditionalBytes. 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.
-
-