8151515: $EXEC output is truncated
authorhannesw
Wed, 09 Mar 2016 15:45:44 +0100
changeset 36481 9826c19a5310
parent 36480 1a873ecd2249
child 36482 ee5a7cd1e795
8151515: $EXEC output is truncated Reviewed-by: sundar, jlaskey
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java	Wed Mar 09 15:15:29 2016 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java	Wed Mar 09 15:45:44 2016 +0100
@@ -249,18 +249,22 @@
         // 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
          */
-        void start() {
-            Thread thread = new Thread(this, "$EXEC Piper");
+        Piper start() {
             thread.setDaemon(true);
             thread.start();
+            return this;
         }
 
         /**
@@ -295,6 +299,10 @@
             }
         }
 
+        public void join() throws InterruptedException {
+            thread.join();
+        }
+
         // Exit thread.
     }
 
@@ -621,15 +629,17 @@
         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.
-                new Piper(inputStream, firstProcess.getOutputStream()).start();
+                piperThreads.add(new Piper(inputStream, firstProcess.getOutputStream()).start());
             } else {
                 // Otherwise assume an input string has been provided.
-                new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start();
+                piperThreads.add(new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start());
             }
         }
 
@@ -638,11 +648,11 @@
             // If outputStream other than System.out is provided.
             if (outputStream != null ) {
                 // Pipe outputStream from last process input stream.
-                new Piper(lastProcess.getInputStream(), outputStream).start();
+                piperThreads.add(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();
+                piperThreads.add(new Piper(lastProcess.getInputStream(), byteOutputStream).start());
             }
         }
 
@@ -650,11 +660,11 @@
         if (errorIsPipe) {
             // If errorStream other than System.err is provided.
             if (errorStream != null) {
-                new Piper(lastProcess.getErrorStream(), errorStream).start();
+                piperThreads.add(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();
+                piperThreads.add(new Piper(lastProcess.getErrorStream(), byteErrorStream).start());
             }
         }
 
@@ -662,13 +672,13 @@
         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();
+            piperThreads.add(new Piper(prev.getInputStream(), next.getOutputStream()).start());
         }
 
         // Wind up processes.
         try {
             // Get the user specified timeout.
-            long timeout = envVarLongValue("JJS_TIMEOUT");
+            final long timeout = envVarLongValue("JJS_TIMEOUT");
 
             // If user specified timeout (milliseconds.)
             if (timeout != 0) {
@@ -683,6 +693,10 @@
                 // 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() : "";