0

I am having trouble getting the output of a subprocess. Usually I would use process.getInputStream(). However, in this case, I set the subprocess to inherit from the parent's IO. I need to do this because the output from the subprocess exceeds the buffer set by the kernel and thus will hang. I am using Linux btw.

    //creating a named pipe
    File fifo = fifoCreator.createFifoPipe("fifo");
    String[] command = new String[] {"cat", fifo.getAbsolutePath()};
    process = new ProcessBuilder(command).inheritIO().start(); //Inherit IO

    FileWriter fw = new FileWriter(fifo.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(boxString);
    bw.close();
    process.waitFor();
    fifoCreator.removeFifoPipe(fifo.toString());

    //Gets nothing here
    stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String input = readAllLines(stdInput);

Ideally I want to keep everything in memory since I want good performance and less trash.

Is there a way to redirect the output of the subprocess into some data structure? If not, is there any way to get the output?

I am thinking of creating a separate thread that gets the output stream.

mrQWERTY
  • 4,039
  • 13
  • 43
  • 91

1 Answers1

2

To avoid the kernel buffer problem you could use a thread to read the output as in this question. They call it StreamGobbler

Community
  • 1
  • 1
krico
  • 5,723
  • 2
  • 25
  • 28