public class ParserInputStream extends InputStream
As such, a ParserInputStream can be used whenever the input data needs to be re-read. For example, it is used for pre-parsing a file to determine the nature of its content, before passing it to the appropriate parser for actual parsing.
It can operate in buffering or non-buffering mode. When in buffering mode, it buffers the input, extending the size of the buffer as required.
The ParserInputStream can be reset to allow re-reading of the input data. When reset and put in non-buffering mode, it consumes the bytes in the buffer to service read requests, until it reaches the end of the buffered data, at which point it will start reading from the underlying InputStream. When reset and operating in buffering mode, it consumes the bytes in the buffer to service read requests, until it reaches the end of the buffered data, at which point it will read more data into the buffer and services read requests from the contents of the buffer.
A ParserInputStream can only be reset if already in buffering mode.
Constructor and Description |
---|
ParserInputStream(InputStream stream)
Constructs a ParserInputStream from an InputStream.
|
Modifier and Type | Method and Description |
---|---|
boolean |
getBufferingMode()
Gets the buffering mode indicating whether the stream is currently in
buffering mode or non-buffering mode.
|
int |
read()
Reads the next byte of data from this input stream.
|
int |
read(byte[] b)
Reads up to b.length bytes of data from this input stream into an
array of bytes.
|
int |
read(byte[] b,
int offset,
int len)
Reads up to len bytes of data from this input stream into an array of
bytes at the position specified by offset.
|
void |
resetStream(boolean bufferingMode)
Resets the stream such that it can be re-read.
|
void |
setBufferingMode(boolean mode)
Set the stream into the desired mode.
|
available, close, mark, markSupported, reset, skip
public ParserInputStream(InputStream stream)
stream
- the InputStreampublic int read() throws IOException
The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
read
in class InputStream
IOException
- if an I/O error occurspublic int read(byte[] b) throws IOException
read
in class InputStream
b
- the buffer into which the data is readIOException
- if an I/O error occurspublic int read(byte[] b, int offset, int len) throws IOException
read
in class InputStream
b
- the buffer into which the data is readoffset
- the start offset of the datalen
- the maximum number of bytes readIOException
- if an I/O error occurspublic void setBufferingMode(boolean mode)
If true is passed in, the stream is put into buffering mode, so that it can be reset subsequently to re-read the stream. If false is passed in, the stream is put into non-buffering mode, and it cannot be subsequently reset.
mode
- can be true or false, indicating whether the stream should
be put into buffering mode or non-buffering mode
modepublic boolean getBufferingMode()
public void resetStream(boolean bufferingMode) throws IfsException
The stream can continue to operate in buffering mode after the reset, or it can be set to non-buffering mode following reset. The buffering mode should be set according to whether the stream will need to be re-read. A stream that is not in buffering mode cannot be reset.
bufferingMode
- determines whether the stream should continue
in buffering mode after being resetIfsException
- if attempting to reset a stream that is not in
buffering modeCopyright © 2023. All rights reserved.