Java - PushbackInputStream mark(int readlimit) method



Description

The Java PushbackInputStream mark(int readlimit) method marks the current position in this input stream,but for PushbackInputStream it does nothing.

The method exists because PushbackInputStream inherits it from the InputStream superclass, but it does not override it to provide actual functionality.

Instead

  • If you try to call mark(int) or reset() on a PushbackInputStream, they will do nothing or may not behave as expected.

  • Calling markSupported() will return false.

Declaration

Following is the declaration for java.io.PushbackInputStream.mark(int readlimit) method.

public void mark(int readlimit)

Parameters

readlimit − The maximum limit of bytes that can be read before the mark position becomes invalid.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PushbackInputStream mark(int readlimit) method

The following example shows the usage of PushbackInputStream mark(int readlimit) 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]);
         }

         // mark this position, but it does nothing for this class
         pis.mark(5);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello

Example - markSupported() returns false

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

PushbackInputStreamDemo.java

package com.tutorialspoint;

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

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

      System.out.println("markSupported(): " + pbis.markSupported()); // Output: false
   }
}

Output

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

markSupported(): false

Explanation

  • markSupported() correctly indicates that PushbackInputStream does not support mark/reset operations.

Example - Attempt to use mark() and reset() (ineffective)

The following example shows the usage of PushbackInputStream mark(int readlimit) 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 = "Example".getBytes();

      try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data))) {
         pbis.mark(10);  // No effect
         int ch1 = pbis.read(); // 'E'
         int ch2 = pbis.read(); // 'x'
         System.out.println("Read chars: " + (char)ch1 + (char)ch2); // Output: Ex

         pbis.reset(); // Does nothing; not supported
         System.out.println("Reset called (no effect)");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read chars: Ex
java.io.IOException: mark/reset not supported

Explanation

  • Even though mark() and reset() are called, they do nothing because PushbackInputStream doesn't support them.

  • You can only push back bytes using unread(), not use mark/reset.

java_io_pushbackinputstream.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy