缓冲输入流中标记读取限制的用途是什么 [英] what is the use of mark read limit in bufferedinputstream

查看:117
本文介绍了缓冲输入流中标记读取限制的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Java streams, I would like to read a specific files content then need to read it from the beginning. I have created a BufferedInputStream and i'm confused about the documentation of BufferedInputStream.mark(int markLimit)

文档说:

public void mark(int readlimit)




此方法标记输入中可以通过调用reset()方法将流重置到的位置。参数readlimit是设置标记后,在标记变为无效之前可以从流中读取的字节数。例如,如果调用mark()的读取限制为10,则在调用reset()方法之前从流中读取11个字节的数据时,该标记无效,并且不需要流对象实例记住标记。

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.

重写:类FilterInputStream中的
标记

Overrides: mark in class FilterInputStream

参数:
readlimit-标记无效之前可以读取的字节数**

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();  
    }
}

在上面的代码中,我将marklimit设置为1但根据文档,该标记不会失效。

In the above code I have set the marklimit as 1 but the mark is not goes into invalid as per the documentation.

任何人都可以用小例子清楚地解释一下设置它的实际目的是什么吗?

Can anyone clearly explain me what is the actual purpose of setting this with small example?

预先感谢

推荐答案

通过以指定的限制调用 mark ,您正在请求达到指定的限制后可以支持重置的功能,您并不否认超出此限制的功能。明确规范说:

By calling mark with a specified limit, you are requesting the capability to support resetting after reading up to the specified limit, you are not denying a capability beyond that. The specification clearly says:


但是,如果流超过 readlimit 重置之前从流中读取c $ c>个字节。

However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called.

不需要并不意味着不允许。规范只是说明您不能期望的始终有效,也没有说明您可以期望的始终失效。

"is not required to" does not imply "is not allowed to". The specification simply states what you can not expect to always work, it doesn’t state what you can expect to always fail.

对于 BufferedInputStream ,很容易解释发生的情况。每个 BufferedInputStream 都有一个容量,默认为8192,它始终可以重置超过其当前缓冲区容量的字节。通过指定更高的限制,您将导致它在需要时分配更大的缓冲区 ,以履行保证。

In the case of BufferedInputStream, it’s easy to explain what happens under the hood. Each BufferedInputStream has a capacity, the default is 8192, and it always can reset past as many bytes as its current buffer capacity. By specifying a higher limit, you will cause it to allocate a larger buffer when needed, to fulfill the guaranty.

因为您不能查询流的当前缓冲区容量,只要您读取的字节数不超过指定的限制,就只能依靠reset的保证。

Since you can’t query a stream for its current buffer capacity, you can only rely on the guaranty that reset works as long as you don’t read more bytes than the specified limit.

轻松更改示例以使其可重现:

It’s easy to change your example to make it fail reproducible:

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

这篇关于缓冲输入流中标记读取限制的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆