1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.github.maven_nar.cpptasks.compiler;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.util.Vector;
29
30 import org.apache.tools.ant.taskdefs.Execute;
31 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
32
33
34
35
36
37
38
39 public class CaptureStreamHandler implements ExecuteStreamHandler {
40
41 static class Copier extends Thread {
42 InputStream is;
43
44 Vector<String> lines;
45
46 Copier(final InputStream is) {
47 this.is = is;
48 this.lines = new Vector<>(10);
49 }
50
51 public Vector<String> getLines() {
52 return this.lines;
53 }
54
55 @Override
56 public void run() {
57 try {
58 final BufferedReader reader = new BufferedReader(new InputStreamReader(this.is));
59 while (true) {
60 final String line = reader.readLine();
61 if (line == null) {
62 break;
63 }
64 this.lines.addElement(line);
65 }
66 } catch (final IOException e) {
67
68 }
69 }
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public static CaptureStreamHandler execute(final String[] cmdline) {
86 final CaptureStreamHandler handler = new CaptureStreamHandler();
87 final Execute exec = new Execute(handler);
88 exec.setCommandline(cmdline);
89 try {
90 final int status = exec.execute();
91 } catch (final IOException ex) {
92 }
93 return handler;
94 }
95
96
97
98
99
100
101
102
103
104 public static String[] run(final String[] cmdline) {
105 final CaptureStreamHandler handler = execute(cmdline);
106 return handler.getOutput() != null ? handler.getOutput() : new String[0];
107 }
108
109 private String[] stderr;
110
111 private String[] stdout;
112
113 private InputStream processErrorStream;
114
115 private InputStream processOutputStream;
116
117 public CaptureStreamHandler() {
118 }
119
120
121
122
123
124
125
126 public void gatherOutput() {
127 try {
128 final Copier errorCopier = new Copier(this.processErrorStream);
129 final Copier outputCopier = new Copier(this.processOutputStream);
130 errorCopier.start();
131 outputCopier.start();
132 errorCopier.join();
133 outputCopier.join();
134 this.stderr = new String[errorCopier.getLines().size()];
135 errorCopier.getLines().copyInto(this.stderr);
136 this.stdout = new String[outputCopier.getLines().size()];
137 outputCopier.getLines().copyInto(this.stdout);
138 } catch (final Exception e) {
139 this.stderr = this.stdout = new String[0];
140 }
141 }
142
143
144
145
146
147 public String[] getOutput() {
148 return null != this.stderr && this.stderr.length > 0 ? this.stderr : this.stdout;
149 }
150
151
152 public String[] getStderr() {
153 return this.stderr;
154 }
155
156
157 public String[] getStdout() {
158 return this.stdout;
159 }
160
161
162
163
164
165
166
167 @Override
168 public void setProcessErrorStream(final InputStream is) throws IOException {
169 this.processErrorStream = is;
170 }
171
172
173
174
175
176
177
178
179 @Override
180 public void setProcessInputStream(final OutputStream os) throws IOException {
181 os.close();
182 }
183
184
185
186
187
188
189
190 @Override
191 public void setProcessOutputStream(final InputStream is) throws IOException {
192 this.processOutputStream = is;
193 }
194
195
196
197
198 @Override
199 public void start() throws IOException {
200 gatherOutput();
201 }
202
203
204
205
206 @Override
207 public void stop() {
208 }
209
210 }