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

前端 未结 5 1956
时光取名叫无心
时光取名叫无心 2021-01-02 15:44

Basically, I have a URL that streams xml updates from a chat room when new messages are posted. I\'d like to turn that URL into an InputStream and continue reading from it

5条回答
  •  暖寄归人
    2021-01-02 16:31

    How to create a Java non-blocking InputStream

    You can't. Your question embodies a contradiciton in terms. Streams in Java are blocking. There is therefore no such thing as a 'non-blocking InputStream'.

    Reader.ready() returns true when data can be read without blocking. Period. InputStreams and Readers are blocking. Period. Everything here is working as designed. If you want more concurrency with these APIs you will have to use multiple threads. Or Socket.setSoTimeout() and its near relation in HttpURLConnection.

提交回复
热议问题