gpt4 book ai didi

java - 缓冲输入流中标记读取限制的用途是什么

转载 作者:行者123 更新时间:2023-12-01 14:28:59 25 4
gpt4 key购买 nike

我是 Java 流的新手,我想阅读特定文件的内容,然后需要从头开始阅读。我创建了一个 BufferedInputStream 并且我对 BufferedInputStream.mark(int markLimit) 的文档感到困惑

文档说:

public void mark(int readlimit)

This method marks a position in the input to which the stream can be "reset" by calling the reset() method. The parameter readlimit is the number of bytes that can be read from the stream after setting the mark before the mark becomes invalid. For example, if mark() is called with a read limit of 10, then when 11 bytes of data are read from the stream before the reset() method is called, then the mark is invalid and the stream object instance is not required to remember the mark.

Note that the number of bytes that can be remembered by this method can be greater than the size of the internal read buffer. It is also not dependent on the subordinate stream supporting mark/reset functionality.

Overrides: mark in class FilterInputStream

Parameters: readlimit - The number of bytes that can be read before the mark becomes invalid**



我的代码是:
public class Test {
public static void main(String[] args) throws IOException {

File resource = new File("beforeFix.txt");
FileInputStream fileInputStream = new FileInputStream(resource);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
int i = bufferedInputStream.read();
bufferedInputStream.mark(1);
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
bufferedInputStream.reset();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
i = bufferedInputStream.read();
bufferedInputStream.reset();
}
}

在上面的代码中,我将标记限制设置为 1,但根据文档,标记不会变为无效。

谁能清楚地解释我用小例子设置这个的实际目的是什么?

提前致谢

最佳答案

为了使reset工作并回到你标记的位置,你标记后读取的数据需要缓存在内存中。您在标记时指定的值是应为此保留的内存量。

因此,如果您打算在调用 reset 之前读取 100 个字节,那么您的缓冲区需要至少为 100 个字节,这就是您必须使用的标记。

bufferedInputStream.mark(200);

... read no more than 200 bytes ...

bufferedInputStream.reset(); // reset back to marked position

更新

它看起来像 mark 的文档与实际行为不符。文档指出:
the maximum limit of bytes that can be read before the mark position becomes invalid

但是,看起来应该是 the minimum limit ,或者至少底层实现不需要在超过读取限制时立即丢弃标记,如果它们仍然可以支持重置到标记位置。

关于java - 缓冲输入流中标记读取限制的用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com