InputStream.available() 不起作用
我正在尝试使用 inputstream.available () 来检查是否有任何数据要读取而不阻塞线程。但它永远不会返回任何值> 0.我用错了吗?
while (slept < logOnTimeOut) {
if ( sslSocket.getInputStream().available() > 0 ) {
if (input.readLine().equals("OK") ) { // todo: set timeout here
System.out.println("Successfully Logged On");
isLoggedOn = true;
return true;
}
} else {
Thread.sleep(500);
slept += 500;
}
}
I am trying to use inputstream.available () to check if there is any data to read without blocking the thread. but it never return any value > 0. am I using it wrong?
while (slept < logOnTimeOut) {
if ( sslSocket.getInputStream().available() > 0 ) {
if (input.readLine().equals("OK") ) { // todo: set timeout here
System.out.println("Successfully Logged On");
isLoggedOn = true;
return true;
}
} else {
Thread.sleep(500);
slept += 500;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读 javadoc:
简而言之,
InputStream.available()
并不像您想象的那么有用。如果您需要检测流的结尾,请从中读取(read())并检测结果是否为
-1
。不要使用available()
。Read the javadoc:
In short,
InputStream.available()
is not half as useful as you think it is.If you need to detect the end of the stream,
read()
from it and it detect if the result is-1
. Do not useavailable()
.