
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.