Apache Commons exec PumpStreamHandler continuous input

1.2k Views Asked by At

I am trying to solve interaction with command line process using Apache Commons exec. I'm stuck with following code:

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream ins = new ByteArrayOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(ins);
BufferedWriter writer = new BufferedWriter(ow);
ByteArrayInputStream in = new ByteArrayInputStream(ins.toByteArray());
PumpStreamHandler psh = new PumpStreamHandler(out, null, in);
CommandLine cl = CommandLine.parse(initProcess);
DefaultExecutor exec = new DefaultExecutor();
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
exec.setStreamHandler(psh);
try {
    exec.execute(cl, resultHandler);
    int i = 0;
    while (true) {
        String o = out.toString();
        if (!o.trim().isEmpty()) {
            System.out.println(o);
            out.reset();
        }
        // --- PROBLEM start ---
        if (i == 3) {
            writer.write(internalProcessCommand); 
            // string with or without trailing \n, both tested
            writer.flush();
            writer.close();
            // tested even ins.write(internalProcessCommand.getBytes())
        }
        // --- PROBLEM end ---
        Thread.sleep(3000);
        i++;
    }
} catch (ExecuteException e) {
    System.err.println(e.getMessage());
}

I hope my code is clear. I continuously read out and print it after 3 seconds while clearing stream. Problem is input to in passed to PumpStreamHandler. I need to pass process commands from code itself, continuously and dynamically as if I was interacting with process through CLI. When I simply use System.in as PumpStreamHandler argument, I can write process commands from console fine. How can I manage to have same result passing strings from code?

Edit: I also tried to connect PipedInputStream receiving data from PipedOutputStream, but it seems that data can be read only after closing PipedOutputStream which makes it un-reusable thus I can't achieve interactivity.

Edit 2: Solved myself. Solution in answer below. Howgh. :-)

3

There are 3 best solutions below

0
On BEST ANSWER

Okay, I solved this using built-in tools rather than external libraries. I was able to achieve my goal thanks to independant thread which reads Process' InputStream:

ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
StreamReader outputReader = new StreamReader(process.getInputStream(), System.out);
outputReader.start();
StreamReader err = new StreamReader(process.getErrorStream(), System.err);
err.start();

for (int i = 0; i < 3; i++) {
    Thread.sleep(5000);
    writer.write(internalProcessCommand + "\n");
    writer.flush();
}
writer.write("exit\n");
writer.flush();

while (process.isAlive()) {
    System.out.println("alive?");
    Thread.sleep(100);
}
System.out.println("dead");
outputReader.shutdown();
err.shutdown();

StreamReader:

class StreamReader extends Thread {

    private AtomicBoolean running = new AtomicBoolean(false);
    private InputStream in;
    private OutputStream out;

    public StreamReader(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
        running.set(true);
    }

    @Override
    public void run() {
        Scanner scanner = new Scanner(in);
        PrintWriter writer = new PrintWriter(out, true);
        while (running.get()) {
            if (scanner.hasNextLine()) {
                writer.println(scanner.nextLine());
            }
        }
        scanner.close();
    }

    public void shutdown() {
        running.set(false);
    }

}
0
On

Based on the answer given by @champagniac, I have created a simple fix which introduces additional flushing by only replacing the PumpStreamHandler:

public class PumpStreamHandlerFixed extends PumpStreamHandler
{
  public PumpStreamHandlerFixed()
  {
    super();
  }

  public PumpStreamHandlerFixed(OutputStream out, OutputStream err, InputStream input)
  {
    super(out, err, input);
  }

  public PumpStreamHandlerFixed(OutputStream out, OutputStream err)
  {
    super(out, err);
  }

  public PumpStreamHandlerFixed(OutputStream outAndErr)
  {
    super(outAndErr);
  }

  @Override
  protected Thread createPump(InputStream is, OutputStream os, boolean closeWhenExhausted)
  {
    os = new AutoFlushingOutputStream(os);

    final Thread result = new Thread(new StreamPumper(is, os, closeWhenExhausted), "Exec Stream Pumper");
    result.setDaemon(true);
    return result;
  }
}

class AutoFlushingOutputStream extends OutputStream
{
  private final OutputStream decorated;

  public AutoFlushingOutputStream(OutputStream decorated)
  {
    this.decorated = decorated;
  }

  @Override
  public void write(byte[] b, int off, int len) throws IOException
  {
    this.decorated.write(b, off, len);
    this.decorated.flush();
  }

  @Override
  public void write(int b) throws IOException
  {
    this.decorated.write(b);
    this.decorated.flush();
  }

  @Override
  public void close() throws IOException
  {
    this.decorated.close();
  }

  @Override
  public void flush() throws IOException
  {
    this.decorated.flush();
  }
}
0
On

A solution is to copy the implementation of PumpStreamHandler and StreamPumper, to let say ImmediatePumpStreamHandler and ImmediateStreamPumper, and make the following two changes:

  • Add os.flush(); directly after os.write(buf, 0, length); on line 108 in ImmediateStreamPumper.
  • Change new StreamPumper(...) to new ImmediateStreamPumper(...) on line 269 in ImmediatePumpStreamHandler.