缓冲输入流标记读取限制 [英] Buffered Input Stream mark read limit

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

问题描述

我正在学习如何使用InputStream。我试图为BufferedInputStream使用mark,但是当我尝试重置时,我有以下例外:

I am learning how to use an InputStream. I was trying to use mark for BufferedInputStream, but when I try to reset I have these exceptions:

java.io.IOException: Resetting to invalid mark

我认为这意味着我的标记读取限制设置错误。我实际上不知道如何设置mark()中的读取限制。我试过这样:

I think this means that my mark read limit is set wrong. I actually don't know how to set the read limit in mark(). I tried like this:

is = new BufferedInputStream(is);
is.mark(is.available());

这也是错误的。

is.mark(16);

这也会引发同样的异常。
我怎么知道我应该设置什么读取限制?因为我将从输入流中读取不同的文件大小。

This also throws the same exception. How do I know what read limit I am supposed to set? Since I will be reading different file sizes from the input stream.

推荐答案

标记有时候需要检查几个字节超出您的阅读以决定下一步该做什么,然后重置回标记并调用期望文件指针位于输入逻辑部分开头的例程。我不认为它真的是用于其他方面。

mark is sometimes useful if you need to inspect a few bytes beyond what you've read to decide what to do next, then you reset back to the mark and call the routine that expects the file pointer to be at the beginning of that logical part of the input. I don't think it is really intended for much else.

如果你看一下 BufferedInputStream 它说


标记操作会记住输入流中的一个点,并且复位操作会导致在从包含的输入流中获取新字节之前重新读取自最近标记操作以来读取的所有字节。

The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

这里要记住的关键是一旦你在流中标记一个点,如果你继续读取超过标记的长度,该标记将不再有效,并且重置调用将失败。因此标记适用于特定情况,在其他情况下使用不多。

The key thing to remember here is once you mark a spot in the stream, if you keep reading beyond the marked length, the mark will no longer be valid, and the call to reset will fail. So mark is good for specific situations and not much use in other cases.

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

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