java输入流

发布于 2024-12-06 03:57:33 字数 94 浏览 0 评论 0 原文

当到达流末尾时,InputStreamavailable() 方法应该返回什么?

文档没有指定行为。

What is an InputStream's available() method is supposed to return when the end of the stream is reached?

The documentation doesn't specify the behavior.

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

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

发布评论

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

评论(5

空城之時有危險 2024-12-13 03:57:33

..已到达流末尾

不要使用 available() 来检测流结束!相反,请查看 InputStream.read(),其中:

如果由于已到达流末尾而没有可用字节,则返回值 -1。

..end of the stream is reached

Don't use available() for detecting end of stream! Instead look to the int returned by InputStream.read(), which:

If no byte is available because the end of the stream has been reached, the value -1 is returned.

陌伤浅笑 2024-12-13 03:57:33

JavaDoc 确实在“返回”部分告诉您 -

在到达输入流末尾时可以无阻塞地从此输入流读取(或跳过)的字节数的估计值或 0。 

(来自 InputStream JavaDoc)

The JavaDoc does tell you in the Returns section -

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream. 

(from InputStream JavaDoc)

别低头,皇冠会掉 2024-12-13 03:57:33

理论上,如果到达流末尾,则没有字节可供读取,并且 available 返回 0。但要小心。并非所有流都提供此方法的真正实现。 InputStream 本身总是返回 0。

如果您需要非阻塞功能,即从流中读取而不在读取时被阻塞,请改用 NIO。

Theoretically if end of stream is reached there are not bytes to read and available returns 0. But be careful with it. Not all streams provide real implementation of this method. InputStream itself always returns 0.

If you need non-blocking functionality, i.e. reading from stream without being blocked on read use NIO instead.

甜警司 2024-12-13 03:57:33

来自 Java 7 文档:
“在到达输入流末尾时可以无阻塞地读取(或跳过)此输入流的字节数的估计值或 0。”

所以,我想说在这种情况下它应该返回 0。对我来说,这似乎也是最直观的行为。

From the Java 7 documentation:
"an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream."

So, I would say it should return 0 in this case. That also seems the most intuitive behaviour to me.

轮廓§ 2024-12-13 03:57:33

返回可以从此输入流读取(或跳过)的字节数的估计值,而不会被该输入流的方法的下一次调用所阻塞。下一次调用可能是同一个线程或另一个线程。单次读取或跳过这么多字节不会阻塞,但可能会读取或跳过更少的字节。

The available method for class InputStream always returns 0. 

http://download.oracle .com/javase/6/docs/api/java/io/InputStream.html#available%28%29

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

The available method for class InputStream always returns 0. 

http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#available%28%29

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