How do I detect end of stream in a non-blocking manner when reading a stream through URL/URLConnection, if available() reports nothing to read on end of stream?

John Mitchell



In the fully general case, you can't (since the client can just keep sending you data). Given that Java doesn't have asynchronous I/O (yet), you need to design your system to take into account the potentially blocking nature of the I/O. Also, if you do the I/O in a separate thread then you also have to deal with the issue of properly and safely being able to stop your thread if the I/O fails.

In terms of doing URL stuff, I suggest that you look at the HTTPClient package.

In terms of threading, you should definitely look at Doug Lea's util.concurrent package. Also, of course, check out the jGuru Threads FAQ for more information about threads.

Java 1.4 supports non-blocking I/O now.