Java IO Tutorial - Java PushbackInputStream (InputStream in, int size) Constructor








Syntax

PushbackInputStream(InputStream in, int size) constructor from PushbackInputStream has the following syntax.

public PushbackInputStream(InputStream in,     int size)

Example

In the following code shows how to use PushbackInputStream.PushbackInputStream(InputStream in, int size) constructor.

/*w  w w .j  a va 2s . com*/
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class Main {
  public static void main(String args[]) throws IOException {
    byte buf[] = "==  = ".getBytes();
    PushbackInputStream f = new PushbackInputStream(new ByteArrayInputStream(buf),100);
    int c;
    while ((c = f.read()) != -1) {
      switch (c) {
      case '=':
        c = f.read();
        if (c == '=')
          System.out.print(".eq.");
        else {
          System.out.print("=");
          f.unread(c);
        }
        break;
      default:
        System.out.print((char) c);
        break;
      }
    }
  }
}

The code above generates the following result.