Java - PushbackInputStream unread(byte[] b,int off,int len) method



Description

The Java PushbackInputStream unread(byte[] b,int off,int len) method pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[off], the byte after that will have the value b[off+1], and so forth.

unread(byte[] b,int off,int len) method −

  • Pushes back len bytes from the byte array b, starting at offset off.

  • These bytes will be returned before any unread bytes still in the stream.

  • Useful for partial backtracking in parsing situations.

Declaration

Following is the declaration for java.io.PushbackInputStream.unread(byte[] b,int off,int len) method.

public void unread(byte[] b,int off,int len)

Parameters

  • b − The byte array to push back.

  • off − The start offset of the data.

  • len − The number of bytes to push 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(byte[] b,int off,int len) method

The following example shows the usage of PushbackInputStream unread(byte[] b,int off,int len) 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();

         // create a new byte array to be unread
         byte[] b = {'W', 'o', 'r', 'l', 'd'};

         // unread the byte array
         pis.unread(b, 2, 3);

         // read again from the buffer one character at a time
         for (int i = 0; i < 3; i++) {

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

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

Output

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

Hello
rld

Example - Unread a portion of a buffer

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

PushbackInputStreamDemo.java

package com.tutorialspoint;

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

public class PushbackInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] data = "ABCDEFG".getBytes(); // 7 bytes

      try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data), 10)) {
         byte[] buffer = new byte[4];
         pbis.read(buffer); // Read A B C D
         System.out.println("Initial read: " + new String(buffer)); // Output: ABCD

         // Push back just 'C' and 'D' from buffer (offset 2, length 2)
         pbis.unread(buffer, 2, 2);

         // Now reading again should return C, D, then E
         byte[] nextRead = new byte[3];
         pbis.read(nextRead);
         System.out.println("After unread: " + new String(nextRead)); // Output: CDE
      }
   }
}

Output

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

Initial read: ABCD
After unread: CDE

Explanation

  • Reads the first 4 bytes (ABCD) into a buffer.

  • Pushes back only 'C' and 'D' using unread(buffer, 2, 2).

  • Next read() fetches CD (pushed back) and the next byte from the stream (E).

Example - Look ahead, then unread a partial header

The following example shows the usage of PushbackInputStream unread(byte[] b,int off,int len) method.

PushbackInputStreamDemo.java

package com.tutorialspoint;

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

public class PushbackInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] data = "TYPE:DataPayload".getBytes();

      try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data), 20)) {
         byte[] header = new byte[5];
         pbis.read(header); // Read T Y P E :

         // Check the header
         String head = new String(header);
         System.out.println("Header: " + head); // Output: TYPE:

         // Push back only last 3 chars (P E :)
         pbis.unread(header, 2, 3);

         // Now read again from pushed-back section
         byte[] reread = new byte[5];
         pbis.read(reread);
         System.out.println("Re-read: " + new String(reread)); // Output: PE:Da
      }
   }
}

Output

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

Header: TYPE:
Re-read: PE:Da

Explanation

  • Reads TYPE: (5 bytes), simulating header processing.

  • Pushes back 'P', 'E', ':' (bytes 2 to 4 of the header).

  • The next read starts with PE: and continues into the next part of the stream.

java_io_pushbackinputstream.htm
Advertisements