Results 1 to 2 of 2
  1. #1
    alexant is offline Member
    Join Date
    Sep 2015
    Posts
    1
    Rep Power
    0

    Default Simultaneously stream data to/from a Process using stdin/stdout

    I have a command line tool that reads and writes data (images, not strings) using switches like this:

    $ command -i input_file -o output_file

    I need to send data to it from a stream, and receive the output also via a stream. So, the Java equivalent of this:

    $ command -i /dev/stdin -o /dev/stdout < input_file > output_file

    But I'm not clear on how to go about this.

    Here we go with an attempt:

    Java Code:
    ProcessBuilder pb = new ProcessBuilder(); // command setup omitted
    Process process = pb.start();
    // this is a FileInputStream for testing, but I will need it to be any kind of InputStream
    InputStream input = new FileInputStream(new File("some_file"));
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    
    process.waitFor();
    And here... I am stuck. I know I have to write to process.getOutputStream() which is the process' stdin. And I think I have to do that before process.waitFor(). But this causes a broken pipe IOException:

    Java Code:
    IOUtils.copy(input, process.getOutputStream());
    If I try to do it in a thread, I get another broken pipe:

    Java Code:
    new Thread(new StreamCopier(input, process.getOutputStream())).start();
    Maybe I need to write and read simultaneously:

    Java Code:
    new Thread(new StreamCopier(input, process.getOutputStream())).start();
    new Thread(new StreamCopier(process.getInputStream(), output)).start();
    I think I am making this more complicated than it is. Really, I just need to write a stream to my process' stdin, and have it write the output to a ByteArrayOutputStream. Then I can call toByteArray() on that to get the data.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Simultaneously stream data to/from a Process using stdin/stdout

    That soulnds like something way more complicated than anything I have ever tried, but are you aware of this classic writeup? When Runtime.exec() won't

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Data input stream behaving strangely
    By werner291 in forum New To Java
    Replies: 1
    Last Post: 01-01-2012, 06:39 PM
  2. Replies: 15
    Last Post: 04-12-2011, 03:42 PM
  3. Two external app. Redirect stdin to stdout.
    By alenyashka in forum Advanced Java
    Replies: 0
    Last Post: 08-18-2010, 09:26 AM
  4. Data Stream Mining
    By YNC in forum New To Java
    Replies: 3
    Last Post: 03-02-2010, 09:15 AM
  5. Make STDIN as a nonblocking mode
    By leslie in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 06:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •