Skip to content

Commit

Permalink
[java] read selenium manager output as UTF-8 #13653
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed May 10, 2024
1 parent 17ba2aa commit 2aa0f5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 7 additions & 3 deletions java/src/org/openqa/selenium/io/CircularOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,23 @@ public void close() {
}

@Override
public synchronized String toString() {
public String toString() {
return toString(Charset.defaultCharset());
}

public synchronized String toString(Charset encoding) {
int size = filled ? buffer.length : end;
byte[] toReturn = new byte[size];

// Handle the partially filled array as a special case
if (!filled) {
System.arraycopy(buffer, 0, toReturn, 0, end);
return new String(toReturn, Charset.defaultCharset());
return new String(toReturn, encoding);
}

int n = buffer.length - end;
System.arraycopy(buffer, end, toReturn, 0, n);
System.arraycopy(buffer, 0, toReturn, n, end);
return new String(toReturn, Charset.defaultCharset());
return new String(toReturn, encoding);
}
}
3 changes: 2 additions & 1 deletion java/src/org/openqa/selenium/manager/SeleniumManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -141,7 +142,7 @@ private static Result runCommand(Path binary, List<String> arguments) {
process.shutdown();
}
code = process.exitValue();
output = process.getOutput();
output = process.getOutput(StandardCharsets.UTF_8);
} catch (Exception e) {
throw new WebDriverException("Failed to run command: " + arguments, e);
}
Expand Down
14 changes: 13 additions & 1 deletion java/src/org/openqa/selenium/os/ExternalProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -254,7 +255,18 @@ public ExternalProcess(Process process, CircularOutputStream outputStream, Threa
* @return stdout and stderr as String in Charset.defaultCharset() encoding
*/
public String getOutput() {
return outputStream.toString();
return getOutput(Charset.defaultCharset());
}

/**
* The last N bytes of the combined stdout and stderr as String, the value of N is set while
* building the OsProcess.
*
* @param encoding the encoding to decode the stream
* @return stdout and stderr as String in the given encoding
*/
public String getOutput(Charset encoding) {
return outputStream.toString(encoding);
}

public boolean isAlive() {
Expand Down

0 comments on commit 2aa0f5a

Please sign in to comment.