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