public class RandomAccessInputStream extends AbstractInputStreamWrapper
A RandomAccessInputStream
adds functionality to another input
stream-namely, the ability to buffer the input, allowing it to be read
multiple times, and to support the mark
and reset
methods.
When the RandomAccessInputStream
is created, an internal
Store is created. As bytes from the stream are read or skipped,
the internal store
is refilled as necessary from the source
input stream. The implementation of store
can be changed to fit
the application needs: cache on disk rather than in memory. The default
store
implementation caches 64K in memory and then write the
content on disk.
It also adds the functionality of marking an InputStream
without
specifying a mark length, thus allowing a reset
after an
indefinite length of bytes has been read. Check the #mark(int))
javadoc for details.
Internally it uses a RandomAccessFile
to cache and seek the data.
Since it must be able to random seek it can't be (easily) buffered
internally. External programs should wrap this class with a
BufferedInputStream
to improve performances (especially if
int read()
method is called).
Store
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_DISK_TRHESHOLD
Default size for passing from memory allocation to disk allocation for
the buffer.
|
protected long |
markLimit |
protected long |
markPosition
Position in the stream when the mark() was issued.
|
protected long |
randomAccessIsPosition
Position of read cursor in the RandomAccessInputStream.
|
protected long |
sourcePosition
Position of reading in the source stream.
|
closeCalled, source
Constructor and Description |
---|
RandomAccessInputStream(InputStream source)
Constructor for RandomAccessInputStream.
|
RandomAccessInputStream(InputStream source,
int threshold)
Creates a
RandomAccessInputStream with the specified
treshold, and saves its argument, the input stream source ,
for later use. |
RandomAccessInputStream(InputStream source,
SeekableStore store)
Constructor for RandomAccessInputStream.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
protected void |
closeOnce()
closeOnce
|
Store |
getStore()
Return the underlying store where the cache of data is kept.
|
protected int |
innerRead(byte[] b,
int off,
int len)
innerRead
|
void |
mark(int readLimit) |
boolean |
markSupported()
Overrides the
markSupported() method of the
InputStream class. |
void |
reset() |
void |
seek(long position)
Reposition the read pointer in the stream.
|
void |
setStore(Store store)
Setter for the field
store . |
String |
toString()
Provides a String representation of the state of the stream for debugging
purposes.
|
close, read, read, skip
read
public static final int DEFAULT_DISK_TRHESHOLD
protected long markLimit
protected long markPosition
protected long randomAccessIsPosition
protected long sourcePosition
public RandomAccessInputStream(InputStream source)
Constructor for RandomAccessInputStream.
source
- The underlying input stream.public RandomAccessInputStream(InputStream source, int threshold)
Creates a RandomAccessInputStream
with the specified
treshold, and saves its argument, the input stream source
,
for later use.
When data read under threshold size treshold
is kept into
memory. Over this size it is placed on disk.
source
- The underlying input stream.threshold
- Maximum number of bytes to keep into memory.ThresholdStore
public RandomAccessInputStream(InputStream source, SeekableStore store)
Constructor for RandomAccessInputStream.
source
- The underlying input stream.store
- a SeekableStore
object.public int available() throws IOException
available
in class InputStream
IOException
protected void closeOnce() throws IOException
closeOnce
closeOnce
in class AbstractInputStreamWrapper
IOException
- if any error occurs reading the bytes.public Store getStore()
protected int innerRead(byte[] b, int off, int len) throws IOException
innerRead
innerRead
in class AbstractInputStreamWrapper
b
- an array of byte.off
- a int.len
- a int.IOException
- if any error occurs reading the bytes.public void mark(int readLimit)
Marks the current position in this input stream. A subsequent call to the reset() method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.
This method extends the original behavior of the class
InputStream
allowing to use indefinite marking.
readLimit> 0
The readLimit
arguments
tells this input stream to allow that many bytes to be read before the
mark position gets invalidated.readLimit == 0
Invalidate the all the current marks and
clean up the temporary files.readLimit < 0
Set up an indefinite mark: reset can
be invoked regardless on how many bytes have been read.mark
in class InputStream
reset()
,
InputStream.reset()
public boolean markSupported()
markSupported()
method of the
InputStream
class.markSupported
in class InputStream
InputStream.markSupported();
public void reset() throws IOException
Repositions this stream to the position at the time the mark
method was last called on this input stream.
After invoking mark
it can be invoked multiple times and it
always reset the stream at the previously marked position.
reset
in class InputStream
IOException
- if this stream has not been marked or if the mark has been
invalidated.mark(int)
,
InputStream.reset()
public void seek(long position) throws IOException
Reposition the read pointer in the stream. Next read will start from the position passed as argument.
position
- a long indicating the position in the stream we want to go.IOException
- throw if any IOException occurs reading the underlying stream
or if it is attempted a seek to a position greater to the
effective number of bytes in the stream.public void setStore(Store store)
Setter for the field store
.
store
- a Store
object.Copyright © 2008–2016. All rights reserved.