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.test.lib.process;
25
26 public class OutputBuffer {
27 private final String stdout;
28 private final String stderr;
29
30 /**
31 * Create an OutputBuffer, a class for storing and managing stdout and stderr
32 * results separately
33 *
34 * @param stdout stdout result
35 * @param stderr stderr result
36 */
37 public OutputBuffer(String stdout, String stderr) {
38 this.stdout = stdout;
39 this.stderr = stderr;
40 }
41
42 /**
43 * Returns the stdout result
44 *
45 * @return stdout result
46 */
47 public String getStdout() {
48 return stdout;
49 }
50
51 /**
52 * Returns the stderr result
53 *
54 * @return stderr result
55 */
56 public String getStderr() {
57 return stderr;
58 }
59 }
|
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.test.lib.process;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.InputStream;
28 import java.util.concurrent.CancellationException;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.Future;
31
32 public interface OutputBuffer {
33 public static class OutputBufferException extends RuntimeException {
34 private static final long serialVersionUID = 8528687792643129571L;
35
36 public OutputBufferException(Throwable cause) {
37 super(cause);
38 }
39 }
40
41 /**
42 * Returns the stdout result
43 *
44 * @return stdout result
45 */
46 public String getStdout();
47 /**
48 * Returns the stderr result
49 *
50 * @return stderr result
51 */
52 public String getStderr();
53 public int getExitValue();
54
55 public static OutputBuffer of(Process p) {
56 return new LazyOutputBuffer(p);
57 }
58
59 public static OutputBuffer of(String stdout, String stderr, int exitValue) {
60 return new EagerOutputBuffer(stdout, stderr, exitValue);
61 }
62
63 public static OutputBuffer of(String stdout, String stderr) {
64 return of(stdout, stderr, -1);
65 }
66
67 class LazyOutputBuffer implements OutputBuffer {
68 private static class StreamTask {
69 private final ByteArrayOutputStream buffer;
70 private final Future<Void> future;
71
72 private StreamTask(InputStream stream) {
73 this.buffer = new ByteArrayOutputStream();
74 this.future = new StreamPumper(stream, buffer).process();
75 }
76
77 public String get() {
78 try {
79 future.get();
80 return buffer.toString();
81 } catch (InterruptedException e) {
82 Thread.currentThread().interrupt();
83 throw new OutputBufferException(e);
84 } catch (ExecutionException | CancellationException e) {
85 throw new OutputBufferException(e);
86 }
87 }
88 }
89
90 private final StreamTask outTask;
91 private final StreamTask errTask;
92 private final Process p;
93
94 private LazyOutputBuffer(Process p) {
95 this.p = p;
96 outTask = new StreamTask(p.getInputStream());
97 errTask = new StreamTask(p.getErrorStream());
98 }
99
100 @Override
101 public String getStdout() {
102 return outTask.get();
103 }
104
105 @Override
106 public String getStderr() {
107 return errTask.get();
108 }
109
110 @Override
111 public int getExitValue() {
112 try {
113 return p.waitFor();
114 } catch (InterruptedException e) {
115 Thread.currentThread().interrupt();
116 throw new OutputBufferException(e);
117 }
118 }
119 }
120
121 class EagerOutputBuffer implements OutputBuffer {
122 private final String stdout;
123 private final String stderr;
124 private final int exitValue;
125
126 private EagerOutputBuffer(String stdout, String stderr, int exitValue) {
127 this.stdout = stdout;
128 this.stderr = stderr;
129 this.exitValue = exitValue;
130 }
131
132 @Override
133 public String getStdout() {
134 return stdout;
135 }
136
137 @Override
138 public String getStderr() {
139 return stderr;
140 }
141
142 @Override
143 public int getExitValue() {
144 return exitValue;
145 }
146 }
147 }
|