| 
15 | 15 |  */  | 
16 | 16 | package com.diffplug.gradle;  | 
17 | 17 | 
 
  | 
18 |  | -import java.io.BufferedReader;  | 
19 | 18 | import java.io.File;  | 
20 | 19 | import java.io.IOException;  | 
21 |  | -import java.io.InputStreamReader;  | 
22 |  | -import java.nio.charset.Charset;  | 
23 | 20 | import java.util.Arrays;  | 
24 | 21 | import java.util.List;  | 
25 | 22 | 
 
  | 
 | 23 | +import javax.management.RuntimeErrorException;  | 
 | 24 | + | 
26 | 25 | import org.apache.commons.io.FileUtils;  | 
27 | 26 | 
 
  | 
28 | 27 | import com.diffplug.common.base.Throwing;  | 
@@ -143,42 +142,32 @@ public static Result runCmd(File directory, String cmd, boolean echoCmd, boolean  | 
143 | 142 | 		Process process = builder.start();  | 
144 | 143 | 
 
  | 
145 | 144 | 		// wrap the process' input and output  | 
146 |  | -		try (  | 
147 |  | -				BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset()));  | 
148 |  | -				BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset()));) {  | 
149 |  | - | 
 | 145 | +		try {  | 
150 | 146 | 			if (echoCmd) {  | 
151 | 147 | 				System.out.println("cmd>" + cmd);  | 
152 | 148 | 			}  | 
153 | 149 | 
 
  | 
154 |  | -			// dump the output  | 
155 |  | -			ImmutableList.Builder<String> output = ImmutableList.builder();  | 
156 |  | -			ImmutableList.Builder<String> error = ImmutableList.builder();  | 
157 |  | - | 
158 |  | -			String line = null;  | 
159 |  | -			while ((line = stdInput.readLine()) != null) {  | 
160 |  | -				output.add(line);  | 
161 |  | -				if (echoOutput) {  | 
162 |  | -					System.out.println(line);  | 
163 |  | -				}  | 
164 |  | -			}  | 
165 |  | - | 
166 |  | -			// dump the input  | 
167 |  | -			while ((line = stdError.readLine()) != null) {  | 
168 |  | -				error.add(line);  | 
169 |  | -				if (echoOutput) {  | 
170 |  | -					System.err.println(line);  | 
171 |  | -				}  | 
172 |  | -			}  | 
 | 150 | +			InputStreamCollector stdInputThread = new InputStreamCollector(process.getInputStream(), echoOutput ? System.out : null, null);  | 
 | 151 | +			stdInputThread.start();  | 
 | 152 | +			InputStreamCollector stdErrorThread = new InputStreamCollector(process.getErrorStream(), echoOutput ? System.err : null, null);  | 
 | 153 | +			stdErrorThread.start();  | 
173 | 154 | 
 
  | 
174 | 155 | 			// check that the process exited correctly  | 
175 | 156 | 			int exitValue = process.waitFor();  | 
176 |  | -			if (exitValue != EXIT_VALUE_SUCCESS) {  | 
 | 157 | +			// then wait for threads collecting the output of thread  | 
 | 158 | +			stdInputThread.join();  | 
 | 159 | +			stdErrorThread.join();  | 
 | 160 | + | 
 | 161 | +			if (stdInputThread.getException() != null) {  | 
 | 162 | +				throw new RuntimeException(stdInputThread.getException());  | 
 | 163 | +			} else if (stdErrorThread.getException() != null) {  | 
 | 164 | +				throw new RuntimeException(stdErrorThread.getException());  | 
 | 165 | +			} else if (exitValue != EXIT_VALUE_SUCCESS) {  | 
177 | 166 | 				throw new RuntimeException("'" + cmd + "' exited with " + exitValue);  | 
178 | 167 | 			}  | 
179 | 168 | 
 
  | 
180 | 169 | 			// returns the result of this successful execution  | 
181 |  | -			return new Result(directory, cmd, output.build(), error.build());  | 
 | 170 | +			return new Result(directory, cmd, stdInputThread.getOutput(), stdErrorThread.getOutput());  | 
182 | 171 | 		} catch (InterruptedException e) {  | 
183 | 172 | 			// this isn't expected, but it's possible  | 
184 | 173 | 			throw new RuntimeException(e);  | 
 | 
0 commit comments