Thursday, October 16, 2025
HomeLanguagesJavaByteArrayInputStream mark() method in Java with Examples

ByteArrayInputStream mark() method in Java with Examples

The mark() method is a built-in method of the Java.io.ByteArrayInputStream marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid.

Syntax:

public void mark(int arg)

Parameters: The function accepts a single mandatory parameter arg which specifies the maximum limit of bytes that can be read before the mark position becomes invalid.

Return Value: The function returns nothing.

Below is the implementation of the above function:

Program 1:




// Java program to implement
// the above function
import 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");
  
        // mark() invocation;
        exam.mark(0);
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}


Output:

5
6
7
Mark() invocation
8
9

Program 2:




// Java program to implement
// the above function
import 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("Mark() invocation");
  
        // mark() invocation;
        exam.mark(3);
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}


Output:

1
Mark() invocation
2
3

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#mark(int)

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS