Package org.biojava.utils.io
Class CachingInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.biojava.utils.io.CachingInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,Seekable
public class CachingInputStream extends java.io.InputStream implements Seekable
A wrapper aroundInputStream
that provides in-memory caching of the input data. This allows it to provide aseek(long)
method, which lets the user use anInputStream
like 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[]
cache
The byte cache itself.protected java.io.InputStream
in
The underlying input stream whose data we're cachingprotected int
ptr
The 0-based index into cache of the _next_ byte to return.protected int
validLen
A count of the number of bytes incache
that contain data read from the stream.
-
Constructor Summary
Constructors Constructor Description CachingInputStream(java.io.InputStream in)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
expandCache(int additionalBytes)
Expands the cache to hold some number ofadditionalBytes
.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 atpos
are returned by the next read.long
skip(long num)
-
-
-
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 incache
that contain data read from the stream.
-
in
protected java.io.InputStream in
The underlying input stream whose data we're caching
-
-
Method Detail
-
seek
public void seek(long pos) throws java.io.IOException
Description copied from interface:Seekable
Moves the pointer in the inputstream such that the byte starting atpos
are returned by the next read.
-
read
public int read() throws java.io.IOException
- Specified by:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int start, int len) throws java.io.IOException
- Overrides:
read
in classjava.io.InputStream
- Throws:
java.io.IOException
-
skip
public long skip(long num) throws java.io.IOException
- Overrides:
skip
in classjava.io.InputStream
- Throws:
java.io.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.
-
-