PushbackInputStream adds
functionality to another input stream, namely
the ability to "push back" or "unread" bytes,
by storing pushed-back bytes in an internal buffer.
This is useful in situations where
it is convenient for a fragment of code
to read an indefinite number of data bytes
that are delimited by a particular byte
value; after reading the terminating byte,
the code fragment can "unread" it, so that
the next read operation on the input stream
will reread the byte that was pushed back.
For example, bytes representing the characters
constituting an identifier might be terminated
by a byte representing an operator character;
a method whose job is to read just an identifier
can read until it sees the operator and
then push the operator back to be re-read.
| Modifier and Type | Field and Description |
|---|---|
| protected byte[] | buf
The pushback buffer. |
| protected int | pos
The position within the pushback buffer from which the next byte will be read. |
| Access | Constructor and Description |
|---|---|
| public | PushbackInputStream(InputStream
the input stream from which bytes will be read. in, int the size of the pushback buffer. size)Creates a |
| public | PushbackInputStream(InputStream
the input stream from which bytes will be read. in)Creates a |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the number of bytes that can be read (or skipped over) from the input stream without blocking.Overrides java. Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. |
| public synchronized void | close()
Overrides java. Implements java. Closes this input stream and releases any system resources associated with the stream. |
| private void | |
| public void | mark(int
the maximum limit of bytes that can be read before
the mark position becomes invalid. readlimit)Overrides java. Marks the current position in this input stream. |
| public boolean | Returns: false, since this class does not support the
mark and reset methods.Overrides java. Tests if this input stream supports the |
| public int | Returns: the next byte of data, or-1 if the end of the
stream has been reached.Overrides java. Reads the next byte of data from this input stream. |
| public int | Returns: the total number of bytes read into the buffer, or-1 if there is no more data because the end of
the stream has been reached.the buffer into which the data is read. b, int the start offset in the destination array off, int bthe maximum number of bytes read. len)Overrides java. Reads up to |
| public void | reset()
Overrides java. Repositions this stream to the position at the time the
|
| public long | skip(long n)
Overrides java. Skips over and discards |
| public long | transferTo(OutputStream out)
Overrides java. Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read. |
| public void | unread(int
the b)int value whose low-order
byte is to be pushed back.Pushes back a byte by copying it to the front of the pushback buffer. |
| public void | unread(byte[]
the byte array to push back. b, int the start offset of the data. off, int the number of bytes to push back. len)Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. |
| public void | unread(byte[]
the byte array to push back b)Pushes back an array of bytes by copying it to the front of the pushback buffer. |
| buf | back to summary |
|---|---|
| protected byte[] buf The pushback buffer.
| |
| pos | back to summary |
|---|---|
| protected int pos The position within the pushback buffer from which the next byte will
be read. When the buffer is empty,
| |
| PushbackInputStream | back to summary |
|---|---|
| public PushbackInputStream(InputStream in, int size) Creates a
| |
| PushbackInputStream | back to summary |
|---|---|
| public PushbackInputStream(InputStream in) Creates a
| |
| available | back to summary |
|---|---|
| public int available() throws IOException Overrides java. Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes. The method returns the sum of the number of bytes that have been
pushed back and the value returned by
| |
| close | back to summary |
|---|---|
| public synchronized void close() throws IOException Overrides java. Implements java. Closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), unread(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
| |
| ensureOpen | back to summary |
|---|---|
| private void ensureOpen() throws IOException Check to make sure that this stream has not been closed | |
| mark | back to summary |
|---|---|
| public void mark(int readlimit) Overrides java. Marks the current position in this input stream. The
| |
| markSupported | back to summary |
|---|---|
| public boolean markSupported() Overrides java. Tests if this input stream supports the
| |
| read | back to summary |
|---|---|
| public int read() throws IOException Overrides java. Reads the next byte of data from this input stream. The value
byte is returned as an This method returns the most recently pushed-back byte, if there is
one, and otherwise calls the
| |
| read | back to summary |
|---|---|
| public int read(byte[] b, int off, int len) throws IOException Overrides java. Reads up to
| |
| reset | back to summary |
|---|---|
| public void reset() throws IOException Overrides java. Repositions this stream to the position at the time the
The method
| |
| skip | back to summary |
|---|---|
| public long skip(long n) throws IOException Overrides java. Skips over and discards The
| |
| transferTo | back to summary |
|---|---|
| public long transferTo(OutputStream out) throws IOException Overrides java. Doc from java. Reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read. On return, this input stream will be at end of stream. This method does not close either stream. This method may block indefinitely reading from the input stream, or writing to the output stream. The behavior for the case where the input and/or output stream is asynchronously closed, or the thread interrupted during the transfer, is highly input and output stream specific, and therefore not specified.
If the total number of bytes transferred is greater than Long#MAX_VALUE, then If an I/O error occurs reading from the input stream or writing to the output stream, then it may do so after some bytes have been read or written. Consequently the input stream may not be at end of stream and one, or both, streams may be in an inconsistent state. It is strongly recommended that both streams be promptly closed if an I/O error occurs.
| |
| unread | back to summary |
|---|---|
| public void unread(int b) throws IOException Pushes back a byte by copying it to the front of the pushback buffer.
After this method returns, the next byte to be read will have the value
| |
| unread | back to summary |
|---|---|
| public void unread(byte[] b, int off, int len) throws IOException Pushes back a portion of an array of bytes by copying it to the front
of the pushback buffer. After this method returns, the next byte to be
read will have the value
| |
| unread | back to summary |
|---|---|
| public void unread(byte[] b) throws IOException Pushes back an array of bytes by copying it to the front of the
pushback buffer. After this method returns, the next byte to be read
will have the value
| |