Java中的 ByteArrayInputStream mark() 方法及示例
mark()方法是Java.io.ByteArrayInputStream的内置方法,用于标记输入流的当前位置。它设置readlimit,即在标记位置无效之前可以读取的最大字节数。
语法:
public void mark(int arg)
参数:该函数接受一个强制参数arg ,该参数指定在标记位置无效之前可以读取的最大字节数。
返回值:该函数不返回任何内容。
下面是上述函数的实现:
方案一:
// 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());
}
}
输出:
5
6
7
Mark() invocation
8
9
方案二:
// 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());
}
}
输出:
1
Mark() invocation
2
3
参考: https: Java/io/ByteArrayInputStream.html#mark(int)