Filter Streams   «Prev  Next»

Lesson 7PushbackInputStream
ObjectiveExplore the Use of PushbackInputStream to unread the Last Byte Read

Use of PushbackInputStream to unread the Last Byte Read

The java.io.PushbackInputStream class provides a pushback buffer so a program can unread the last byte read. The fundamental method of the PushbackInputStream class is unread(). The PushbackInputStream class has a one-byte pushback buffer so a program can "unread" the last character read. The next time data is read from the stream, the unread character is reread.
A PushbackInputStream adds functionality to another input stream, namely the ability to "push back" or "unread" one byte. The java.io.PushbackInputStream class provides a pushback buffer so a program can "unread" the last several bytes read. The next time data is read from the stream, the unread bytes are reread.
By default the buffer is only one byte long, and trying to unread more than one byte throws an IOException. However, you can change the default buffer size with the second constructor
public PushbackInputStream(InputStream in)
public PushbackInputStream(InputStream in, int size)

Although both PushbackInputStream and BufferedInputStream use buffers, only a PushbackInputStream allows unreading, and only a BufferedInputStream allows
  1. marking and
  2. resetting.

In a PushbackInputStream , markSupported() returns false.
public boolean markSupported()

The read() and available() methods work exactly as with normal input streams. However, they first attempt to read from the pushback buffer.
public int read() throws IOException
public int read(byte[] data, int offset, int length) throws IOException
public int available() throws IOException

Java Stream unread() method from the Oracle Java SE 7 Documentation

  1. public void unread(int b) throws IOException: Pushes back a byte by copying it to the front of the pushback buffer.
  2. public void unread(byte b[], int offset, int length) throws IOException: Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.
  3. public void unread(byte b[]) throws IOException: Pushes back an array of bytes by copying it to the front of the pushback buffer.

public class PushbackInputStream 
extends FilterInputStream


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. The next time data is read from the stream, the unread bytes are reread.
By default the buffer is only one byte long, and trying to unread more than that throws an IOException. However you can change the default buffer size with the second constructor below:

public PushbackInputStream(InputStream inputStream)
 
public PushbackInputStream(InputStream in, int numBytes)

Beyond the familiar methods of InputStream, PushbackInputStream provides unread( ), shown here:
void unread(int b)
void unread(byte buffer [ ])
void unread(byte buffer, int offset, int numBytes)

The first form pushes back the low-order byte of b. This will be the next byte returned by a subsequent call to read( ). The second form pushes back the bytes in buffer. The third form pushes back numBytes bytes beginning at offset from buffer. An IOException will be thrown if there is an attempt to push back a byte when the pushback buffer is full.
Although both PushbackInputStream and BufferedInputStream use buffers, only a PushbackInputStream allows unreading and only a BufferedInputStream allows marking and resetting. In a PushbackInputStream, the markSupported() method returns false and the reset() method throws an IOException.
The read() and available() methods work exactly as with normal input streams. However, they first attempt to read from the pushback buffer.

Filter Streams - Quiz

Click the Quiz link below to take a brief multiple-choice quiz on filter streams.
Filter Streams - Quiz