public class RewindableStream extends RandomStream
Such a Stream can be used when you need to decode data from an Input stream, but some pre-processing of some number of starting bytes is needed - for example in image decoding.
As data is read from the stream it is copied into a buffer as well as being returned for consumption. A rewind() operation then causes the stream to seek to the start of the stream, and you can subsequently seek() up to that point.
You would not create a RewindableStream directly - you would instead use the toRewindableStream() method instead to return a Stream that acts as a RandomStream.
You should not use a RewindableStream as a general purpose RandomStream due to the fact it can only seek within the rewind buffer.
READ_ONLY, READ_WRITE| Modifier and Type | Method and Description |
|---|---|
void |
close()
This method closes the stream.
|
long |
getPosition()
Retrieve the file position.
|
boolean |
isOpenForWriting()
Returns true if this Stream was open in Read-Write mode as opposed to Read-Only mode.
|
int |
read()
This method reads an unsigned byte from the input stream and returns it
as an int in the range of 0-255.
|
int |
read(byte[] buffer,
int start,
int length)
This method read bytes from a stream and stores them into a
caller supplied buffer.
|
static void |
rewind(RandomStream stream)
Rewind a Stream created by toRewindableStream().
|
void |
setPosition(long pos)
Set the Stream position.
|
long |
skip(long num)
This method skips the specified number of bytes in the stream.
|
static RandomStream |
toRewindableStream(InputStream stream)
Return a Stream that can act as at least a limited RandomStream by being
able to rewind after reading a number of bytes.
|
void |
write(byte[] buff,
int start,
int length)
This method writes
len bytes from the specified array
b starting at index off into the array. |
void |
write(int b)
This method writes a single byte to the output stream.
|
canSetLength, flush, getLength, isReadWrite, mark, markSupported, reset, setLength, setMode, throwCantWrite, toOutputStream, validateMode, writeavailable, finalize, hashCode, readpublic static RandomStream toRewindableStream(InputStream stream)
stream - the InputStream to be made rewindable.public static void rewind(RandomStream stream) throws IOException
stream - the RandomStream returned by toRewindableStream().IOException - if an IO error occurs.public boolean isOpenForWriting()
RandomStreamisOpenForWriting in class RandomStreampublic void close()
throws IOException
InputStreamIOException
This method does nothing in this class, but subclasses may override this method in order to provide additional functionality.
close in class InputStreamIOException - If an error occurs, which can only happen
in a subclasspublic int read()
throws IOException
InputStreamThis method will block until the byte can be read.
read in class InputStreamIOException - If an error occurspublic int read(byte[] buffer,
int start,
int length)
throws IOException
InputStreamoff into the buffer and attempts to read
len bytes. This method can return before reading the
number of bytes requested. The actual number of bytes read is
returned as an int. A -1 is returned to indicate the end of the
stream.
This method will block until some data can be read.
This method operates by calling the single byte read() method
in a loop until the desired number of bytes are read. The read loop
stops short if the end of the stream is encountered or if an IOException
is encountered on any read operation except the first. If the first
attempt to read a bytes fails, the IOException is allowed to propagate
upward. And subsequent IOException is caught and treated identically
to an end of stream condition. Subclasses can (and should if possible)
override this method to provide a more efficient implementation.
read in class InputStreambuffer - The array into which the bytes read should be storedstart - The offset into the array to start storing byteslength - The requested number of bytes to readIOException - If an error occurs.public long skip(long num)
throws IOException
InputStreamThis method reads and discards bytes into a byte array until the specified number of bytes were skipped or until either the end of stream is reached or a read attempt returns a short count. Subclasses can override this metho to provide a more efficient implementation where one exists.
skip in class RandomStreamnum - The requested number of bytes to skipIOException - If an error occurspublic void setPosition(long pos)
throws IOException
RandomStreamsetPosition in class RandomStreampos - the new position to set.IOException - on error.public long getPosition()
throws IOException
getPosition in class RandomStreamIOException - if an error occured while getting the position.public void write(int b)
throws IOException
RandomStreamint passed as the argument.
Subclasses must provide an implementation of this method.
write in class RandomStreamb - The byte to be written to the output stream, passed as
the low eight bits of an intIOException - If an error occurspublic void write(byte[] buff,
int start,
int length)
throws IOException
RandomStreamlen bytes from the specified array
b starting at index off into the array.
This method in this class calls the single byte write()
method in a loop until all bytes have been written. Subclasses should
override this method if possible in order to provide a more efficent
implementation.
write in class RandomStreambuff - The array of bytes to write fromstart - The index into the array to start writing fromlength - The number of bytes to writeIOException - If an error occurs