public class RereadableInputStream
extends java.io.InputStream
Modifier and Type | Field and Description |
---|---|
private boolean |
bufferIsInFile
Whether or not the stream's contents are being stored in a file
as opposed to memory.
|
private byte[] |
byteBuffer
The buffer used to store the stream's content; this storage is moved
to a file when the stored data's size exceeds maxBytesInMemory.
|
private boolean |
closeOriginalStreamOnClose
Specifies whether or not to close the original input stream
when close() is called.
|
private boolean |
firstPass
True when the original stream is being read; set to false when
reading is set to use the stored data instead.
|
private java.io.InputStream |
inputStream
The inputStream currently being used by this object to read contents;
may be the original stream passed in, or a stream that reads
the saved copy.
|
private int |
maxBytesInMemory
Maximum number of bytes that can be stored in memory before
storage will be moved to a temporary file.
|
private java.io.InputStream |
originalInputStream
Input stream originally passed to the constructor.
|
private boolean |
readToEndOfStreamOnFirstRewind
Specifies whether or not to read to the end of stream on first
rewind.
|
private int |
size
The total number of bytes read from the original stream at the time.
|
private java.io.File |
storeFile
File used to store the stream's contents; is null until the stored
content's size exceeds maxBytesInMemory.
|
private java.io.OutputStream |
storeOutputStream
OutputStream used to save the content of the input stream in a
temporary file.
|
Constructor and Description |
---|
RereadableInputStream(java.io.InputStream inputStream,
int maxBytesInMemory,
boolean readToEndOfStreamOnFirstRewind,
boolean closeOriginalStreamOnClose)
Creates a rereadable input stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the input stream and removes the temporary file if one was
created.
|
private void |
closeStream()
Closes the input stream currently used for reading (may either be
the original stream or a memory or file stream after the first pass).
|
int |
getSize()
Returns the number of bytes read from the original stream.
|
int |
read()
Reads a byte from the stream, saving it in the store if it is being
read from the original stream.
|
void |
rewind()
"Rewinds" the stream to the beginning for rereading.
|
private void |
saveByte(int inputByte)
Saves the byte read from the original stream to the store.
|
private java.io.InputStream originalInputStream
private java.io.InputStream inputStream
private int maxBytesInMemory
private boolean firstPass
private boolean bufferIsInFile
private byte[] byteBuffer
private int size
private java.io.File storeFile
private java.io.OutputStream storeOutputStream
private boolean readToEndOfStreamOnFirstRewind
private boolean closeOriginalStreamOnClose
public RereadableInputStream(java.io.InputStream inputStream, int maxBytesInMemory, boolean readToEndOfStreamOnFirstRewind, boolean closeOriginalStreamOnClose)
inputStream
- stream containing the source of datamaxBytesInMemory
- maximum number of bytes to use to store
the stream's contents in memory before switching to disk; note that
the instance will preallocate a byte array whose size is
maxBytesInMemory. This byte array will be made available for
garbage collection (i.e. its reference set to null) when the
content size exceeds the array's size, when close() is called, or
when there are no more references to the instance.readToEndOfStreamOnFirstRewind
- Specifies whether or not to
read to the end of stream on first rewind. If this is set to false,
then when rewind() is first called, only those bytes already read
from the original stream will be available from then on.public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public void rewind() throws java.io.IOException
java.io.IOException
private void closeStream() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public int getSize()
private void saveByte(int inputByte) throws java.io.IOException
inputByte
- byte read from original streamjava.io.IOException