Class PushbackInputStream in Java

digitization, transformation, binary

PushbackInputStream in Java:

PushbackInputStream class is an input stream filter that provides a one-byte push back buffer. This feature allows an application to ‘unread’ the last character it read. The next time a read is performed on the input stream the ‘unread’ character is ‘reread’.

This functionality is useful in a situation where a fragment of the code to read is the indefinite number of data bytes. These data bytes are delimited by particular byte values. After reading the terminating byte, the code fragment can ‘unread’ it, so that the next read operation on the input stream will reread the byte that was pushed back.

Ads code goes here

The constructor accepts InputStream as an argument. This class is generally used for Scanner for a compiler.

The class structure of PushbackInputStream is given as:


public class java.io.PushbackInputStream extends  java.io.FilterInputStream{
//member element
protected int pushBack;
//A character 'unread' will be next byte read.The value-1 indicates nil character in the buffer.
protected byte[] buf;
//constructors
public PushbackInputStream(InputStream in);
//constructs a new PushbackInputStream object that reads input from  the specified InputStream.
public PushbackInputStream(InputStream in,int size);
//methods:
public int read();
public int read(byte b[],int offSet,int length);
public void unread(int ch);
public int available();
public boolean markSupported();
public void unread(byte[] b);
public void unread(byte[] b, int off, int len);
}
 

The details of the class structure are given as follows:

protected int pushBack;

protected int pushBack represents the position of pushed-back data in the buffer. When there is no pushed-back data, pos is buf.length.

As data is pushed back, pos decreases. As pushed-back data is read, pos increases. When the pushback buffer is full, pos is 0.

protected byte[] buf;

protected byte[] buf is the buffer that holds data that has been pushed back.

public PushbackInputStream(InputStream in);

public PushbackInputStream(InputStream in) constructor creates a PushbackInputStream that reads from the given InputStream, using a pushback buffer with the default size of one byte.

Parameter
in – The input stream to wrap.

public PushbackInputStream(InputStream in,int size);

public PushbackInputStream(InputStream in, int size) constructor creates a PushbackInputStream that reads from the given InputStream, using a pushback buffer of the given size.

Parameters
in –  The input stream to wrap.
size – The size of the pushback buffer.

public int read();

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

The method blocks until the byte are read, the end of the stream is encountered, or an exception is thrown.
This method returns the next byte of data, or -1 if the end of the stream is encountered.

public int read(byte b[],int offSet,int length);

public int read(byte b[], int offset, int length) method copies bytes from the stream into the given array b, starting at index off and continuing for len bytes. If the array can be populated solely from the pushback buffer, the method returns immediately.

Otherwise, the read(byte[], int, int) method of the underlying stream is called to make up the
difference. The method blocks until some data is available.

This method returns the actual number of bytes read, or -1 if the end of the stream is encountered immediately.
Parameters
b – An array of bytes to be filled from the stream.
offset – An offset into the byte array.
length– The number of bytes to read.

public void unread(int ch);

public void unread(int ch) method puts the given byte into the pushback buffer.
Parameter
ch – The value to push back.

public int available();

public int available() method returns the number of bytes that can be read without having to wait for more data to become available. This is b + u, where b is the number of bytes in the pushback buffer and u is the number of available bytes in the underlying stream.

This method returns the number of bytes that can be read without blocking.

public boolean markSupported();

public boolean markSupported() method returns false to indicate that this class does not support mark() and reset().
This method returns the boolean value false.

public void unread(byte[] b);

public void unread(byte[] b) method puts all of the bytes in the given array into the pushback buffer.
Parameter
b – An array of bytes to push back.

public void unread(byte[] b, int off, int len);

public void unread(byte[] b, int off, int len) method puts len bytes from the given array, starting at offset off, into the pushback buffer.
Parameter
b -An array of bytes to push back.
off – An offset into the array.
len -The number of bytes to push back.

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.

Apart from these PushbackInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

PushbackInputStream class also has inherited methods from FilterInputStream. They are as follows:

  • mark(int)
  • reset()
  • close()
  • read(byte[])
  • skip(long)

Share and Enjoy !

Leave a Comment

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