The Wayback Machine - http://web.archive.org/web/20240712215817/https://www.geeksforgeeks.org/java-io-pushbackinputstream-class-java/
Open In App

Java.io.PushbackInputStream class in Java

Last Updated : 14 Mar, 2022
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



Similar Reads

PushbackInputStream close() method in Java with Examples
The close() method of PushbackInputStream class in Java is used to closes the input stream and it releases resources of system that are associated with the stream. After calling this method, further calling other method if this class will throw the IOException. Syntax: public void close() throws IOException Specified By: This method is specified by
2 min read
PushbackInputStream skip() method in Java with Examples
The skip(long n) method of PushbackInputStream class in Java is used to skip over and discards n bytes of data from this input stream. This method first skips over the bytes in the pushback buffer, and then calls the skip method of the main input stream. It returns the actual number of bytes skipped. Syntax: public long skip(long n) throws IOExcept
2 min read
PushbackInputStream available() method in Java with Examples
The available() method of PushbackInputStream class in Java is used to find the number of bytes that can be read from the input stream without blocking. It returns the estimated value of this number of bytes. It may be blocked by the next invocation of a method for the same input stream. Syntax: public int available() throws IOException Overrides:
2 min read
PushbackInputStream reset() method in Java with Examples
The reset() method of PushbackInputStream class in Java is used to reset the steam to the position where mark() method was called. This method does nothing for PushbackInputStream. Syntax: public void reset() throws IOException Overrides: This method overrides the reset() method of FilterInputStream class. Parameters: This method does not accept an
2 min read
PushbackInputStream markSupported() method in Java with Examples
The markSupported() method of PushbackInputStream class in Java is used to verify the supportability of mark() and reset() methods. It always returns false as this class does not support mark() and reset(). Syntax: public boolean markSupported() Overrides: This method overrides the markSupported() method of FilterInputStream class. Parameters: This
2 min read
PushbackInputStream mark() method in Java with Examples
The mark() method of PushbackInputStream class in Java is used to mark the current position in the input stream. This method does nothing for PushbackInputStream. Syntax: public void mark(int readlimit) Overrides: This method overrides the mark() method of FilterInputStream class. Parameters: This method accepts single parameter readlimit that repr
2 min read
PushbackInputStream read() method in Java with Examples
The read() method of PushbackInputStream class in Java is of two types: The read() method of PushbackInputStream class in Java is used to read the next byte of data from the input stream. This method returns the read byte from the input stream in the form of an integer. Syntax: public int read() throws IOException Overrides: This method overrides t
4 min read
PushbackInputStream unread() method in Java with Examples
The unread() method of PushbackInputStream class in Java is of three types: The unread(int b) method of PushbackInputStream class in Java is used to push back a byte by copying it to the front of the pushback buffer. After revoking this method, when the next byte is read it has the value equal to the parameter passed. Syntax: public void unread(int
6 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Article Tags :
Practice Tags :
three90RightbarBannerImg