Skip to content

Commit 17a2c33

Browse files
committed
[java] read complete output of selenium manager #14820
1 parent a96f6aa commit 17a2c33

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

java/src/org/openqa/selenium/manager/SeleniumManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ private static Result runCommand(Path binary, List<String> arguments) {
123123
String output;
124124
int code;
125125
try {
126-
ExternalProcess.Builder processBuilder = ExternalProcess.builder();
126+
ExternalProcess.Builder processBuilder =
127+
ExternalProcess.builder()
128+
// keep all output of the process to avoid JSON syntax errors while parsing
129+
.bufferSize(-1);
127130

128131
Properties properties = System.getProperties();
129132
for (String name : properties.stringPropertyNames()) {

java/src/org/openqa/selenium/os/ExternalProcess.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2121

22+
import java.io.ByteArrayOutputStream;
2223
import java.io.File;
2324
import java.io.IOException;
2425
import java.io.InputStream;
@@ -159,8 +160,8 @@ public Builder directory(File directory) {
159160
}
160161

161162
/**
162-
* Where to copy the combined stdout and stderr output to, {@code OsProcess#getOutput} is still
163-
* working when called.
163+
* Where to copy the combined stdout and stderr output to, {@code ExternalProcess#getOutput} is
164+
* still working when called.
164165
*
165166
* @param stream where to copy the combined output to
166167
* @return this instance to continue building
@@ -172,9 +173,9 @@ public Builder copyOutputTo(OutputStream stream) {
172173
}
173174

174175
/**
175-
* The number of bytes to buffer for {@code OsProcess#getOutput} calls.
176+
* The number of bytes to buffer for {@code ExternalProcess#getOutput} calls.
176177
*
177-
* @param toKeep the number of bytes, default is 4096
178+
* @param toKeep the number of bytes, default is 32768
178179
* @return this instance to continue building
179180
*/
180181
public Builder bufferSize(int toKeep) {
@@ -195,13 +196,19 @@ public ExternalProcess start() throws UncheckedIOException {
195196
}
196197

197198
try {
198-
CircularOutputStream circular = new CircularOutputStream(bufferSize);
199+
OutputStream buffer;
200+
201+
if (bufferSize != -1) {
202+
buffer = new CircularOutputStream(bufferSize);
203+
} else {
204+
buffer = new ByteArrayOutputStream();
205+
}
199206

200207
Thread worker =
201208
new Thread(
202209
() -> {
203210
// copyOutputTo might be system.out or system.err, do not to close
204-
OutputStream output = new MultiOutputStream(circular, copyOutputTo);
211+
OutputStream output = new MultiOutputStream(buffer, copyOutputTo);
205212
// closing the InputStream does somehow disturb the process, do not to close
206213
InputStream input = process.getInputStream();
207214
// use the CircularOutputStream as mandatory, we know it will never raise a
@@ -221,7 +228,7 @@ public ExternalProcess start() throws UncheckedIOException {
221228
worker.setDaemon(true);
222229
worker.start();
223230

224-
return new ExternalProcess(process, circular, worker);
231+
return new ExternalProcess(process, buffer, worker);
225232
} catch (Throwable t) {
226233
// ensure we do not leak a process in case of failures
227234
try {
@@ -239,18 +246,18 @@ public static Builder builder() {
239246
}
240247

241248
private final Process process;
242-
private final CircularOutputStream outputStream;
249+
private final OutputStream outputStream;
243250
private final Thread worker;
244251

245-
public ExternalProcess(Process process, CircularOutputStream outputStream, Thread worker) {
252+
public ExternalProcess(Process process, OutputStream outputStream, Thread worker) {
246253
this.process = process;
247254
this.outputStream = outputStream;
248255
this.worker = worker;
249256
}
250257

251258
/**
252259
* The last N bytes of the combined stdout and stderr as String, the value of N is set while
253-
* building the OsProcess.
260+
* building the ExternalProcess.
254261
*
255262
* @return stdout and stderr as String in Charset.defaultCharset() encoding
256263
*/
@@ -260,13 +267,20 @@ public String getOutput() {
260267

261268
/**
262269
* The last N bytes of the combined stdout and stderr as String, the value of N is set while
263-
* building the OsProcess.
270+
* building the ExternalProcess.
264271
*
265272
* @param encoding the encoding to decode the stream
266273
* @return stdout and stderr as String in the given encoding
267274
*/
268275
public String getOutput(Charset encoding) {
269-
return outputStream.toString(encoding);
276+
if (outputStream instanceof CircularOutputStream) {
277+
return ((CircularOutputStream) outputStream).toString(encoding);
278+
} else if (outputStream instanceof ByteArrayOutputStream) {
279+
return ((ByteArrayOutputStream) outputStream).toString(encoding);
280+
} else {
281+
throw new IllegalStateException(
282+
"unexpected OutputStream implementation: " + outputStream.getClass().getSimpleName());
283+
}
270284
}
271285

272286
public boolean isAlive() {

0 commit comments

Comments
 (0)