Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,23 @@ public static boolean externalCompile(List<File> srcFiles, File outdir, File[] c
StringBuilder errorBuffer = new StringBuilder();
StringBuilder outputBuffer = new StringBuilder();

Thread out = copy(proc.getInputStream(), outputBuffer);
Thread err = copy(proc.getErrorStream(), errorBuffer);

proc.waitFor();
try {
// the compiler reads nothing from stdin - leaving the pipe open just
// holds a file descriptor until the Process is collected
proc.getOutputStream().close();

Thread out = copy(proc.getInputStream(), outputBuffer);
Thread err = copy(proc.getErrorStream(), errorBuffer);

proc.waitFor();

// the readers can still be draining the pipes after the process exits,
// so join before reporting what they collected
out.join();
err.join();
} finally {
proc.destroy();
}

if (verbose || proc.exitValue() != 0) {
if (outputBuffer.length() > 0) {
Expand Down Expand Up @@ -347,9 +360,13 @@ private static File findJavaTool(String tool) {
*/
private static Thread copy(InputStream stream, final StringBuilder output) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset()));
Thread readerThread = new Thread(() ->
reader.lines().forEach(s -> output.append(s).append('\n'))
);
Thread readerThread = new Thread(() -> {
try (BufferedReader r = reader) {
r.lines().forEach(s -> output.append(s).append('\n'));
} catch (IOException e) {
// nothing useful to do with a failure to drain the pipe
}
});
readerThread.start();
return readerThread;
}
Expand Down
Loading