Java PushbackInputStream

Java is used in various applications. It also used in real time projects. Lets we discuss about the PushbackInputStream.

Explanation

  • An input stream can have a byte read and then “pushed back” (or restored) to the stream by using pushback. The PushbackInputStream class.
    concept was implemented.
  • It offers a way to “peek” at input stream data without interfering with it.
  • FilterInputStream is extended by it.

Class Declaration :

  • public class PushbackInputStream extends

Fields :

  • protected byte[] buf: This buffer is used for pushback.
  • protected int pos : This is where the next byte will be read from in the pushback buffer.
  • protected InputStream in : It needs to be filtered in the input stream.

Table

Method  Description 
protected byte[] Buf

The pushback buffer 

protected int Pos

The next byte is read the position within the  pushback buffer.

How to create PushbackInput:

You must first build an instance of a Java PushbackInputStream before you can utilize it. This is an illustration of how to make a PushbackInputStream in Java:

PushbackInputStream input =new PushbackInputStream (new FileInputStream )

Read Bytes :

int aByte = pushbackInputStream.read();

while(aByte != -1) {
        byte byteRead = (byte) aByte;

    aByte = pushbackInputStream.read();
}

Push a byte :

int aByte = pushbackInputStream.read();

pushbackInputStream.unread(aByte);

aByte = pushbackInputStream.read()

Methods Table :

Method Description
int available() The input stream thait.number of bytes that can be read and returned it.
int read() It reads the subsequent data byte from the input stream.
boolean markSupported() It reads the boolean that supported from the input stream.
void mark(int readlimit) It is employed to indicate where the input stream is at that moment.
long skip(long x) It is employed to bypass and eliminate x bytes of information.
void unread(int b) By copying the byte to the pushback buffer, it is utilized to push the byte back.
void unread(byte[] b) It copies the array of bytes to the pushback buffer in order to push it back.
void reset() The input stream is reset using it.
void close() The input stream is closed using it.

Example :

import java.io.*;  
public class InputStreamExample {  
public static void main(String[] args)
{  
          String srg = "1##2#34##12";  
          byte ary[] = srg.getBytes();  
          ByteArrayInputStream array = new ByteArrayInputStream(ary);  
          PushbackInputStream push = new PushbackInputStream(array);  
          int i;        
              while( (i = push.read())!= -1) {  
                  if(i == '#') {  
                      int j;  
                      if( (j = push.read()) == '#'){  
                           System.out.print("**");  
                      }else {  
                          push.unread(j);  
                          System.out.print((char)i);  
                      }  
                  }else {  
                              System.out.print((char)i);  
                  }  
             }        
  }   
}

Output :
1**2#34**#12

Example 2:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
 
 
public class PushbackInputStream
{
    public static void main(String arg[]) 
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "Hey geeks ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
                pw.println("Bytes: " + push.available());
         
                pw.println("Supported :" + push.markSupported());
         
        pw.close();
    }
}

Output :
Bytes :10
Supported :False

Constructors:

  • PushbackInputStream(InputStream in) :One byte can be returned to the input stream thanks to the stream object that is created in this way.
  • PushbackInputStream(InputStream in, int numBytes): This produces a stream with a numBytes-long pushback buffer. As a result, the input stream can receive several bytes back.

Difference between PushbackInputStream and BufferedInputStream:

  • When buffering is not supported by your input stream, use PushbackInputStream.
  • After reading, you return it to the stream. We just use BufferedInputStream to call reset() after reading the 10 bytes with read(10) and marking(10).

Uses :

  • By adding the capacity to buffer input and support the mark and reset methods, a BufferedInputStream enhances the usefulness of another input stream.
  • When BufferedInputStream is created, the internal buffer array is generated during the time.

Closing the PushbackInput:

  • It is important to remember to close a Java PushbackInputStream once you have completed reading its contents.
  • The InputStream instance that a PushbackInputStream is reading from will also close when a PushbackInputStream is closed.
pushbackInputStream.close();

Conclusion

A Java class called PushbackInputStream lets you “push back” or unread some bytes into the stream. This comes in handy when you read a few bytes from an input stream and determine that you would like to “unread” the bytes by pushing them back into the stream so that they can be read once more.