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 IOExceptionOverrides: This method overrides the read() method of FilterInputStream class.
Parameters: This method does not accept any parameter.
Return value: This method returns the next byte of the stream. It returns -1 if the stream is ended.
Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or an I/O error occurs.
Below programs illustrate read() method of PushbackInputStream class in IO package:
Program 1:
// Java program to illustrate// PushbackInputStream read() methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create an array       Âbyte[] byteArray           Â=newbyte[] {'G','E','E',                          Â'K','S'};       Â// Create inputStream       ÂInputStream inputStr           Â=newByteArrayInputStream(byteArray);       Â// Create object of       Â// PushbackInputStream       ÂPushbackInputStream pushbackInputStr           Â=newPushbackInputStream(inputStr);       Âfor(inti =0; i < byteArray.length; i++) {           ÂSystem.out.println(               Â"Char : "               Â+ (char)pushbackInputStr.read());       Â}   Â}}Output:Char : G Char : E Char : E Char : K Char : S
Program 2:
// Java program to illustrate// PushbackInputStream read() methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create an array       Âbyte[] byteArray           Â=newbyte[] {'G','E','E','K','S',                          Â'F','O','R','G','E',                          Â'E','K','S'};       Â// Create inputStream       ÂInputStream inputStr           Â=newByteArrayInputStream(byteArray);       Â// Create object of       Â// PushbackInputStream       ÂPushbackInputStream pushbackInputStr           Â=newPushbackInputStream(inputStr);       Âfor(inti =0; i < byteArray.length; i++) {           ÂSystem.out.println(               Â"Char : "               Â+ (char)pushbackInputStr.read());       Â}   Â}}Output:Char : G Char : E Char : E Char : K Char : S Char : F Char : O Char : R Char : G Char : E Char : E Char : K Char : S
- The read(byte[], int, int) method of PushbackInputStream class in Java is used to reads up to given bytes of data from the input stream into an array of bytes. This method does not read one character at a time, it reads several characters at one time.
Syntax:
public int read(byte[] b, int offset, int length) throws IOExceptionOverrides: This method overrides the read() method of FilterInputStream class.
Parameters: This method does not accept three parameters:
- b – It represents the byte array into which data is read.
- offset – It represents the starting index in the array.
- length – It represents the number of byte to be read.
Return value: This method returns the total number of bytes read. It returns -1 if the stream is ended.
Exceptions:
- NullPointerException – This method throws NullPointerException if byte array is null.
- IndexOutOfBoundsException – This method throws IndexOutOfBoundsException if offset is negative or length is negative or length is greater than the difference of length of array and offset.
- IOException – This method throws IOException if the input stream is closed by calling the close() method of the same class or an I/O error occurs.
Below programs illustrate read(byte[], int, int) method of PushbackInputStream class in IO package:
Program 1:
// Java program to illustrate// PushbackInputStream// read(byte[], int, int) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create buffer       Âbyte[] b =newbyte[1024];       Â// Create an array       Âbyte[] byteArray           Â=newbyte[] {'G','E','E',                          Â'K','S'};       Â// Create inputStream       ÂInputStream inputStr           Â=newByteArrayInputStream(byteArray);       Â// Create object of       Â// PushbackInputStream       ÂPushbackInputStream pushbackInputStr           Â=newPushbackInputStream(inputStr);       ÂpushbackInputStr.read(b,0,4);       Âfor(inti =0; i <4; i++) {           ÂSystem.out.print((char)b[i]);       Â}   Â}}Output:GEEK
Program 2:
// Java program to illustrate// PushbackInputStream// read(byte[], int, int) methodÂÂimportjava.io.*;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)       ÂthrowsIOException   Â{       Â// Create buffer       Âbyte[] b =newbyte[1024];       Â// Create an array       Âbyte[] byteArray           Â=newbyte[] {'G','E','E','K','S',                          Â'F','O','R','G','E',                          Â'E','K','S'};       Â// Create inputStream       ÂInputStream inputStr           Â=newByteArrayInputStream(byteArray);       Â// Create object of       Â// PushbackInputStream       ÂPushbackInputStream pushbackInputStr           Â=newPushbackInputStream(inputStr);       ÂpushbackInputStr.read(b,8,5);       Âfor(inti =8; i <13; i++) {           ÂSystem.out.print((char)b[i]);       Â}   Â}}Output:GEEKS
References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#read()
2. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#read(byte%5B%5D, int, int)



