简体   繁体   English

为什么InputStreamReader是read()块而不是返回-1?

[英]Why does InputStreamReader read() block instead of returning -1?

I'm using java's InputStreamReader read() function. 我正在使用java的InputStreamReader read()函数。 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. 当我到达输入流的末尾时,我应该进入我的int变量-1的值,但它转到阻塞。 Why don't I get a -1 at the end of the input stream? 为什么我不在输入流的末尾得到-1? (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: 使用ready()函数是一个很好的解决方案:

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

Thanks in advance, Guy. 提前谢谢,盖伊。

This would happen if the other end is not closing the connection. 如果另一端未关闭连接,则会发生这种情况。 When the socket is closed, read() will return a -1. 当套接字关闭时,read()将返回-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. 我会读到(byte [])直到达到结束并期望另一端在完成时关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM