Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8357164

Use "stdin.encoding" in Console's read*() methods

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • behavioral
    • low
    • Applications using java.io.Console may observe differences in how input characters are decoded if the platform's terminal input/output encodings differ. However, this behavior is expected and consistent with the platform or user configuration.
    • Java API, System or security property
    • SE

      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} returned by the
      + * {@link #charset()} method, unless {@link System##stdin.encoding
      + * stdin.encoding} differs from {@link System##stdout.encoding
      + * stdout.encoding}, in which case read operations use the {@code Charset}
      + * designated by {@code stdin.encoding}.
      + * <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, add the following sentence to the method description of charset()

            * 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
            * {@link java.nio.charset.Charset#defaultCharset() Charset.defaultCharset()}.
      +     * If {@link System##stdin.encoding stdin.encoding} differs from
      +     * {@link System##stdout.encoding stdout.encoding}, read operations use the
      +     * {@code Charset} designated by {@code stdin.encoding}, instead of the returned
      +     * {@code Charset}.
            *
            * @return a {@link java.nio.charset.Charset Charset} object used for the
            *          {@code Console}

            naoto Naoto Sato
            naoto Naoto Sato
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated: