Java - PushbackInputStream reset() method



Description

The Java PushbackInputStream reset() method repositions this stream to the position at the time the mark method was last called on this input stream, but for class PushbackInputStream does nothing except throw an IOException.

The reset() method is not supported in PushbackInputStream. reset() is inherited but not functional. Although PushbackInputStream inherits the reset() method from InputStream, it does not override it to provide any actual functionality. Calling reset() on a PushbackInputStream will throw: java.io.IOException: mark/reset not supported.

Declaration

Following is the declaration for java.io.PushbackInputStream.reset() method.

public void reset()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PushbackInputStream reset() method

The following example shows the usage of PushbackInputStream reset() 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);

      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]);
         }

         // try to call the reset method, but it is not supported
         pis.reset();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

java.io.IOException: mark/reset not supported
Hello

Example - Demonstrate reset() throws exception

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) {
      byte[] data = "ABC".getBytes();

      try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data))) {
         pbis.read(); // read 'A'
         pbis.reset(); // This will throw IOException
      } catch (IOException e) {
         System.out.println("Exception: " + e); // Output: java.io.IOException: mark/reset not supported
      }
   }
}

Output

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

Exception: java.io.IOException: mark/reset not supported

Explanation

  • reset() is attempted after reading.

  • Since PushbackInputStream doesn't support it, an exception is thrown.

Example - Use unread() instead of reset()

The following example shows the usage of PushbackInputStream reset() 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) {
      byte[] data = "XY".getBytes();

      try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data))) {
         int first = pbis.read(); // 'X'
         System.out.println("Read: " + (char) first); // Output: X

         pbis.unread(first); // Push it back
         int again = pbis.read(); // 'X' again
         System.out.println("Read again after unread: " + (char) again); // Output: X
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read: X
Read again after unread: X

Explanation

  • Instead of reset(), unread() is used to simulate reading the same byte again.

  • This is the idiomatic way to "reset" in PushbackInputStream.

java_io_pushbackinputstream.htm
Advertisements