I'm programming a simple Java NIO server and have a little headache: I get normal InputStream
s i need to pipe to my clients. I have a single thread performing all writes, so this creates a problem: if the InputStream
blocks, all other connection writing will be paused.
I can use InputStream.available()
to check if there are any incoming data I can read without blocking, but if I've reached end-of-stream it seems I must call read()
to know.
This creates a major headache for me, but I can't possibly believe I'm the first to have this problem.
The only options I've come up with so far:
- Have a separate thread for each
InputStream
, however that's just silly since I'm using non-blocking I/O in the first place. I could also have a thread pool doing this but then again that limits the amount of simultaneous clients I can pipe theInputStream
to. - Have a separate thread reading these streams with a timeout (using another thread to interrupt if reading has lasted longer than a certain amount of time), but that'll most certainly choke the data flow should I have many open
InputStream
s not delivering data.
Of course, if there was a magic InputStream.isEof()
or isClosed()
then this wouldn't be any problem at all :'(