Signup/Sign In

Java ByteArrayInputStream markSupported() Method

In this tutorial, we will learn about the markSupported() method of the ByteArrayInputStream class in Java. This method is used to verify whether the stream supports the mark() and reset() method or not. It returns the boolean value true if the stream supports them otherwise it returns false. It is a non-static method, available in the java.io package, which should only be accessed using class objects. It will throw an exception if accessed through class name.

Syntax:

This is the syntax declaration of the markSupported() method, this method does not accept any parameter. This method returns a boolean value indicating the supportability of the mark() and reset() methods by the stream.

public boolean markSupported()

Example: ByteArrayInputStream markSupported() Method

Here we are checking if the current stream supports the mark() method or not, in our case this method is returning a true value it means the current stream supports the mark() method.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		byte[] buf = {1, 2, 3, 4};  
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);  
        boolean isMarkSupported = byteArrayInputStream.markSupported();  
        System.out.println("Is mark supported : "+isMarkSupported);  
	}  
}


Is mark supported : true

Example: ByteArrayInputStream markSupported() Method

Here, we will explore how this markSupported() method works, firstly we check if this method supports the mark method ore not, then we read some data and at that point, we mark the position by calling the mark() method, then we again read data and call the reset() method as we call the reset method and this will set the stream pointer to the recently marked position snd after this if we call read method then it will reading from that point again.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		try 
		{
			byte[] buf = {65, 66, 67, 68, 69};
			ByteArrayInputStream bais = new ByteArrayInputStream(buf);

			boolean isMarkSupported = bais.markSupported();
			System.out.println("Is mark supported : "+isMarkSupported);

			System.out.println("Byte read "+ bais.read());
			System.out.println("Byte read "+ bais.read());
			System.out.println("Byte read "+ bais.read());

			System.out.println("Mark() invocation");

			bais.mark(0);
			System.out.println("Byte read "+ bais.read());
			System.out.println("Byte read "+ bais.read());

			System.out.println("Reset() invocation");

			bais.reset();
			System.out.println("Byte read "+ bais.read());
			System.out.println("Byte read "+ bais.read());

		} 
		catch(Exception e) 
		{
			e.printStackTrace();
		}
	}  
}


Is mark supported : true
Following is the proof:
Byte read 65
Byte read 66
Byte read 67
Mark() invocation
Byte read 68
Byte read 69
Reset() invocation
Byte read 68
Byte read 69

mail Conclusion:

In this tutorial, we learned about markSupported() method of ByteArrayInputStream class in Java. The markSupported()method of ByteArrayInputStream class in Java is used to verify whether the stream supports the mark() and reset() method or not. It returns the boolean value true if the stream supports them otherwise it returns false.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.