NIO and stdout

Discussion in 'Java' started by Horst Walter, Jun 3, 2004.

  1. Horst Walter

    Horst Walter Guest

    I want to run an external program (Runtime:exec()) and capture the
    output (System.out / System.err) by NIO.

    All examples I found on the web were about NIO and files and NIO and
    ByteBuffers. Is there no easy way to accomplish this? An example would
    really help.

    Thanks
    HW
    Horst Walter, Jun 3, 2004
    #1
    1. Advertising

  2. On 3 Jun 2004 11:15:22 -0700, Horst Walter wrote:
    > I want to run an external program (Runtime:exec()) and capture the
    > output (System.out / System.err) by NIO.
    >
    > All examples I found on the web were about NIO and files and NIO and
    > ByteBuffers. Is there no easy way to accomplish this? An example would
    > really help.


    I'm not sure that you can, there should be a method like
    InputStream.getChannel() otherwise. I don't think you can use NIO to
    read from the Java application's own stdin (System.in) either. Both
    should have been included, IMO.

    It seems like AbstractSelectableChannel could be subclassed, but I
    don't see how you would implement the selection operations on an
    existing InputStream.

    If you have an existing framework and want to use it to read input
    from the child process along with other things, then you could use a
    thread to read from the child process in the traditional way, and pass
    the data using a java.nio.channels.Pipe to the NIO selection loop.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jun 3, 2004
    #2
    1. Advertising

  3. Horst Walter

    Sudsy Guest

    Gordon Beaton wrote:
    <snip>
    > If you have an existing framework and want to use it to read input
    > from the child process along with other things, then you could use a
    > thread to read from the child process in the traditional way, and pass
    > the data using a java.nio.channels.Pipe to the NIO selection loop.


    Good call, Gordon. After digging into the javadocs, that seems like the
    right approach. Kind of annoying that you still have to fire up two
    extra threads, however.
    Sudsy, Jun 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    3
    Views:
    1,302
    Thomas Weidenfeller
    Jan 21, 2004
  2. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,226
    iksrazal
    Jun 18, 2004
  3. Elad
    Replies:
    0
    Views:
    403
  4. Andreas S
    Replies:
    3
    Views:
    246
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    319
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page