< prev index next >

test/lib/jdk/test/lib/process/OutputBuffer.java

Print this page
rev 51638 : [mq]: 8210112

@@ -21,39 +21,124 @@
  * questions.
  */
 
 package jdk.test.lib.process;
 
-public class OutputBuffer {
-  private final String stdout;
-  private final String stderr;
+import java.io.ByteArrayOutputStream;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 
-  /**
-   * Create an OutputBuffer, a class for storing and managing stdout and stderr
-   * results separately
-   *
-   * @param stdout stdout result
-   * @param stderr stderr result
-   */
-  public OutputBuffer(String stdout, String stderr) {
-    this.stdout = stdout;
-    this.stderr = stderr;
+public interface OutputBuffer {
+  public static class OutputBufferException extends RuntimeException {
+    private static final long serialVersionUID = 8528687792643129571L;
+
+    public OutputBufferException(Throwable cause) {
+      super(cause);
+    }
   }
 
   /**
    * Returns the stdout result
    *
    * @return stdout result
    */
-  public String getStdout() {
-    return stdout;
-  }
-
+  public String getStdout();
   /**
    * Returns the stderr result
    *
    * @return stderr result
    */
+  public String getStderr();
+  public int getExitValue();
+
+  public static OutputBuffer of(Process p) {
+    return new LazyOutputBuffer(p);
+  }
+
+  public static OutputBuffer of(String stdout, String stderr, int exitValue) {
+    return new EagerOutputBuffer(stdout, stderr, exitValue);
+  }
+
+  public static OutputBuffer of(String stdout, String stderr) {
+    return of(stdout, stderr, -1);
+  }
+
+  class LazyOutputBuffer implements OutputBuffer {
+    private final ByteArrayOutputStream stderrBuffer;
+    private final ByteArrayOutputStream stdoutBuffer;
+    private final Future<Void> outTask;
+    private final Future<Void> errTask;
+    private final Process p;
+
+    private LazyOutputBuffer(Process p) {
+      this.p = p;
+      stderrBuffer = new ByteArrayOutputStream();
+      stdoutBuffer = new ByteArrayOutputStream();
+      outTask = new StreamPumper(p.getInputStream(),
+              stdoutBuffer).process();
+      errTask = new StreamPumper(p.getErrorStream(),
+              stderrBuffer).process();
+    }
+
+    @Override
+    public String getStdout() {
+      try {
+        outTask.get();
+        return stdoutBuffer.toString();
+      } catch (InterruptedException e) {
+        Thread.currentThread().interrupt();
+        throw new OutputBufferException(e);
+      } catch (ExecutionException | CancellationException e) {
+        throw new OutputBufferException(e);
+      }
+    }
+
+    @Override
+    public String getStderr() {
+      try {
+        errTask.get();
+        return stderrBuffer.toString();
+      } catch (InterruptedException e) {
+        Thread.currentThread().interrupt();
+        throw new OutputBufferException(e);
+      } catch (ExecutionException | CancellationException e) {
+        throw new OutputBufferException(e);
+      }
+    }
+
+    @Override
+    public int getExitValue() {
+      try {
+        return p.waitFor();
+      } catch (InterruptedException e) {
+        Thread.currentThread().interrupt();
+        throw new OutputBufferException(e);
+      }
+    }
+  }
+
+  class EagerOutputBuffer implements OutputBuffer {
+    private final String stdout;
+    private final String stderr;
+    private final int exitValue;
+
+    private EagerOutputBuffer(String stdout, String stderr, int exitValue) {
+      this.stdout = stdout;
+      this.stderr = stderr;
+      this.exitValue = exitValue;
+    }
+
+    public String getStdout() {
+      return stdout;
+    }
+
   public String getStderr() {
     return stderr;
   }
+
+    @Override
+    public int getExitValue() {
+      return exitValue;
+    }
+  }
 }
< prev index next >