Java: use NIO with System.in [duplicate]
Asked Answered
S

3

6

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?

Schaaf answered 16/12, 2009 at 15:13 Comment(0)
U
-1

duplicate:

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)

Unblown answered 16/12, 2009 at 15:17 Comment(2)
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).Schaaf
This is a classic example of why answers that are primarily links are strongly discouraged on StackOverflow. The link to javafaq.nu is dead, so the answer is worthless.Revivalist
T
2

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);
Translator answered 16/12, 2009 at 15:27 Comment(0)
B
0

You could create a Pipe and a custom thread to read from standard input and write to the pipe.

Once you have the Pipe you can then get the input channel.

Bodoni answered 17/11, 2015 at 18:48 Comment(0)
U
-1

duplicate:

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)

Unblown answered 16/12, 2009 at 15:17 Comment(2)
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).Schaaf
This is a classic example of why answers that are primarily links are strongly discouraged on StackOverflow. The link to javafaq.nu is dead, so the answer is worthless.Revivalist

© 2022 - 2024 — McMap. All rights reserved.