Package org.biojava.utils.io
Class CachingInputStream
java.lang.Object
java.io.InputStream
org.biojava.utils.io.CachingInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Seekable
A wrapper around
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.
- Author:
- Rhett Sutphin (UI CBCB)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]
The byte cache itself.protected InputStream
The underlying input stream whose data we're cachingprotected int
The 0-based index into cache of the _next_ byte to return.protected int
A count of the number of bytes incache
that contain data read from the stream. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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) Methods inherited from class java.io.InputStream
available, close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo
-
Field Details
-
cache
The byte cache itself. -
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
A count of the number of bytes incache
that contain data read from the stream. -
in
The underlying input stream whose data we're caching
-
-
Constructor Details
-
CachingInputStream
-
-
Method Details
-
seek
Description copied from interface:Seekable
Moves the pointer in the inputstream such that the byte starting atpos
are returned by the next read.- Specified by:
seek
in interfaceSeekable
- Parameters:
pos
- the position to which to seek- Throws:
IOException
- when there's an I/O problem
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
expandCache
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.
-