running.add(this);
} catch (IOException e) {
String message = String.format("Unable to start command: %s\nReason: %s",
StringUtils.join(builder.command(), " "),
e.getMessage());
throw new ReviewedGATKException(message);
}
int exitCode;
try {
// Notify the background threads to start capturing.
synchronized (toCapture) {
toCapture.put(ProcessStream.Stdout,
new CapturedStreamOutput(settings.getStdoutSettings(), process.getInputStream(), System.out));
toCapture.put(ProcessStream.Stderr,
new CapturedStreamOutput(settings.getStderrSettings(), process.getErrorStream(), System.err));
toCapture.notifyAll();
}
// Write stdin content
InputStreamSettings stdinSettings = settings.getStdinSettings();
Set<StreamLocation> streamLocations = stdinSettings.getStreamLocations();
if (!streamLocations.isEmpty()) {
try {
OutputStream stdinStream = process.getOutputStream();
for (StreamLocation location : streamLocations) {
InputStream inputStream;
switch (location) {
case Buffer:
inputStream = new ByteArrayInputStream(stdinSettings.getInputBuffer());
break;
case File:
try {
inputStream = FileUtils.openInputStream(stdinSettings.getInputFile());
} catch (IOException e) {
throw new UserException.BadInput(e.getMessage());
}
break;
case Standard:
inputStream = System.in;
break;
default:
throw new ReviewedGATKException("Unexpected stream location: " + location);
}
try {
IOUtils.copy(inputStream, stdinStream);
} finally {
if (location != StreamLocation.Standard)
IOUtils.closeQuietly(inputStream);
}
}
stdinStream.flush();
} catch (IOException e) {
throw new ReviewedGATKException("Error writing to stdin on command: " + StringUtils.join(builder.command(), " "), e);
}
}
// Wait for the process to complete.
try {
process.getOutputStream().close();
process.waitFor();
} catch (IOException e) {
throw new ReviewedGATKException("Unable to close stdin on command: " + StringUtils.join(builder.command(), " "), e);
} catch (InterruptedException e) {
throw new ReviewedGATKException("Process interrupted", e);
} finally {
while (!destroyed && stdout == null || stderr == null) {
synchronized (fromCapture) {
if (fromCapture.containsKey(ProcessStream.Stdout))
stdout = fromCapture.remove(ProcessStream.Stdout);