JDK-8357164 : Use "stdin.encoding" in Console's read*() methods
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P4
  • Status: Finalized
  • Resolution: Unresolved
  • Fix Versions: 25
  • Submitted: 2025-05-16
  • Updated: 2025-05-23
Related Reports
CSR :  
Description
Summary
-------

Use the `Charset` specified by the `stdin.encoding` system property in `Console` methods performing read operations

Problem
-------

The `java.io.Console` class uses the `Charset` specified by the `stdout.encoding` system property for both reading and writing operations. However, on some platforms, the terminal's input and output encodings can be configured independently. In such cases, relying on `stdout.encoding` for reading standard input may result in garbled characters.

Solution
--------

Use the `Charset` specified by the `stdin.encoding` system property for reading the standard input.

Specification
-------------

Add the following paragraph in the class description of `java.io.Console`:
```
--- a/src/java.base/share/classes/java/io/Console.java
+++ b/src/java.base/share/classes/java/io/Console.java
@@ -59,6 +59,12 @@
  * on the objects returned by {@link #reader()} and {@link #writer()} may
  * block in multithreaded scenarios.
  * <p>
+ * Read and write operations use the {@code Charset}s specified by
+ * {@link System##stdin.encoding stdin.encoding} and {@link
+ * System##stdout.encoding stdout.encoding}, respectively. The
+ * {@code Charset} used for write operations can also be retrieved using
+ * the {@link #charset()} method. Since {@code Console} is intended for
+ * interactive use on a terminal, these charsets are typically the same.
+ * <p>
  * Operations that format strings are locale sensitive, using either the
  * specified {@code Locale}, or the
  * {@link Locale##default_locale default format Locale} to produce localized
```
To match the above description, change the method description of `charset()`
```
      /**
- * Returns the {@link java.nio.charset.Charset Charset} object used for
- * the {@code Console}.
+ * {@return the {@link java.nio.charset.Charset Charset} object used for
+ * the write operations on this {@code Console}}
       * <p>
- * The returned charset is used for interpreting the input and output source
- * (e.g., keyboard and/or display) specified by the host environment or user,
- * which defaults to the one based on {@link System##stdout.encoding stdout.encoding}.
- * It may not necessarily be the same as the default charset returned from
+ * The returned charset is used for encoding the data that is sent to
+ * the output (e.g., display), specified by the host environment or user.
+ * It defaults to the one based on {@link System##stdout.encoding stdout.encoding},
+ * and may not necessarily be the same as the default charset returned from
       * {@link java.nio.charset.Charset#defaultCharset() Charset.defaultCharset()}.
       *
- * @return a {@link java.nio.charset.Charset Charset} object used for the
- * {@code Console}
       * @since 17
       */
```