Java IO Tutorial - Java PushbackInputStream .unread (byte[] b, int off, int len)








Syntax

PushbackInputStream.unread(byte[] b, int off, int len) has the following syntax.

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

Example

In the following code shows how to use PushbackInputStream.unread(byte[] b, int off, int len) method.

/*from ww w . jav a2 s  . c  o m*/

import java.io.*;

public class Main {

   public static void main(String[] args) {
      byte[] arrByte = new byte[1024];
      byte[] byteArray = new byte[]{'j', 'a', 'v', 'a', '2','s','.','c','o','m'};

      InputStream is = new ByteArrayInputStream(byteArray);
      PushbackInputStream pis = new PushbackInputStream(is, 10);
      try {
         for (int i = 0; i < byteArray.length; i++) {
            arrByte[i] = (byte) pis.read();
            System.out.println((char) arrByte[i]);
         }
         byte[] b = {'W', 'o', 'r', 'l', 'd'};

         pis.unread(b, 2, 3);

         for (int i = 0; i < 3; i++) {
            arrByte[i] = (byte) pis.read();
            System.out.println((char) arrByte[i]);
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.