Java - PushbackInputStream unread(int b) method



Description

The Java PushbackInputStream unread(int b) method pushes back a byte by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value (byte)b.

Declaration

Following is the declaration for java.io.PushbackInputStream.unread(int b) method.

public void unread(int b)

Parameters

b − The int value whose low-order byte is to be pushed back.

Return Value

This method does not return a value.

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.

Example - Usage of PushbackInputStream unread(int b) method

The following example shows the usage of PushbackInputStream unread(int b) method.

PushbackInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;

public class PushbackInputStreamDemo {
   public static void main(String[] args) {
      
      // declare a buffer and initialize its size:
      byte[] arrByte = new byte[1024];

      // create an array for our message
      byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o',};

      // create object of PushbackInputStream class for specified stream
      InputStream is = new ByteArrayInputStream(byteArray);
      PushbackInputStream pis = new PushbackInputStream(is, 10);
      
      try {
         // read from the buffer one character at a time
         for (int i = 0; i < byteArray.length; i++) {

            // read a char into our array
            arrByte[i] = (byte) pis.read();

            // display the read byte
            System.out.print((char) arrByte[i]);
         }

         // change line
         System.out.println();

         // unread a char
         pis.unread('F');

         // read again from the buffer 
         arrByte[1] = (byte) pis.read();

         // display the read byte
         System.out.print((char) arrByte[1]);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello
F

Example - Re-reading a byte

The following example shows the usage of PushbackInputStream markSupported() method.

PushbackInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class PushbackInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] data = {65, 66, 67}; // A, B, C
      PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data));

      int b = pbis.read();
      System.out.println("Read: " + (char) b); // Output: A

      pbis.unread(b); // Pushes 'A' back to the stream

      int b2 = pbis.read();
      System.out.println("Re-read: " + (char) b2); // Output: A again
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Read: A
Re-read: A

Explanation

  • We read a byte ('A') from the stream.

  • We call unread(b) to push it back.

  • We read again and get the same byte ('A') because it was unread.

This is useful if you want to "peek" at a byte and decide later whether to process or skip it.

Example - Conditional parsing with unread

The following example shows the usage of PushbackInputStream unread(int b) method.

PushbackInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class PushbackInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] data = "1X2".getBytes();
      PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data));

      int b = pbis.read();
      if (Character.isDigit(b)) {
         System.out.println("Digit: " + (char) b); // Output: 1
      } else {
         pbis.unread(b); // Push back if not digit
      }

      b = pbis.read();
      if (Character.isDigit(b)) {
         System.out.println("Digit: " + (char) b);
      } else {
         System.out.println("Non-digit: " + (char) b); // Output: X
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Digit: 1
Non-digit: X
java_io_pushbackinputstream.htm
Advertisements