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 b) throws IOExceptionParameters: This method accepts one parameter b that represents the integer value which is to be pushed back.
Return value: This method does not return any value.
Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or if there is not enough space in the pushback buffer for the byte.
Below programs illustrate unread(int) method of PushbackInputStream class in IO package:
Program 1:
// Java program to illustrate// PushbackInputStream unread(int) 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.print(               Â(char)pushbackInputStr.read());       Â}       Â// Call unread() method       ÂpushbackInputStr.unread(65);       ÂSystem.out.print(           Â"\n"           Â+ (char)pushbackInputStr.read());   Â}}Output:GEEKS A
Program 2:
// Java program to illustrate// PushbackInputStream unread() 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.print(               Â(char)pushbackInputStr.read());       Â}       Â// Call unread() method       ÂpushbackInputStr.unread(90);       ÂSystem.out.print(           Â"\n"           Â+ (char)pushbackInputStr.read());   Â}}Output:GEEKSFORGEEKS Z
- The unread(byte[] b) method of PushbackInputStream class in Java is used to push back an array of 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 first element of the byte array.
Syntax:
public void unread(byte[] b) throws IOExceptionParameters: This method accepts one parameter b that represents the byte array which is to be pushed back.
Return value: This method does not return any value.
Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or if there is not enough space in the pushback buffer for the array byte.
Below programs illustrate unread(byte[]) method of PushbackInputStream class in IO package:
Program 1:
// Java program to illustrate// PushbackInputStream unread(byte[]) 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,100);       Âfor(inti =0; i < byteArray.length; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}       Â// Create byte array       Âbyte[] b =newbyte[] {'A','B','C'};       Â// Call unread() method       ÂpushbackInputStr.unread(b);       ÂSystem.out.println();       Âfor(inti =0; i < b.length; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}   Â}}Output:GEEKS ABC
Program 2:
// Java program to illustrate// PushbackInputStream unread(byte[]) 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,100);       Âfor(inti =0; i < byteArray.length; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}       Â// Create byte array       Âbyte[] b =newbyte[] {'X','Y','Z'};       Â// Call unread() method       ÂpushbackInputStr.unread(b);       ÂSystem.out.println();       Âfor(inti =0; i < b.length; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}   Â}}Output:GEEKSFORGEEKS XYZ
- The unread(byte[] b, int offset, int length) method of PushbackInputStream class in Java is used to push back a part of an array of 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 first element of the portion of the given byte array.
Syntax:
public void unread(byte[] b, int offset, int length) throws IOExceptionParameters: This method accepts three parameters:
- b – It represents the byte array the portion of which is to be pushed.
- offset – It represents the starting index of the portion of byte array.
- length – It represents the number of bytes to be pushed.
Return value: This method does not return any value.
Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or if there is not enough space in the pushback buffer for the array byte.
Below programs illustrate unread(byte[], int, int) method of PushbackInputStream class in IO package:
Program 1:
// Java program to illustrate// PushbackInputStream// unread(byte[], int, int) 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,100);       Âfor(inti =0; i < byteArray.length; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}       Â// Create byte array       Âbyte[] b           Â=newbyte[] {'A','B','C',                          Â'D','E'};       Â// Call unread() method       ÂpushbackInputStr.unread(b,2,3);       ÂSystem.out.println();       Âfor(inti =0; i <3; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}   Â}}Output:GEEKS CDE
Program 2:
// Java program to illustrate// PushbackInputStream// unread(byte[], int, int) 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,100);       Âfor(inti =0; i < byteArray.length; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}       Â// Create byte array       Âbyte[] b =newbyte[] {'W','X','Y','Z'};       Â// Call unread() method       ÂpushbackInputStr.unread(b,1,3);       ÂSystem.out.println();       Âfor(inti =0; i <3; i++) {           ÂSystem.out.print(               Â(char)pushbackInputStr.read());       Â}   Â}}Output:GEEKSFORGEEKS XYZ
References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#unread(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#unread(byte%5B%5D)
3. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#unread(byte%5B%5D, int, int)
