为什么 InputStreamReader read() 会阻塞而不是返回 -1?

发布于 2024-12-28 03:24:33 字数 264 浏览 4 评论 0原文

我正在使用java的InputStreamReader read()函数。当我到达输入流的末尾时,我应该进入 int 变量的值 -1,但它却阻塞了。 为什么我在输入流末尾没有得到 -1? (我已经逐个字母地调试它,确保它实际上是输入的末尾并且连接套接字处于活动状态)。

使用ready()函数是一个很好的解决方案吗:

if (isr.ready())
    currCharVal = isr.read();

提前致谢, 盖伊。

I'm using java's InputStreamReader read() function. When I reach the end of the input stream i'm supposed to get in to my int variable the value of -1, but instead it goes to block.
Why don't I get a -1 at the end of the input stream? (i've debugged it letter by letter making sure it is actualy the end of the input and that the connection socket is alive).

Is using the ready() function a good solution by doing:

if (isr.ready())
    currCharVal = isr.read();

Thanks in advance,
Guy.

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

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

发布评论

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

评论(1

一江春梦 2025-01-04 03:24:33

如果另一端没有关闭连接,就会发生这种情况。当套接字关闭时,read() 将返回 -1。

根据我的经验,使用“ready”和“available”是相当不可预测的。我只会读取(byte[])直到到达末尾并期望另一端在完成后关闭。

This would happen if the other end is not closing the connection. When the socket is closed, read() will return a -1.

Using ready and available is rather unpredictable in my experience. I would just read(byte[]) until the end is reached and expect the other end to close when finished.

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