How to create a Java non-blocking InputStream from a HttpsURLConnection? -


basically, have url streams xml updates chat room when new messages posted. i'd turn url inputstream , continue reading long connection maintained , long haven't sent thread.interrupt(). problem i'm experiencing bufferedreader.ready() doesn't seem become true when there content read stream.

i'm using following code:

bufferedreader buf = new bufferedreader(new inputstreamreader(ins));   string str = ""; while(thread.interrupted() != true) {     connected = true;     debug("listening...");      if(buf.ready())     {         debug("something read.");         if ((str = buf.readline()) != null) {             // str 1 line of text; readline() strips newline character(s)             urlcontents += string.format("%s%n", str);             urlcontents = filter(urlcontents);         }     }      // give system chance buffer or interrupt.     try{thread.sleep(1000);} catch(exception ee) {debug("caught thread exception.");} } 

when run code, , post chat room, buf.ready() never becomes true, resulting in lines never being read. however, if skip "buf.ready()" part , read lines directly, blocks further action until lines read.

how either a) buf.ready() return true, or b) in such way prevent blocking?

thanks in advance, james

reader.ready() returns true when data can read without blocking. period. inputstreams , readers blocking. period. here working designed. if want more concurrency these apis have use multiple threads. or socket.setsotimeout() , near relation in httpurlconnection.


Comments

Popular posts from this blog

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -

java - Getting corefrences with Standard corenlp package -

Java - Returning an array from a method to main -