1616
1717package org .intellij .erlang .utils ;
1818
19+ import com .intellij .openapi .util .text .StringUtil ;
1920import org .jetbrains .annotations .NotNull ;
2021
2122import java .io .BufferedReader ;
2223import java .io .IOException ;
24+ import java .io .InputStream ;
2325import java .io .InputStreamReader ;
2426import java .util .concurrent .*;
2527
@@ -28,21 +30,17 @@ private ExtProcessUtil() {
2830 }
2931
3032 @ NotNull
31- public static String restrictedTimeExec (@ NotNull String cmd , int timeout ) {
33+ public static ExtProcessOutput execAndGetFirstLine (@ NotNull String cmd , long timeout ) {
3234 try {
3335 final Process cmdRunner = Runtime .getRuntime ().exec (cmd );
3436 ExecutorService singleTreadExecutor = Executors .newSingleThreadExecutor ();
35- Future <String > cmdRunnerFuture = singleTreadExecutor .submit (new Callable <String >() {
37+ Future <ExtProcessOutput > cmdRunnerFuture = singleTreadExecutor .submit (new Callable <ExtProcessOutput >() {
3638 @ Override
37- public String call () throws Exception {
39+ public ExtProcessOutput call () throws Exception {
3840 cmdRunner .waitFor ();
39- BufferedReader outReader = new BufferedReader (new InputStreamReader (cmdRunner .getInputStream ()));
40- try {
41- String firstLine = outReader .readLine ();
42- return firstLine == null ? "" : firstLine ;
43- } finally {
44- outReader .close ();
45- }
41+ String stdOut = readLine (cmdRunner .getInputStream ());
42+ String stdErr = readLine (cmdRunner .getErrorStream ());
43+ return new ExtProcessOutput (stdOut , stdErr );
4644 }
4745 });
4846 try {
@@ -53,6 +51,41 @@ public String call() throws Exception {
5351 singleTreadExecutor .shutdown ();
5452 } catch (IOException e ) { // Suppress
5553 }
54+ return new ExtProcessOutput ("" , "" );
55+ }
56+
57+ @ NotNull
58+ private static String readLine (InputStream stream ) {
59+ BufferedReader errReader = new BufferedReader (new InputStreamReader (stream ));
60+ try {
61+ return StringUtil .notNullize (errReader .readLine ());
62+ } catch (IOException ignore ) {
63+ } finally {
64+ try {
65+ errReader .close ();
66+ } catch (IOException ignore ) {
67+ }
68+ }
5669 return "" ;
5770 }
71+
72+ public static class ExtProcessOutput {
73+ private final String myStdOut ;
74+ private final String myStdErr ;
75+
76+ public ExtProcessOutput (@ NotNull String stdOut , @ NotNull String stdErr ) {
77+ myStdOut = stdOut ;
78+ myStdErr = stdErr ;
79+ }
80+
81+ @ NotNull
82+ public String getStdOut () {
83+ return myStdOut ;
84+ }
85+
86+ @ NotNull
87+ public String getStdErr () {
88+ return myStdErr ;
89+ }
90+ }
5891}
0 commit comments