views:

141

answers:

2

Is it possible to use NIO with System.in?

I would like to somehow treat 'stdin' as a selectable channel. Has anyone found a way to do this?

+1  A: 

duplicate:

http://stackoverflow.com/questions/765981/how-to-get-selectablechannel-from-an-inputstream

hmmm... on second thought, for stdin there may be a solution. I found this reference:

http://www.javafaq.nu/java-example-code-346.html

and in particular:

SystemInPipe.java (class which encapsulates stdin as a selectable channel)

Jason S
I appreciate what that guy is trying to do in SystemInPipe, but its still not much better than spawning a daemon thread to poll it. Problem is you can't interrupt that thread (ever).
Justin
A: 

I don't know about a SelectableChannel, but you can convert an InputStream to a ReadableByteChannel with

using java.nio.channels.Channels;

...

ReadableByteChannel in = Channels.newChannel(System.in);
R. Bemrose