The mark() method in ByteArrayInputStream in Java is used to set a mark at the current position in the stream. This allows you to later reset the stream to this marked position using the reset() method. The mark() method is particularly useful when you need to read from a certain point and then return to that point later in the stream.
Syntax:
public void mark(int readlimit)readlimit: The maximum number of bytes that can be read before the mark position becomes invalid. InByteArrayInputStream, this parameter is ignored, and the mark is always valid.
Return Value:
- The method does not return a value.
Program of mark() method in ByteArrayInputStream in Java
Example 1: Basic Usage of mark()
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
byte[] buf = {5, 6, 7, 8, 9};
ByteArrayInputStream exam = new ByteArrayInputStream(buf);
System.out.println(exam.read());
System.out.println(exam.read());
System.out.println(exam.read());
System.out.println("Mark() invocation");
exam.mark(0);
System.out.println(exam.read());
System.out.println(exam.read());
}
}
Output
5 6 7 Mark() invocation 8 9
Explanation of the above Program:
- This program demonstrates the use of
mark()with a readlimit of 0. - The mark is effectively invalidated immediately because no bytes can be read before the mark becomes invalid.
Example 2: Using mark() with a Non-zero Read Limit
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
byte[] buf = {1, 2, 3};
ByteArrayInputStream exam = new ByteArrayInputStream(buf);
System.out.println(exam.read());
System.out.println("Mark() invocation");
exam.mark(3);
System.out.println(exam.read());
System.out.println(exam.read());
}
}
Output
1 Mark() invocation 2 3
Explanation of the above Program:
- This example shows
mark()with a readlimit of 3. - The mark position remains valid as long as up to 3 bytes are read before the mark is invalidated.
Example Program: Using mark() and reset()
To better understand the functionality of mark(), consider the following example which also uses the reset() method.
import java.io.*;
public class MarkAndResetDemo {
public static void main(String[] args) {
String str = "Kamlesh is one of the student who not attend placement in campus.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
// Read the first 10 bytes
for (int i = 0; i < 10; i++) {
int c = inputStream.read();
if (c != -1) System.out.print((char) c);
}
inputStream.mark(20);
// Read the next 20 bytes
for (int i = 0; i < 20; i++) {
int c = inputStream.read();
if (c != -1) System.out.print((char) c);
}
System.out.println();
// Reset to the marked position and read again
inputStream.reset();
// Read the next 30 bytes
for (int i = 0; i < 30; i++) {
int c = inputStream.read();
if (c != -1) System.out.print((char) c);
}
}
}
Output
Kamlesh is one of the student one of the student who not at
Explanation of the above Program:
- This example demonstrates how
mark()andreset()can be used together. - After reading 10 bytes, the mark is set.
- Then, 20 more bytes are read before resetting the stream to the marked position.
- Finally, 30 bytes are read from the marked position, showing how
mark()andreset()can be used to revisit a previously marked position in the stream.
Conclusion
The mark() method in ByteArrayInputStream provides a way to control and manage the reading position in a byte array. By setting a mark, you can later use the reset() method to return to this marked position, enabling flexible and efficient data processing. Understanding how to effectively use these methods is essential for tasks that require revisiting specific points in a data stream.