Site icon TechVidvan

Java PushbackInputStream

java pushbackinputstream

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

Explanation

Class Declaration :

Fields :

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:

Difference between PushbackInputStream and BufferedInputStream:

Uses :

Closing the PushbackInput:

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.

Exit mobile version