RFR: 8356420: Provide examples on wrapping System.in [v4]
Naoto Sato
naoto at openjdk.org
Tue May 13 02:31:43 UTC 2025
On Tue, 13 May 2025 00:55:14 GMT, Stuart Marks <smarks at openjdk.org> wrote:
>> Naoto Sato has updated the pull request incrementally with one additional commit since the last revision:
>>
>> Reflects more comments
>
> src/java.base/share/classes/java/lang/System.java line 129:
>
>> 127: * within the object that handles character encoding. After this is done,
>> 128: * subsequent reading should use only the wrapper object; operating directly
>> 129: * on {@code System.in} results in unspecified behavior.
>
> Sorry, I think I suggested this wording initially, but I now think it needs to be changed. I'd suggest changing the text after the semicolon to
>
>> continuing to operate directly on {@code System.in} results in unspecified behavior.
Done.
> src/java.base/share/classes/java/util/Scanner.java line 89:
>
>> 87: * String aLine = sc.nextLine();
>> 88: * }
>> 89: * }
>
> Now that I'm looking at the other examples, I have what I think is better example for using Scanner on System.in. Take this example and move it after the example below, which reads long values from the file `myNumbers`. The new example could do the same thing with the hasNextLong/nextLong loop. But instead it would read from System.in using the encoding specified by `stdin.encoding`. The example could explain that reading from files uses the default encoding (`file.encoding`) whereas reading from System.in should use `stdin.encoding` because on some systems this differs from the default encoding.
I added an extra sentence for the explanation.
> src/java.base/share/classes/javax/security/auth/callback/CallbackHandler.java line 116:
>
>> 114: * System.err.flush();
>> 115: * nc.setName((new BufferedReader
>> 116: * (new InputStreamReader(System.in, "stdin.encoding"))).readLine());
>
> Needs to be `System.getProperty("stdin.encoding"))`.
Good catch! Thanks.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25155#discussion_r2085812565
PR Review Comment: https://git.openjdk.org/jdk/pull/25155#discussion_r2085812615
PR Review Comment: https://git.openjdk.org/jdk/pull/25155#discussion_r2085812480
More information about the core-libs-dev
mailing list