Skip to content
Snippets Groups Projects
Commit 41987c9a authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

read stdout/stderr at the same time if error output is not being redirected

parent f69f7e91
No related branches found
No related tags found
No related merge requests found
......@@ -2,17 +2,21 @@ package de.uni_passau.fim.seibt.gitwrapper.process;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import static de.uni_passau.fim.seibt.gitwrapper.repo.GitWrapper.EXIT_FAIL;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.Level.WARNING;
/**
......@@ -21,6 +25,7 @@ import static java.util.logging.Level.WARNING;
public class ProcessExecutor {
private static final Logger LOG = Logger.getLogger(ProcessExecutor.class.getCanonicalName());
private static final ExecutorService exService = Executors.newCachedThreadPool();
/**
* The result of a process execution.
......@@ -78,8 +83,35 @@ public class ProcessExecutor {
try {
Process p = builder.start();
String stdOut = IOUtils.toString(p.getInputStream(), StandardCharsets.UTF_8).trim();
String stdErr = IOUtils.toString(p.getErrorStream(), StandardCharsets.UTF_8).trim();
String stdOut;
String stdErr;
if (builder.redirectErrorStream()) {
stdOut = IOUtils.toString(p.getInputStream(), UTF_8).trim();
stdErr = IOUtils.toString(p.getErrorStream(), UTF_8).trim();
} else {
Future<String> outF = exService.submit(() -> IOUtils.toString(p.getInputStream(), UTF_8).trim());
Future<String> errF = exService.submit(() -> IOUtils.toString(p.getErrorStream(), UTF_8).trim());
try {
stdOut = outF.get();
stdErr = errF.get();
} catch (InterruptedException e) {
LOG.log(WARNING, e, () -> String.format("Interrupted while waiting for '%s' to finish.", cmd));
outF.cancel(true);
errF.cancel(true);
p.destroyForcibly();
return Optional.empty();
} catch (ExecutionException e) {
LOG.log(WARNING, e, () -> String.format("Exception while reading the output of '%s'.", cmd));
outF.cancel(true);
errF.cancel(true);
p.destroyForcibly();
return Optional.empty();
}
}
IOUtils.closeQuietly(p.getInputStream());
IOUtils.closeQuietly(p.getErrorStream());
......@@ -93,7 +125,7 @@ public class ProcessExecutor {
LOG.log(WARNING, e, () -> String.format("Interrupted while waiting for '%s' to finish.", cmd));
p.destroyForcibly();
exitCode = EXIT_FAIL;
return Optional.empty();
}
ExecRes res = new ExecRes(exitCode, stdOut, stdErr);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment