Open In App

Java.io.PushbackInputStream class in Java

Pushback is used on an input stream to allow a byte to be read and then returned (i.e, “pushed back”) to the stream. The PushbackInputStream class implements this idea. It provides a mechanism “peek” at what is coming from an input stream without disrupting it. 
It extends FilterInputStream.
Fields: 

Constructors: 



Methods: 

Syntax: public int available()
Returns: the number of bytes that can be read
 (or skipped over) from the input stream without blocking.
Exception: IOException - if this input stream has 
been closed by invoking its close() method, or an I/O error occurs.
Syntax: public void close()
Returns: NA
Exception: IOException - if an I/O error occurs.
Syntax: public boolean markSupported()
Returns: false, since this class does not support the mark and reset methods.
Exception: NA




// java code illustrating unread() method
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
 
 
public class PushbackInputStreamDemo
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
 
      // unread method
        push.unread('A');
        b[1] = (byte)push.read();
        pw.println((char)b[1]);
    }
}

Output: 



available bytes: 10
mark supported? :false
Syntax: public int read()
Returns: the next byte of data, or -1 if 
the end of the stream has been reached.
Exception: IOException - if this input stream 
has been closed by invoking its close() method, or an I/O error occurs.
Syntax: public int read(byte[] b, int off, int len).
Returns: the total number of bytes read into 
the buffer, or -1 if there is no more data because the end of 
the stream has been reached.
Exception:  NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or 
len is greater than b.length - off
IOException - if this input stream has been closed by invoking its 
close() method, or an I/O error occurs.





Output: 

GeeksforGeeks a computer science portal 
GeeksforGeeks
Syntax: public void mark(int readlimit)
Returns: NA
Exception: NA
Syntax: public void reset()
Returns: NA
Exception: IOException - if this method is invoked.




// Java code illustrating mark() and reset() method
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
 
 
public class PushbackInputStreamDemo
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
        int c;
        while((c=push.read())!=-1)
        {
            pw.print((char)c);
        }
        pw.println();
         
        // marking the position
        push.mark(5);
         
        // resetting is not supported throw exception
        push.reset();
         
        pw.close();
    }
}

Output: 

GeeksforGeeks a computer science portal 
Exception in thread "main" java.io.IOException: mark/reset not supported
    at java.io.PushbackInputStream.reset(PushbackInputStream.java:364)
    at PushbackInputStreamDemo.main(PushbackInputStreamDemo.java:29)
Syntax: public void unread(byte[] b)
returns: NA
Exception: IOException - If there is not enough room in 
the pushback buffer for the specified number of bytes, or this input 
stream has been closed by invoking its close() method.
Syntax: public void unread(byte[] b,int off,int len)
Returns: NA
Exception: IOException - If there is not enough room 
in the pushback buffer for the specified number of bytes, or this input 
stream has been closed by invoking its close() method.




// Java code illustrating unread() method
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
 
 
public class PushbackInputStreamDemo
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
        int c;
        while((c=push.read())!=-1)
        {
            pw.print((char)c);
        }
        pw.println();
 
      // unread method
        push.unread(b);
        push.unread(b, 0, 6);
 
        while((c=push.read())!=-1)
        {
            pw.print((char)c);
        }
        pw.println();
        pw.close();
    }
}

Output: 

GeeksforGeeks a computer science portal
orGeeks a computer science portal
Syntax: public void unread(int b)
Returns: NA
Exception: IOException - If there is not enough 
room in the pushback buffer for the byte, or this input stream 
has been closed by invoking its close() method.




// java code illustrating unread() method
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
 
 
public class PushbackInputStreamDemo
{
    public static void main(String arg[]) throws Exception
    {
        PrintWriter pw = new PrintWriter(System.out, true);
        String str = "GeeksforGeeks a computer science portal ";
        byte b[] = str.getBytes();
        ByteArrayInputStream bout = new ByteArrayInputStream(b);
        PushbackInputStream push = new PushbackInputStream(bout);
         
 
      // unread method
        push.unread('A');
        b[1] = (byte)push.read();
        pw.println((char)b[1]);
    }
}

Output: 

A

Article Tags :