I posted the same question here a few days ago ( Java reading standard output from an external program using an input stream ), and I found some great tips in solving with a block while reading (while (is.read ())! = -1)), but I still can not solve the problem.
After reading the answers to this similar question,
Reading InputStream input lock (esp, response sent by Guss),
I am starting to believe that a cyclic input stream using is.read ()! = -1 condition does not work if the program is interactive (that is, it accepts several inputs from the user and presents additional outputs on subsequent inputs, and the program exits only when explicit exit command is given). I admit that I donβt know much about multithreading, but I think I need a mechanism to quickly suspend input streams (one for stdout, stderr one) when user input is required, and resume work after input to prevent blocking. The following is my current code that is testing a block on the specified line:
EGMProcess egm = new EGMProcess (new String [] {directory + "/ egm", "-o",
"CasinoA", "-v", "VendorA", "-s", "localhost: 8080 / gls / MessageRobot.action",
"-E", "glss_env_cert.pem", "-S", "glss_sig_cert.pem", "-C", "glsc_sig_cert.pem",
"-d", "config", "-L", "config / log.txt", "-H", "GLSA-SampleHost"}, new String [] {"PATH = $ {PATH}"}, directory );
egm.execute(); BufferedReader stdout = new BufferedReader(new InputStreamReader(egm.getInputStream())); BufferedReader stderr = new BufferedReader(new InputStreamReader(egm.getErrorStream())); EGMStreamGobbler stdoutprocessor = new EGMStreamGobbler(stdout, egm); EGMStreamGobbler stderrprocessor = new EGMStreamGobbler(stderr, egm); BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(egm.getOutputStream())); stderrprocessor.run(); //<-- the block occurs here! stdoutprocessor.run(); //EGM/Agent test cases //check bootstrap menu if(!checkSimpleResult("******** EGM Bootstrap Menu **********", egm)) { String stdoutdump = egm.getStdOut(); egm.cleanup(); throw new Exception("can't find '******** EGM Bootstrap Menu **********'" + "in the stdout" + "\nStandard Output Dump:\n" + stdoutdump); } //select bootstrap stdin.write("1".toCharArray()); stdin.flush(); if(!checkSimpleResult("Enter port to receive msgs pushed from server ('0' for no push support)", egm)){ String stdoutdump = egm.getStdOut(); egm.cleanup(); throw new Exception("can't find 'Enter port to receive msgs pushed from server ('0' for no push support)'" + "in the stdout" + "\nStandard Output Dump:\n" + stdoutdump); }
...
public class EGMStreamGobbler implements Runnable {
private BufferedReader instream; private EGMProcess egm; public EGMStreamGobbler (BufferedReader isr, EGMProcess aEGM) {instream = isr; egm = aEGM; } public void run () {try {int c; while ((c = instream.read ())! = 1) {egm.processStdOutStream ((char) c); }} catch (IOException e) {e.printStackTrace (); }}
}
Sorry for the length of the code, but my questions are:
1) Is there a way to control the input process (stdout, stderr) without using read ()? Or am I just doing it poorly?
2) Is multithreading the right strategy for developing an input process and writing output?
PS: if someone can provide a similar problem with the solution, this will help me a lot!