99
1010import java .io .BufferedReader ;
1111import java .io .IOException ;
12+ import java .io .InputStream ;
1213import java .io .InputStreamReader ;
1314import java .util .List ;
1415
1516import com .google .gson .Gson ;
1617import com .google .gson .GsonBuilder ;
1718
1819import multichain .command .MultichainException ;
20+ import multichain .object .Stream ;
1921
2022/**
2123 * @author Ub - H. MARTEAU
@@ -138,11 +140,11 @@ private static String removeHeader(String result) {
138140 * @throws MultichainException
139141 */
140142 protected static String execute (CommandEnum command , String ... parameters ) throws MultichainException {
141- BufferedReader stdError = null ;
143+
142144 if (!CHAIN .equals ("" )) {
143145 Runtime rt = Runtime .getRuntime ();
144146 Process pr ;
145- String result = "" ;
147+
146148 try {
147149 if (parameters .length > 0 ) {
148150 String params = "" ;
@@ -153,34 +155,22 @@ protected static String execute(CommandEnum command, String... parameters) throw
153155 } else {
154156 pr = rt .exec ("multichain-cli " + CHAIN + " " + command .toString ().toLowerCase ());
155157 }
158+ //Get the output from both error stream and output stream
159+ StreamGobbler errorGobbler = new StreamGobbler (pr .getErrorStream ());
160+ StreamGobbler outputGobbler = new StreamGobbler (pr .getInputStream ());
156161
157- BufferedReader stdInput = new BufferedReader (new InputStreamReader (pr .getInputStream ()));
162+ errorGobbler .start ();
163+ outputGobbler .start ();
158164
159- stdError = new BufferedReader ( new InputStreamReader ( pr .getErrorStream ()) );
165+ pr .waitFor ( );
160166
161- // read the output from the command
162- String s ;
163- while ((s = stdInput .readLine ()) != null ) {
164- result = result .concat (s + "\n " );
165- }
166- } catch (IOException e ) {
167- // TODO Auto-generated catch block
168- e .printStackTrace ();
169- }
167+ if (outputGobbler .output .length () > 0 )
168+ return outputGobbler .output ;
170169
171- if (!result .isEmpty () && !result .equalsIgnoreCase ("" )) {
172- return result ;
173- } else {
174- // read any errors from the attempted command
175- String s ;
176- try {
177- while ((s = stdError .readLine ()) != null ) {
178- result = result .concat (s + "\n " );
179- }
180- } catch (IOException e ) {
181- e .printStackTrace ();
182- }
183- throw new MultichainException (null , result );
170+ return errorGobbler .output ;
171+ } catch (IOException | InterruptedException e ) {
172+ e .printStackTrace ();
173+ return "" ;
184174 }
185175 } else {
186176 return "ERROR, CHAIN NAME ARE EMPTY !" ;
@@ -291,5 +281,24 @@ protected static void setCHAIN(String cHAIN) {
291281 CHAIN = cHAIN ;
292282 }
293283
284+ static class StreamGobbler extends Thread {
285+ private InputStream is ;
286+ private String output = "" ;
287+
288+ StreamGobbler (InputStream is ) {
289+ this .is = is ;
290+ }
291+
292+ public void run () {
293+ try {
294+ BufferedReader br = new BufferedReader (new InputStreamReader (is ));
295+ String line ;
296+ while ( (line = br .readLine ()) != null )
297+ output = output .concat (line + "\n " );
298+ } catch (IOException ioe ) {
299+ ioe .printStackTrace ();
300+ }
301+ }
302+ }
294303
295304}
0 commit comments