1 /*
   2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.testlibrary;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.util.concurrent.CancellationException;
  28 import java.util.concurrent.ExecutionException;
  29 import java.util.concurrent.Future;
  30 
  31 /**
  32  * @deprecated This class is deprecated. Use the one from
  33  *             {@code <root>/test/lib/share/classes/jdk/test/lib/process}
  34  */
  35 @Deprecated
  36 class OutputBuffer {
  37     private static class OutputBufferException extends RuntimeException {
  38         private static final long serialVersionUID = 8528687792643129571L;
  39 
  40         public OutputBufferException(Throwable cause) {
  41             super(cause);
  42         }
  43     }
  44 
  45     private final Process p;
  46     private final Future<Void> outTask;
  47     private final Future<Void> errTask;
  48     private final ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
  49     private final ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();
  50 
  51     /**
  52      * Create an OutputBuffer, a class for storing and managing stdout and
  53      * stderr results separately
  54      *
  55      * @param stdout
  56      *            stdout result
  57      * @param stderr
  58      *            stderr result
  59      */
  60     OutputBuffer(Process p) {
  61         this.p = p;
  62         StreamPumper outPumper = new StreamPumper(p.getInputStream(),
  63                 stdoutBuffer);
  64         StreamPumper errPumper = new StreamPumper(p.getErrorStream(),
  65                 stderrBuffer);
  66 
  67         outTask = outPumper.process();
  68         errTask = errPumper.process();
  69     }
  70 
  71     /**
  72      * Returns the stdout result
  73      *
  74      * @return stdout result
  75      */
  76     public String getStdout() {
  77         try {
  78             outTask.get();
  79             return stdoutBuffer.toString();
  80         } catch (InterruptedException e) {
  81             Thread.currentThread().interrupt();
  82             throw new OutputBufferException(e);
  83         } catch (ExecutionException | CancellationException e) {
  84             throw new OutputBufferException(e);
  85         }
  86     }
  87 
  88     /**
  89      * Returns the stderr result
  90      *
  91      * @return stderr result
  92      */
  93     public String getStderr() {
  94         try {
  95             errTask.get();
  96             return stderrBuffer.toString();
  97         } catch (InterruptedException e) {
  98             Thread.currentThread().interrupt();
  99             throw new OutputBufferException(e);
 100         } catch (ExecutionException | CancellationException e) {
 101             throw new OutputBufferException(e);
 102         }
 103     }
 104 
 105     public int getExitValue() {
 106         try {
 107             return p.waitFor();
 108         } catch (InterruptedException e) {
 109             Thread.currentThread().interrupt();
 110             throw new OutputBufferException(e);
 111         }
 112     }
 113 }