Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java PushbackInputStream Class

Java PushbackInputStream class overrides InputStream and provides extra functionality to another input stream. It can unread a byte which is already read and push back one byte.

Class declaration

Let's see the declaration for class:

  1. public class PushbackInputStream extends FilterInputStream  

Class Methods

It is used to test if the input stream support mark and reset method.



int available()

It is used to return the number of bytes that can be read from the input stream.

int read()

It is used to read the next byte of data from the input stream.

boolean markSupported()


void mark(int readlimit)

It is used to mark the current position in the input stream.

long skip(long x)

It is used to skip over and discard x bytes of data.

void unread(int b)

It is used to pushes back the byte by copying it to the pushback buffer.

void unread(byte[] b)

It is used to pushes back the array of byte by copying it to the pushback buffer.

void reset()

It is used to reset the input stream.

void close()

It is used to close the input stream.

Example of PushbackInputStream class

  1. import*;  
  2. public class InputStreamExample {  
  3. public static void main(String[] args)throws Exception{  
  4.           String srg = "1##2#34###12";  
  5.           byte ary[] = srg.getBytes();  
  6.           ByteArrayInputStream array = new ByteArrayInputStream(ary);  
  7.           PushbackInputStream push = new PushbackInputStream(array);  
  8.           int i;        
  9.               while( (i =!= -1) {  
  10.                   if(i == '#') {  
  11.                       int j;  
  12.                       if( (j = == '#'){  
  13.                            System.out.print("**");  
  14.                       }else {  
  15.                           push.unread(j);  
  16.                           System.out.print((char)i);  
  17.                       }  
  18.                   }else {  
  19.                               System.out.print((char)i);  
  20.                   }  
  21.              }        
  22.   }   
  23. }  


  1. 1**2#34**#12