PushbackInputStream class

 

 See  InputStream class  for PushbackInputStream’s inherited methods.  They are:

 

available( )                   Tells how many bytes can be gotten without blocking.

close( )                         Close the stream.

mark( int readAheadLimit )  Mark the present position in the stream.

markSupported( )         Tell whether this stream supports the mark(...) operation.

read( )                          Read a single byte, returning it as the low end of an int.

read( byte[ ] b )             Read bytes into a byte array.

read( byte[ ] b, int off, int len)  Read bytes into a portion of a byte array.

ready( )                         Tell whether this stream is ready to be read.

reset( )                          Reset the stream to the beginning.

skip( long n )                Skip characters.

 

 

  PushbackInputStream adds three unread(...) methods to those of InputStream.

 

void  unread( byte[ ] )                                    Pushes back an array of bytes by copying it to the front of the pushback buffer.

void  unread( byte[ ] , int, len)                   Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer.

void  unread( int )                                         Pushes back a byte by copying it to the front of the pushback buffer.

 

 

  PushbackInputStream peeks at a character and puts it back if it’s not a left bracket.  i.e.

 

import java.io.*;

 

try {

PushbackInputStream pbin = new PushbackInputStream(new BufferedInputStream(new FileInputStream( "test.txt" )));

int b  = pbin.read( );                                             // read it

System.out.println( (char) b );

if ( b != '<')  pbin.unread( b );                              // put it back

pbin.close( );

} catch (Exception e) {   }