Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java PushbackInputStream Class

In this tutorial, we will learn about PushbackInputStream class in Java. This class extends FilterInputStream class and provides the mechanism "peek" at what is coming from an input stream without disrupting it. This class can unread a byte that is already read and push back one byte.

Syntax

This is the syntax declaration of PushbackInputSream class and this class extends FilterInputStream.

public class PushbackInputStream extends FilterInputStream  

PushbackInputStream Class Methods

All the methods of PushbackInputStream methods are available in the table given below:

Method Description
int available() This method is used to return the number of bytes that can be read from the input stream.
int read() This method is used to read the next byte of data from the input stream.
void mark(int readlimit) This method is used to mark the current position in the input stream.
long skip(long x) This method is used to skip over and discard x bytes of data.
void unread(int b) This method is used to pushes back the byte by copying it to the pushback buffer.
void unread(byte[] b) This method is used to pushes back the array of byte by copying it to the pushback buffer.
void reset() This method is used to reset the input stream.
void close() This method is used to close the input stream.
boolean markSupported() This method is used to check whether the position is marked or not.

Example of PushbackInputStream

In this example, we are demonstrating how available() method works in PushbackInputStream class. Here we have declared the print writer which will give output on the console. We also created a stream on input using ByteArrayInputStream and we passed this stream to the constructor while creating an object of PushbackInputStream class. available() method returns the number of bytes that can be read from this input stream. In this example, we are getting 18 as an example because the string "Hello Studytonight" is a length of 18.

package studytonight;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{  
			PrintWriter writer = new PrintWriter(System.out, true); 
			String str = "Hello Studytonight"; 
			byte arr[] = str.getBytes(); 
			ByteArrayInputStream stream = new ByteArrayInputStream(arr); 
			PushbackInputStream push = new PushbackInputStream(stream);    
			writer.println("size of available bytes: " + push.available()); 	          
			writer.close(); 
		}
		catch (Exception e)	{  
			System.out.print("Error: "+e.toString());
		}  
	}
}


size of available bytes: 18

Example of PushbackInputStream

In this example, we are implementing the read() method from PushbackInputStream class. This read() method returns an integer of range 0 to 255, the integers are corresponding to the ASCII code of characters. Here we created an object of PushbackInputStream class and called the read method until it reaches the end of the stream.

package studytonight;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{  
			PrintWriter writer = new PrintWriter(System.out, true); 
			String str = "Hello Studytonight"; 
			byte arr[] = str.getBytes(); 
			ByteArrayInputStream stream = new ByteArrayInputStream(arr); 
			PushbackInputStream push = new PushbackInputStream(stream); 
			int ch; 
			while((ch=push.read())!=-1) 
			{ 
				writer.print((char)ch); 
			} 
			writer.println(); 
			writer.close(); 
		}
		catch (Exception e)	{  
			System.out.print("Error: "+e.toString());
		}  
	}
}


Hello Studytonight

Conclusion

In this article, we learned about PushbackInputStream class. This class extends FilterInputStream class and provides the mechanism "peek" at what is coming from an input stream without disrupting it. This class can unread a byte that is already read and push back one byte.