为什么 InputStreamReader read() 会阻塞而不是返回 -1?
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果另一端没有关闭连接,就会发生这种情况。当套接字关闭时,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.