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

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

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • 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}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
             */

            naoto Naoto Sato
            naoto Naoto Sato
            Alan Bateman, Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: