< prev index next >
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java
Print this page
*** 247,268 ****
// Stream to copy from.
private final InputStream input;
// Stream to copy to.
private final OutputStream output;
Piper(final InputStream input, final OutputStream output) {
this.input = input;
this.output = output;
}
/**
* start - start the Piper in a new daemon thread
*/
! void start() {
! Thread thread = new Thread(this, "$EXEC Piper");
thread.setDaemon(true);
thread.start();
}
/**
* run - thread action
*/
--- 247,272 ----
// Stream to copy from.
private final InputStream input;
// Stream to copy to.
private final OutputStream output;
+ private final Thread thread;
+
Piper(final InputStream input, final OutputStream output) {
this.input = input;
this.output = output;
+ this.thread = new Thread(this, "$EXEC Piper");
}
/**
* start - start the Piper in a new daemon thread
+ * @return this Piper
*/
! Piper start() {
thread.setDaemon(true);
thread.start();
+ return this;
}
/**
* run - thread action
*/
*** 293,302 ****
--- 297,310 ----
// Don't care.
}
}
}
+ public void join() throws InterruptedException {
+ thread.join();
+ }
+
// Exit thread.
}
// Process exit statuses.
static final int EXIT_SUCCESS = 0;
*** 619,676 ****
// Prepare for string based i/o if no redirection or provided streams.
ByteArrayOutputStream byteOutputStream = null;
ByteArrayOutputStream byteErrorStream = null;
// If input is not redirected.
if (inputIsPipe) {
// If inputStream other than System.in is provided.
if (inputStream != null) {
// Pipe inputStream to first process output stream.
! new Piper(inputStream, firstProcess.getOutputStream()).start();
} else {
// Otherwise assume an input string has been provided.
! new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start();
}
}
// If output is not redirected.
if (outputIsPipe) {
// If outputStream other than System.out is provided.
if (outputStream != null ) {
// Pipe outputStream from last process input stream.
! new Piper(lastProcess.getInputStream(), outputStream).start();
} else {
// Otherwise assume an output string needs to be prepared.
byteOutputStream = new ByteArrayOutputStream(BUFFER_SIZE);
! new Piper(lastProcess.getInputStream(), byteOutputStream).start();
}
}
// If error is not redirected.
if (errorIsPipe) {
// If errorStream other than System.err is provided.
if (errorStream != null) {
! new Piper(lastProcess.getErrorStream(), errorStream).start();
} else {
// Otherwise assume an error string needs to be prepared.
byteErrorStream = new ByteArrayOutputStream(BUFFER_SIZE);
! new Piper(lastProcess.getErrorStream(), byteErrorStream).start();
}
}
// Pipe commands in between.
for (int i = 0, n = processes.size() - 1; i < n; i++) {
final Process prev = processes.get(i);
final Process next = processes.get(i + 1);
! new Piper(prev.getInputStream(), next.getOutputStream()).start();
}
// Wind up processes.
try {
// Get the user specified timeout.
! long timeout = envVarLongValue("JJS_TIMEOUT");
// If user specified timeout (milliseconds.)
if (timeout != 0) {
// Wait for last process, with timeout.
if (lastProcess.waitFor(timeout, TimeUnit.MILLISECONDS)) {
--- 627,686 ----
// Prepare for string based i/o if no redirection or provided streams.
ByteArrayOutputStream byteOutputStream = null;
ByteArrayOutputStream byteErrorStream = null;
+ final List<Piper> piperThreads = new ArrayList<>();
+
// If input is not redirected.
if (inputIsPipe) {
// If inputStream other than System.in is provided.
if (inputStream != null) {
// Pipe inputStream to first process output stream.
! piperThreads.add(new Piper(inputStream, firstProcess.getOutputStream()).start());
} else {
// Otherwise assume an input string has been provided.
! piperThreads.add(new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start());
}
}
// If output is not redirected.
if (outputIsPipe) {
// If outputStream other than System.out is provided.
if (outputStream != null ) {
// Pipe outputStream from last process input stream.
! piperThreads.add(new Piper(lastProcess.getInputStream(), outputStream).start());
} else {
// Otherwise assume an output string needs to be prepared.
byteOutputStream = new ByteArrayOutputStream(BUFFER_SIZE);
! piperThreads.add(new Piper(lastProcess.getInputStream(), byteOutputStream).start());
}
}
// If error is not redirected.
if (errorIsPipe) {
// If errorStream other than System.err is provided.
if (errorStream != null) {
! piperThreads.add(new Piper(lastProcess.getErrorStream(), errorStream).start());
} else {
// Otherwise assume an error string needs to be prepared.
byteErrorStream = new ByteArrayOutputStream(BUFFER_SIZE);
! piperThreads.add(new Piper(lastProcess.getErrorStream(), byteErrorStream).start());
}
}
// Pipe commands in between.
for (int i = 0, n = processes.size() - 1; i < n; i++) {
final Process prev = processes.get(i);
final Process next = processes.get(i + 1);
! piperThreads.add(new Piper(prev.getInputStream(), next.getOutputStream()).start());
}
// Wind up processes.
try {
// Get the user specified timeout.
! final long timeout = envVarLongValue("JJS_TIMEOUT");
// If user specified timeout (milliseconds.)
if (timeout != 0) {
// Wait for last process, with timeout.
if (lastProcess.waitFor(timeout, TimeUnit.MILLISECONDS)) {
*** 681,690 ****
--- 691,704 ----
}
} else {
// Wait for last process and get exit code.
exitCode = lastProcess.waitFor();
}
+ // Wait for all piper threads to terminate
+ for (final Piper piper : piperThreads) {
+ piper.join();
+ }
// Accumulate the output and error streams.
outputString += byteOutputStream != null ? byteOutputStream.toString() : "";
errorString += byteErrorStream != null ? byteErrorStream.toString() : "";
} catch (InterruptedException ex) {
< prev index next >