Skip to content

Commit 7c8e273

Browse files
committed
8356420: Provide examples on wrapping System.in
Reviewed-by: liach, smarks, alanb, bpb, iris
1 parent a262835 commit 7c8e273

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/java.base/share/classes/java/io/InputStreamReader.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,9 +50,17 @@
5050
* BufferedReader in = new BufferedReader(new InputStreamReader(anInputStream));
5151
* }
5252
*
53+
* <P>To read from {@link System#in}, use the system property value
54+
* {@link System##stdin.encoding stdin.encoding} as the {@code Charset}:
55+
*
56+
* {@snippet lang=java :
57+
* new InputStreamReader(System.in, System.getProperty("stdin.encoding"));
58+
* }
59+
*
5360
* @see BufferedReader
5461
* @see InputStream
5562
* @see Charset
63+
* @see System##stdin.encoding stdin.encoding
5664
*
5765
* @author Mark Reinhold
5866
* @since 1.1

src/java.base/share/classes/java/lang/System.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,19 @@ private System() {
124124
*
125125
* @apiNote
126126
* The typical approach to read character data is to wrap {@code System.in}
127-
* within an {@link java.io.InputStreamReader InputStreamReader} or other object
128-
* that handles character encoding. After this is done, subsequent reading should
129-
* use only the wrapper object; operating directly on {@code System.in} results
130-
* in unspecified behavior.
127+
* within the object that handles character encoding. After this is done,
128+
* subsequent reading should use only the wrapper object; continuing to
129+
* operate directly on {@code System.in} results in unspecified behavior.
130+
* <p>
131+
* Here are two common examples. Using an {@link java.io.InputStreamReader
132+
* InputStreamReader}:
133+
* {@snippet lang=java :
134+
* new InputStreamReader(System.in, System.getProperty("stdin.encoding"));
135+
* }
136+
* Or using a {@link java.util.Scanner Scanner}:
137+
* {@snippet lang=java :
138+
* new Scanner(System.in, System.getProperty("stdin.encoding"));
139+
* }
131140
* <p>
132141
* For handling interactive input, consider using {@link Console}.
133142
*

src/java.base/share/classes/java/util/Scanner.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -78,7 +78,7 @@
7878
* }
7979
* }
8080
*
81-
* <p>As another example, this code allows {@code long} types to be
81+
* <p>This code allows {@code long} types to be
8282
* assigned from entries in a file {@code myNumbers}:
8383
* {@snippet :
8484
* Scanner sc = new Scanner(new File("myNumbers"));
@@ -87,6 +87,19 @@
8787
* }
8888
* }
8989
*
90+
* <p>This code uses a {@code Scanner} to read lines from {@link System#in}. The
91+
* {@code Scanner} uses the system property value of
92+
* {@link System##stdin.encoding stdin.encoding} as the {@code Charset}. Specifying
93+
* the charset explicitly is important when reading from {@code System.in}, as it
94+
* may differ from the {@link Charset#defaultCharset() default charset} depending
95+
* on the host environment or user configuration:
96+
* {@snippet :
97+
* Scanner sc = new Scanner(System.in, System.getProperty("stdin.encoding"));
98+
* while (sc.hasNextLine()) {
99+
* String aLine = sc.nextLine();
100+
* }
101+
* }
102+
*
90103
* <p>The scanner can also use delimiters other than whitespace. This
91104
* example reads several items in from a string:
92105
* {@snippet :

src/java.base/share/classes/javax/security/auth/callback/CallbackHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -113,7 +113,9 @@ public interface CallbackHandler {
113113
* System.err.print(nc.getPrompt());
114114
* System.err.flush();
115115
* nc.setName((new BufferedReader
116-
* (new InputStreamReader(System.in))).readLine());
116+
* (new InputStreamReader(
117+
* System.in,
118+
* System.getProperty("stdin.encoding")))).readLine());
117119
*
118120
* } else if (callbacks[i] instanceof PasswordCallback) {
119121
*

0 commit comments

Comments
 (0)