This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm attempting to use a channel with the input stream of a process. I want this to be non-blocking. However, no matter what, it does block once its ready everything available.
Channels.newChannel(process.getInputStream()).read(byteBuffer) -> stalls if everything read!!
Is there any way to make it nonstalling? What im trying to do is create a thread which analyzes a stream for a certain string. I also want to be able to kill it from the outside whenever. The best solution i guess would probably be to use a block InputStream read by using the amount of bytes available. If there is 0, then pause maybe .1 seconds to avoid killing the cpu and checking again. However, I would be interesting in asynchrous io which is possible with Selectors and also interested in io that can be interrupted (as should be the case when using channels!)
Seth Burleigh wrote:Is there any way to make it nonstalling? [...] However, I would be interesting in asynchrous io which is possible with Selectors and also interested in io that can be interrupted (as should be the case when using channels!)
You said it yourself; using Selectors is the only way to make I/O non-blocking. That will only work with channels that subclass SelectableChannel though, and Channel returned by Channels.newChannel isn't such a subclass. Although process streams are in fact selectable on the operating system level, they are not in Java. As such, non-blocking I/O using Selectors is not possible for your situation.
Of course you can try to create your own selection code using a thread, but yeah, it's a bit harder.