Filter Streams   «Prev 

PushbackInputStream

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 marking and resetting. In a PushbackInputStream , markSupported() returns false.
public boolean markSupported()


Java Stream unread() method

  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.