基本上,我有一个URL,当发布新消息时,该URL在聊天室中流xml更新。我想将URL转换为InputStream并继续读取它,只要保持连接并且没有发送Thread.interrupt()。我遇到的问题是,当有内容要从流中读取时,BufferedReader.ready()似乎不正确。
我正在使用以下代码:
BufferedReader buf = new BufferedReader(new InputStreamReader(ins));
String str = "";
while(Thread.interrupted() != true)
{
connected = true;
debug("Listening...");
if(buf.ready())
{
debug("Something to be read.");
if ((str = buf.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
urlContents += String.format("%s%n", str);
urlContents = filter(urlContents);
}
}
// Give the system a chance to buffer or interrupt.
try{Thread.sleep(1000);} catch(Exception ee) {debug("Caught thread exception.");}
}
当我运行代码并将某些内容发布到聊天室时,buf.ready()永远不会变为true,从而导致行永远不会被读取。但是,如果跳过“
buf.ready()”部分而直接读取行,则它将阻止进一步的操作,直到读取行。
我如何a)使buf.ready()返回true,或者b)以防止阻塞的方式执行此操作?
预先感谢,詹姆斯