Open In App

Java.io.PushbackInputStream class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  • protected byte[] buf:This is the pushback buffer. 
     
  • protected int pos – This is the position within the pushback buffer from which the next byte will be read. 
     
  • protected InputStream in – This is the input stream to be filtered. 

Constructors: 

  • PushbackInputStream(InputStream in): This creates a stream object that allows one byte to be returned to the input stream. 
     
  • PushbackInputStream(InputStream in, int numBytes): This creates a stream that has a pushback buffer that is numBytes long. This allows multiple bytes to be returned to the input stream. 

Methods: 

  • int available(): Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block but may read or skip fewer bytes. 
     
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.
  • void close(): Closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), unread(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect. 
Syntax: public void close()
Returns: NA
Exception: IOException - if an I/O error occurs.
  • boolean markSupported(): Tests if this input stream supports the mark and reset methods, which it does not. 
Syntax: public boolean markSupported()
Returns: false, since this class does not support the mark and reset methods.
Exception: NA

Java




// 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
  • int read(): Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. 
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.
  • int read(byte[] b, int off, int len): Reads up to len bytes of data from this input stream into an array of bytes. This method first reads any pushed-back bytes; after that, if fewer than len bytes have been read then it reads from the underlying input stream. If len is not zero, the method blocks until at least 1 byte of input is available; otherwise, no bytes are read and 0 is returned. 
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.

Java





Output: 

GeeksforGeeks a computer science portal 
GeeksforGeeks
  • void mark(int readlimit): Marks the current position in this input stream. 
    The mark method of PushbackInputStream does nothing. 
Syntax: public void mark(int readlimit)
Returns: NA
Exception: NA
  • void reset(): Repositions this stream to the position at the time the mark method was last called on this input stream. 
    The method reset for class PushbackInputStream does nothing except throw an IOException. 
Syntax: public void reset()
Returns: NA
Exception: IOException - if this method is invoked.

Java




// 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)
  • void unread(byte[] b): Pushes back an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[0], the byte after that will have the value b[1], and so forth. 
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.
  • void unread(byte[] b,int off,int len): Pushes back an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[0], the byte after that will have the value b[1], and so forth. 
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




// 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
  • void unread(int b): Pushes back a byte by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value (byte)b. 
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




// 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



Last Updated : 14 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads