public class PushbackInputStream extends FilterInputStream
FilterInputStream
provides the ability to
unread data from a stream. It maintains an internal buffer of unread
data that is supplied to the next read operation. This is conceptually
similar to mark/reset functionality, except that in this case the
position to reset the stream to does not need to be known in advance.
The default pushback buffer size one byte, but this can be overridden by the creator of the stream.
Constructor and Description |
---|
PushbackInputStream(InputStream in)
This method initializes a
PushbackInputStream to
read from the specified subordinate InputStream
with a default pushback buffer size of 1. |
PushbackInputStream(InputStream in,
int size)
This method initializes a
PushbackInputStream to
read from the specified subordinate InputStream with
the specified buffer size |
Modifier and Type | Method and Description |
---|---|
int |
available()
This method returns the number of bytes that can be read from this
stream before a read can block.
|
void |
close()
This method closes the stream and releases any associated resources.
|
boolean |
markSupported()
This method returns
false to indicate that it does
not support mark/reset functionality. |
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[] b,
int off,
int len)
This method read bytes from a stream and stores them into a
caller supplied buffer.
|
void |
reset()
This method always throws an IOException in this class because
mark/reset functionality is not supported.
|
long |
skip(long n)
This method skips the specified number of bytes in the stream.
|
void |
unread(byte[] b)
This method pushes all of the bytes in the passed byte array into
the pushback bfer.
|
void |
unread(byte[] b,
int off,
int len)
This method pushed back bytes from the passed in array into the
pushback buffer.
|
void |
unread(int b)
This method pushes a single byte of data into the pushback buffer.
|
mark, read
public PushbackInputStream(InputStream in)
PushbackInputStream
to
read from the specified subordinate InputStream
with a default pushback buffer size of 1.in
- The subordinate stream to read frompublic PushbackInputStream(InputStream in, int size)
PushbackInputStream
to
read from the specified subordinate InputStream
with
the specified buffer sizein
- The subordinate InputStream
to read fromsize
- The pushback buffer size to usepublic int available() throws IOException
This method will return the number of bytes available from the pushback buffer plus the number of bytes available from the underlying stream.
available
in class FilterInputStream
IOException
- If an error occurspublic void close() throws IOException
close
in interface Closeable
close
in class FilterInputStream
IOException
- If an error occurs.public boolean markSupported()
false
to indicate that it does
not support mark/reset functionality.markSupported
in class FilterInputStream
false
to indicate that
this class does not support mark/reset functionalitypublic int read() throws IOException
This method will block until the byte can be read.
read
in class FilterInputStream
IOException
- If an error occurspublic int read(byte[] b, int off, int len) throws IOException
offset
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 first reads bytes from the pushback buffer in order to satisfy the read request. If the pushback buffer cannot provide all of the bytes requested, the remaining bytes are read from the underlying stream.
read
in class FilterInputStream
b
- The array into which the bytes read should be storedoff
- The offset into the array to start storing byteslen
- The requested number of bytes to readIOException
- If an error occurs.public void reset() throws IOException
reset
in class FilterInputStream
IOException
- Always thrown for this classpublic long skip(long n) throws IOException
This method first discards bytes from the buffer, then calls the
skip
method on the underlying InputStream
to
skip additional bytes if necessary.
skip
in class FilterInputStream
n
- The requested number of bytes to skipIOException
- If an error occurspublic void unread(byte[] b) throws IOException
b[0]
followed by b[1]
, etc.
If the pushback buffer cannot hold all of the requested bytes, an exception is thrown.
b
- The byte array to be pushed backIOException
- If the pushback buffer is fullpublic void unread(byte[] b, int off, int len) throws IOException
b[offset]
to
b[offset + len]
are pushed in reverse order so that
the next byte read from the stream after this operation will be
b[offset]
followed by b[offset + 1]
,
etc.
If the pushback buffer cannot hold all of the requested bytes, an exception is thrown.
b
- The byte array to be pushed backoff
- The index into the array where the bytes to be push startlen
- The number of bytes to be pushed.IOException
- If the pushback buffer is fullpublic void unread(int b) throws IOException
If the pushback buffer is full, this method throws an exception.
The argument to this method is an int
. Only the low
eight bits of this value are pushed back.
b
- The byte to be pushed back, passed as an intIOException
- If the pushback buffer is full.