Skip to content

Commit

Permalink
[#1474] Avoid UnsupportedCharsetException: cp65001 on Microsoft Win…
Browse files Browse the repository at this point in the history
…dows console when code page is set to UTF-8.

Closes #1474
  • Loading branch information
remkop committed Nov 21, 2021
1 parent 14ff78b commit f98a4ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Picocli follows [semantic versioning](http://semver.org/).


## <a name="4.6.3-fixes"></a> Fixed issues
* [#1474] BUG: Avoid `UnsupportedCharsetException: cp65001` on Microsoft Windows console when code page is set to UTF-8. Thanks to [epuni](https://github.com/epuni) for raising this.
* [#1409][#1463] DOC: add documentation section on using default values in argument groups. Thanks to [Ben Kedo](https://github.com/MadFoal) for the pull request.
* [#1462] DOC, BUILD, DEP: Extend documentation on argument files, fix broken/outdated links, update dependencies. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
* [#1457] DOC: add caution about arguments in @files with quoted option parameters. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.URL;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.BreakIterator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -14523,12 +14524,17 @@ static void close(Closeable closeable) {
}
}
static Charset getStdoutEncoding() {
String encoding = System.getProperty("sun.stdout.encoding");
return encoding != null ? Charset.forName(encoding) : Charset.defaultCharset();
return charsetForName(System.getProperty("sun.stdout.encoding"));
}
static Charset getStderrEncoding() {
String encoding = System.getProperty("sun.stderr.encoding");
return encoding != null ? Charset.forName(encoding) : Charset.defaultCharset();
return charsetForName(System.getProperty("sun.stderr.encoding"));
}
static Charset charsetForName(String encoding) {
if (encoding != null) {
if ("cp65001".equalsIgnoreCase(encoding)) { encoding = "UTF-8"; } // #1474 MS Windows uses code page 65001 for UTF8
return Charset.forName(encoding);
}
return Charset.defaultCharset();
}
static PrintWriter newPrintWriter(OutputStream stream, Charset charset) {
return new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream, charset)), true);
Expand Down

0 comments on commit f98a4ae

Please sign in to comment.