Class PushbackReader in Java

Class PushbackReader in Java

PushbackReader class is a character input stream that uses another input stream as its input source and adds the ability to push characters back onto the stream. This feature is often useful when writing parsers.

In other words, after data has been read from a PushbackReader, it can be pushed back into the same stream from where it was reading sp that it can be reread.

Ads code goes here

When we create a PushbackReader stream, we specify the stream to be read from, and may optionally specify the size of the pushback buffer–i.e., the number of characters that may be pushed back onto the stream or “unread.”If we do not specify a size for this buffer, the default size is one character.

PushbackReader inherits or overrides all the standard Reader methods, and also adds three unread() methods that are used to push a single character, an array of characters, or a portion of an array of characters back onto the stream.

This class is the character stream analog of PushbackInputStream.( PushbackReader is the character-oriented equivalent of PushbackInputStream.)

The class structure for PushbackReader is given as

public class java.io.PushbackReader extends java.io.FilterReader{
// Public Constructors
public PushbackReader(Reader in, int size);
public PushbackReader(Reader in);
// Public Instance Methods
public void close() throws IOException; // Overrides FilterReader
public boolean markSupported(); // Overrides FilterReader
public int read() throws IOException; // Overrides FilterReader
public int read(char[] cbuf, int off, int len) throws IOException; //Overrides FilterReader
public boolean ready() throws IOException; // Overrides FilterReader
public void unread(int c) throws IOException;
public void unread(char[] cbuf, int off, int len) throws IOException;
public void unread(char[] cbuf) throws IOException;
}

Details of PushbackReader implementation:

public PushbackReader(Reader in);

This constructor takes input as Reader object in which implies that the Reader to wrap the reader. It creates a new PushbackReader that reads from the given Reader. It uses pushback buffer with the default size of one byte.

public PushbackReader(Reader in,int size);

This constructor takes input as Reader object in which implies that the Reader to wrap the reader and the size of the pushback buffer. It creates a new PushbackReader that reads from the given Reader and pushback buffer size (provided by the user).

public void close() throws IOException;

close() method closes the reader and releases the system resources associated with it. It overrides the FilterReader.close() method. It throws an IOException in case of any kind of I/O error.

public boolean markSupported();

The markSupported() method returns false to tell that the class does not support mark() and reset() methods. It overrides FilterRedaer.markSupported() method. returns true in case the class supports mark() and reset() methods.

public int read() throws IOException;

The read() method reads a character of data. If there is any data in the pushback buffer, the method returns the next character in the pushback buffer. Otherwise, it calls the read() method of the underlying stream.

READ  Class PipedInputStream in Java

The method blocks until the character is read, the end of the stream is encountered, or an exception is thrown. so read() returns next character data (if available) or -1 if the control reaches to end of the stream. The read method overrides the FilterRedaer.read() method. The read() method throws an IOException in case an I/O error occurs.

public int read(char[] cbuf, int off, int len) throws IOException; 

This flavor of read() method copies characters from the stream into the given array cbuf, starting at index off and continuing for len characters. If the array can be populated solely from the pushback buffer, the method returns immediately. Otherwise, the read(char[], int, int) method of the underlying stream is called to make up the difference. The method blocks until some data are available.

This method overrides FilterReader.read(char[], int, int) method.

The parameter description is as follows:
the cbuf-An array of characters to be filled from the stream.
the off-An offset into the array.
len- The number of characters to read.

The method returns the actual number of characters read or -1 if the end of the stream is encountered immediately. The read() method throws an IOException in case an I/O error occurs.

public boolean ready() throws IOException;

If there is data in the pushback buffer, or if the underlying stream is ready, this method returns true. false otherwise. The underlying stream is ready if the next read() is guaranteed not to block. This method overrides FilterReader.ready() method. The ready() method throws an IOException if the stream is closed due to some issues.

public void unread(int c) throws IOException;

The unread() method puts the given character into the pushback buffer. It takes the argument of type int that indicates the value to push back. It throws IOException in case the pushback buffer is full.

public void unread(char[] cbuf, int off, int len) throws IOException;

This method unread() puts len characters from the given array, starting at offset off, into the pushback
buffer. It throws IOException in case the pushback buffer is full. The parameter description is as follows:
cbuf – An array of characters to push back.
off – An offset into the array.
len – The number of characters to push back.

public void unread(char[] cbuf) throws IOException;

The unread() method puts all the characters of the given array of characters into the pushback buffer. It throws IOException in case the pushback buffer is full.

READ  Abstract Interface Enumeration in Java

Apart from these this class also inherits the following methods from Object class and FilterReader class.

From ObjectFrom FilterRedaer
clone()read(char[])
finalize()skip(long)
hashCode()mark(int)
notify()reset()
wait()
wait(long, int)
equals(Object)
getClass()
notifyAll()
toString()
wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *