Class: java.io.PushbackInputStream
- public class PushbackInputStream
- extends FilterInputStream
A
PushbackInputStream adds
functionality to another input stream, namely
the ability to "push back" or "unread"
one byte. 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.Inheritance
Superclass tree:- java.lang.Object
- java.io.InputStream
- java.io.FilterInputStream
- java.io.PushbackInputStream
Methods
-
PushbackInputStreamtop
public PushbackInputStream(InputStream in)Creates aPushbackInputStreamand saves its argument, the input streamin, for later use. Initially, there is no pushed-back byte (the fieldpushBackis initialized to-1). -
PushbackInputStreamtop
public PushbackInputStream(InputStream in, int size)Creates aPushbackInputStreamwith a pushback buffer of the specifiedsize, and saves its argument, the input streamin, for later use. Initially, there is no pushed-back byte (the fieldpushBackis initialized to-1). -
availabletop
public int available() throws IOExceptionReturns 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 available.
- Override hierarchy:
- available from FilterInputStream
- available from InputStream
-
closetop
public synchronized void close() throws IOExceptionCloses 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.- Override hierarchy:
- close from FilterInputStream
- close from InputStream
-
marktop
public synchronized void mark(int readlimit)Marks the current position in this input stream.The
markmethod ofPushbackInputStreamdoes nothing.- Override hierarchy:
- mark from FilterInputStream
- mark from InputStream
-
markSupportedtop
public boolean markSupported()Tests if this input stream supports themarkandresetmethods, which it does not.- Override hierarchy:
- markSupported from FilterInputStream
- markSupported from InputStream
-
readtop
public int read() throws IOExceptionReads the next byte of data from this input stream. The value byte is returned as anintin the range0to255. If no byte is available because the end of the stream has been reached, the value-1is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.This method returns the most recently pushed-back byte, if there is one, and otherwise calls the
readmethod of its underlying input stream and returns whatever value that method returns.- Override hierarchy:
- read from FilterInputStream
- read from InputStream
-
readtop
public int read(byte[] b, int off, int len) throws IOExceptionReads up tolenbytes of data from this input stream into an array of bytes. This method first reads any pushed-back bytes; after that, if fewer thanlenbytes have been read then it reads from the underlying input stream. Iflenis not zero, the method blocks until at least 1 byte of input is available; otherwise, no bytes are read and0is returned.- Override hierarchy:
- read from FilterInputStream
- read from InputStream
-
resettop
public synchronized void reset() throws IOExceptionRepositions this stream to the position at the time themarkmethod was last called on this input stream.The method
resetfor classPushbackInputStreamdoes nothing except throw anIOException.- Override hierarchy:
- reset from FilterInputStream
- reset from InputStream
-
skiptop
public long skip(long n) throws IOExceptionSkips over and discardsnbytes of data from this input stream. Theskipmethod may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly zero. Ifnis negative, no bytes are skipped.The
skipmethod ofPushbackInputStreamfirst skips over the bytes in the pushback buffer, if any. It then calls theskipmethod of the underlying input stream if more bytes need to be skipped. The actual number of bytes skipped is returned.- Override hierarchy:
- skip from FilterInputStream
- skip from InputStream
-
unreadtop
public void unread(int b) throws IOExceptionPushes 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(byte)b. -
unreadtop
public void unread(byte[] b) throws IOExceptionPushes 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 valueb[0], the byte after that will have the valueb[1], and so forth. -
unreadtop
public void unread(byte[] b, int off, int len) throws IOExceptionPushes 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 valueb[off], the byte after that will have the valueb[off+1], and so forth.
