在这种情况下,为 BufferedInputStream 调用 available() 会导致我误入歧途吗?

发布于 2024-10-22 03:39:10 字数 827 浏览 1 评论 0原文

我正在以 1021 字节的块读取任意大小的文件,文件最后一个块的块大小 <= 1021 字节。目前,我正在使用 BufferedInputStream 来执行此操作,它包裹在 FileInputStream 周围,代码看起来(大致)如下所示(其中 reader > 是 BufferedInputStream 并且它在循环中运行):

int availableData = reader.available();
int datalen = (availableData >= 1021) 
                ? 1021 
                : availableData;
reader.read(bufferArray, 0, datalen);

但是,通过阅读 API 文档,我注意到 available() 只给出了可用的“估计”大小,在调用“阻塞”之前。每次迭代打印出 availableData 的值似乎给出了预期值 - 从文件大小开始,慢慢变小,直到 <= 1021。鉴于这是一个本地文件,我错了吗期望这是一个正确的值 - 是否存在 available() 会给出错误答案的情况?

编辑:抱歉,还有更多信息。 BufferedInputStream 包裹在 FileInputStream 周围。从 FIS 的源代码来看,我认为我可以安全地依赖 available() 作为本地文件中剩余数据量的衡量标准。我说得对吗?

I am reading in arbitrary size file in blocks of 1021 bytes, with a block size of <= 1021 bytes for the final block of the file. At the moment, I am doing this using a BufferedInputStream which is wrapped around a FileInputStream and code that looks (roughly) like the following (where reader is the BufferedInputStream and this is operating in a loop):

int availableData = reader.available();
int datalen = (availableData >= 1021) 
                ? 1021 
                : availableData;
reader.read(bufferArray, 0, datalen);

However, from reading the API docs, I note that available() only gives an "estimate" of the available size, before the call would 'block'. Printing out the value of availableData each iteration seems to give the expected values - starting with the file size and slowly getting less until it is <= 1021. Given that this is a local file, am I wrong to expect this to be a correct value - is there a situation where available() would give an incorrect answer?

EDIT: Sorry, additional information. The BufferedInputStream is wrapped around a FileInputStream. From the source code for a FIS, I think I'm safe to rely on available() as a measure of how much data is left in the case of a local file. Am I right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§对你不离不弃 2024-10-29 03:39:10

这个问题毫无意义。这四行代码与此完全等效:

reader.read(buffer, 0, 1021);

没有在 available() 调用和读取之间引入的计时窗口问题。请注意,此代码仍然不正确,因为您忽略了返回值,在 EOS 上该返回值可以是 -1,也可以是 1 到 1021 之间的任何值。

The question is pointless. Those four lines of code are entirely equivalent to this:

reader.read(buffer, 0, 1021);

without the timing-window problem you have introduced between the available() call and the read. Note that this code is still incorrect as you are ignoring the return value, which can be -1 at EOS, or else anything between 1 and 1021 inclusive.

一花一树开 2024-10-29 03:39:10

它不给出估计的大小,它给出可以读取的剩余字节。这不是使用 BufferedInputStream 进行的估计。

返回可容纳的字节数
从此输入流中读取,无需
阻塞。

如果您想避免阻塞,您应该将 available() 直接传递到 read() 调用中,但请记住,如果返回值为 0 或 -1,则返回。 available() 可能会对不支持该操作的缓冲区类型引发异常。

It doesn't give the estimated size, it gives the remaining bytes that can be read. It's not an estimate with BufferedInputStream.

Returns the number of bytes that can
be read from this input stream without
blocking.

You should pass available() directly into the read() call if you want to avoid blocking, but remember to return if the return value is 0 or -1. available() might throw an exception on buffer types that don't support the operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文