ByteArrayInputStream markSupported() method in Java with Examples

The markSupported() method is a built-in method of the Java.io.ByteArrayInputStream method tests if this input stream supports the mark and reset methods. The markSupported method of ByteArrayInputStreamInputStream returns true always
Syntax:Â
public boolean markSupported()
Parameters: The function does not accepts any parameter.Â
Return Value: The function returns a boolean value. It returns true if this stream instance supports the mark and reset methods, else false.
Below is the implementation of the above function:
Program 1:Â Â
Java
// Java program to implement// the above functionimport java.io.*;Â
public class Main {    public static void main(String[] args) throws Exception    {Â
        byte[] buf = { 5, 6, 7, 8, 9 };Â
        // Create new byte array input stream        ByteArrayInputStream exam            = new ByteArrayInputStream(buf);Â
        // print bytes        System.out.println(exam.read());        System.out.println(exam.read());        System.out.println(exam.read());Â
        System.out.println("Mark() invocation");Â
        // Use of markSupported        boolean check = exam.markSupported();        System.out.println("markSupported() : "                           + check);Â
        if (exam.markSupported()) {Â
            // Use of reset() method :            // repositioning the stream to marked positions.            exam.reset();Â
            System.out.println("\nreset() invoked");            System.out.println(exam.read());            System.out.println(exam.read());        }        else {            System.out.println("reset() method not supported.");        }    }} |
Output:Â
5 6 7 Mark() invocation markSupported() : true reset() invoked 5 6
Â
Program 2:Â
Java
// Java program to implement// the above functionimport java.io.*;Â
public class Main {    public static void main(String[] args) throws Exception    {Â
        byte[] buf = { 1, 2, 3 };Â
        // Create new byte array input stream        ByteArrayInputStream exam            = new ByteArrayInputStream(buf);Â
        // print bytes        System.out.println(exam.read());        System.out.println(exam.read());        System.out.println(exam.read());Â
        // Use of markSupported        boolean check = exam.markSupported();Â
        System.out.println("markSupported() : "                           + check);Â
        if (exam.markSupported()) {Â
            // Use of reset() method :            // repositioning the stream to marked positions            exam.reset();Â
            System.out.println("\nreset() invoked");            System.out.println(exam.read());            System.out.println(exam.read());        }        else {            System.out.println("reset() method not supported.");        }    }} |
Output:Â
1 2 3 markSupported() : true reset() invoked 1 2
Â
Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#markSupported()
Â



