This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Using Channels with the InputStream of a Process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Using Channels with the InputStream of a Process" Watch "Using Channels with the InputStream of a Process" New topic
Author

Using Channels with the InputStream of a Process

Seth Burleigh
Greenhorn

Joined: Jul 25, 2009
Posts: 7
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!)

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seth Burleigh
Greenhorn

Joined: Jul 25, 2009
Posts: 7
Thanks . thats what i was afraid of. My solution was to simply use Thread.sleep(100) after checking if nothing is available. In this instance, this delay won't make much a difference!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using Channels with the InputStream of a Process
 
Similar Threads
Establishing two-way buffered streams
Will these methods wait for input?
A challenge for all of you
Optimal I/O operations in Java
Locking files to prevent concurrent modification