< prev index next >

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

Print this page
rev 51638 : [mq]: 8210112
rev 51639 : [mq]: 8210112-1

@@ -22,10 +22,11 @@
  */
 
 package jdk.test.lib.process;
 
 import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
 public interface OutputBuffer {

@@ -62,50 +63,50 @@
   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 static class StreamTask {
+      private final ByteArrayOutputStream buffer;
+      private final Future<Void> future;
 
-    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();
+      private StreamTask(InputStream stream) {
+        this.buffer = new ByteArrayOutputStream();
+        this.future = new StreamPumper(stream, buffer).process();
     }
 
-    @Override
-    public String getStdout() {
+      public String get() {
       try {
-        outTask.get();
-        return stdoutBuffer.toString();
+          future.get();
+          return buffer.toString();
       } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
         throw new OutputBufferException(e);
       } catch (ExecutionException | CancellationException e) {
         throw new OutputBufferException(e);
       }
     }
+    }
+
+    private final StreamTask outTask;
+    private final StreamTask errTask;
+    private final Process p;
+
+    private LazyOutputBuffer(Process p) {
+      this.p = p;
+      outTask = new StreamTask(p.getInputStream());
+      errTask = new StreamTask(p.getErrorStream());
+    }
 
     @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);
+    public String getStdout() {
+      return outTask.get();
       }
+
+    @Override
+    public String getStderr() {
+      return errTask.get();
     }
 
     @Override
     public int getExitValue() {
       try {

@@ -126,14 +127,16 @@
       this.stdout = stdout;
       this.stderr = stderr;
       this.exitValue = exitValue;
     }
 
+    @Override
     public String getStdout() {
       return stdout;
     }
 
+    @Override
     public String getStderr() {
       return stderr;
     }
 
     @Override
< prev index next >