A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-io-pushbackinputstream-class-java/ below:

Java.io.PushbackInputStream class in Java - GeeksforGeeks

Java.io.PushbackInputStream class in Java

Last Updated : 14 Mar, 2022

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
// Java code illustrating available(), close() 
// and markSupported() methods

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 IOException
    {
        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);
        
        // checking no. of bytes available
        pw.println("available bytes: " + push.available());
        
        // checking if mark is supported
        pw.println("mark supported? :" + push.markSupported());
        
        pw.close();
    }
}

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.
Java
// Java code illustrating read() 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 IOException
    {
        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();
        push.read(b, 0, 13);
        for(int i=0; i<13; i++)
        {
            pw.print((char)b[i]);
        }
        pw.println();
        pw.close();
    }
}

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4