Java-PushbackReader

PushbackReader

The PushbackReader can be used to push a single or multiple characters(that was previously read) back into the input stream so that it can be reread. This is commonly used with parsers. When a character indicating a new input token is read, it is pushed back into the input stream until the current input token is processed. It is then reread when processing of the next input token is initiated.

PushbackReader provides unread( ) method, which pushes back a single character or more characters by copying it to the front of the pushback buffer. Closing a PushbackReader also closes the specified underlying stream. An IOException will be thrown if there is an attempt to return a character when the pushback buffer is full.

PushbackReader Constructors

Constructor Description
PushbackReader(Reader in) Creates a pushback reader with a one-character pushback buffer.
PushbackReader(Reader in, int bsize) Creates a pushback reader that allows multiple character to be pushed back. The size of the pushback buffer is passed in bsize.

PushbackReader Methods

Method Description
int  read() Reads a single character underlying input stream.
int  read(char[ ] cb) Reads up to cb.length characters from the underlying input stream into an array of characters.
int  read(char[ ] cb, int off, int len) Reads up to len characters from the underlying input stream into an array of characters.
int  read(CharBuffer cb) Attempts to read characters into the specified character buffer.
void  unread(int c) Pushes back a single character by copying it to the front of the pushback buffer.
void  unread(char[] cbuf) Pushes back an array of characters by copying it to the front of the pushback buffer.
void  unread(char[] cbuf, int off, int len) Pushes back a portion of an array of characters by copying it to the front of the pushback buffer.
boolean  ready() Tells whether this stream is ready to be read.
boolean  markSupported() Tells whether this stream supports the mark() operation, which it does.
void  mark(int readAheadLimit) Sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.
void  reset() Resets the stream to the most recent mark, or to the beginning if it has never been marked.
long  skip(long n) Skips over n characters from this stream.
void  close() Closes the stream and releases any system resources associated with it.

Program

final1

Program Source

import java.io.CharArrayReader;
import java.io.PushbackReader;
import java.io.IOException;

public class Javaapp {
    
    public static void main(String[] args) throws IOException {
        
        String str = "XDXXRXGGFXXU";
        try(CharArrayReader chr = new CharArrayReader(str.toCharArray());
            PushbackReader pr = new PushbackReader(chr)){
            
            int getc = 0;
            while((getc=pr.read())!=-1)
            {
                if(getc=='X')
                {
                    if((getc=pr.read())=='X')
                    {
                        System.out.print("(XX)");
                    }else{
                        System.out.print("(X)");
                        pr.unread(getc);
                    }
                }else
                    System.out.print((char)getc);
            }
        }
    }
}

Leave a Comment