Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaPushbackInputStream unread() method in Java with Examples

PushbackInputStream unread() method in Java with Examples

The unread() method of PushbackInputStream class in Java is of three types:

  1. 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 IOException
    

    Parameters: 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
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E',
                               'K', 'S' };
      
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
      
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr);
      
            for (int i = 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
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E', 'K', 'S',
                               'F', 'O', 'R', 'G', 'E',
                               'E', 'K', 'S' };
      
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
      
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr);
      
            for (int i = 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
    
  2. 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 IOException
    

    Parameters: 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
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E',
                               'K', 'S' };
      
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
      
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr, 100);
      
            for (int i = 0; i < byteArray.length; i++) {
                System.out.print(
                    (char)pushbackInputStr.read());
            }
      
            // Create byte array
            byte[] b = new byte[] { 'A', 'B', 'C' };
      
            // Call unread() method
            pushbackInputStr.unread(b);
      
            System.out.println();
      
            for (int i = 0; i < b.length; i++) {
                System.out.print(
                    (char)pushbackInputStr.read());
            }
        }
    }

    
    
    Output:

    GEEKS
    ABC
    

    Program 2:




    // Java program to illustrate
    // PushbackInputStream unread(byte[]) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E', 'K', 'S',
                               'F', 'O', 'R', 'G', 'E',
                               'E', 'K', 'S' };
      
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
      
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr, 100);
      
            for (int i = 0; i < byteArray.length; i++) {
                System.out.print(
                    (char)pushbackInputStr.read());
            }
      
            // Create byte array
            byte[] b = new byte[] { 'X', 'Y', 'Z' };
      
            // Call unread() method
            pushbackInputStr.unread(b);
      
            System.out.println();
      
            for (int i = 0; i < b.length; i++) {
                System.out.print(
                    (char)pushbackInputStr.read());
            }
        }
    }

    
    
    Output:

    GEEKSFORGEEKS
    XYZ
    
  3. 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 IOException
    

    Parameters: 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
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E',
                               'K', 'S' };
      
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
      
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr, 100);
      
            for (int i = 0; i < byteArray.length; i++) {
                System.out.print(
                    (char)pushbackInputStr.read());
            }
      
            // Create byte array
            byte[] b
                = new byte[] { 'A', 'B', 'C',
                               'D', 'E' };
      
            // Call unread() method
            pushbackInputStr.unread(b, 2, 3);
      
            System.out.println();
      
            for (int i = 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
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E', 'K', 'S',
                               'F', 'O', 'R', 'G', 'E',
                               'E', 'K', 'S' };
      
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
      
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr, 100);
      
            for (int i = 0; i < byteArray.length; i++) {
                System.out.print(
                    (char)pushbackInputStr.read());
            }
      
            // Create byte array
            byte[] b = new byte[] { 'W', 'X', 'Y', 'Z' };
      
            // Call unread() method
            pushbackInputStr.unread(b, 1, 3);
      
            System.out.println();
      
            for (int i = 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)

RELATED ARTICLES

Most Popular

Recent Comments