Skip to content

Commit 7fc8540

Browse files
committedAug 30, 2021
8260265: UTF-8 by Default
Reviewed-by: alanb, rriggs
1 parent 3204853 commit 7fc8540

22 files changed

+385
-201
lines changed
 

‎make/data/charsetmapping/charsets

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2000, 2021, 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
@@ -49,7 +49,6 @@ charset US-ASCII US_ASCII
4949
alias IBM367
5050
alias cp367
5151
alias csASCII
52-
alias default
5352
# Other aliases
5453
alias 646 # Solaris POSIX locale
5554
alias iso_646.irv:1983

‎src/java.base/share/classes/java/io/ByteArrayOutputStream.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2021, 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
@@ -198,16 +198,17 @@ public synchronized int size() {
198198

199199
/**
200200
* Converts the buffer's contents into a string decoding bytes using the
201-
* platform's default character set. The length of the new {@code String}
202-
* is a function of the character set, and hence may not be equal to the
201+
* default charset. The length of the new {@code String}
202+
* is a function of the charset, and hence may not be equal to the
203203
* size of the buffer.
204204
*
205205
* <p> This method always replaces malformed-input and unmappable-character
206-
* sequences with the default replacement string for the platform's
207-
* default character set. The {@linkplain java.nio.charset.CharsetDecoder}
206+
* sequences with the default replacement string for the
207+
* default charset. The {@linkplain java.nio.charset.CharsetDecoder}
208208
* class should be used when more control over the decoding process is
209209
* required.
210210
*
211+
* @see Charset#defaultCharset()
211212
* @return String decoded from the buffer's contents.
212213
* @since 1.1
213214
*/
@@ -217,10 +218,10 @@ public synchronized String toString() {
217218

218219
/**
219220
* Converts the buffer's contents into a string by decoding the bytes using
220-
* the named {@link java.nio.charset.Charset charset}.
221+
* the named {@link Charset charset}.
221222
*
222223
* <p> This method is equivalent to {@code #toString(charset)} that takes a
223-
* {@link java.nio.charset.Charset charset}.
224+
* {@link Charset charset}.
224225
*
225226
* <p> An invocation of this method of the form
226227
*
@@ -240,7 +241,7 @@ public synchronized String toString() {
240241
*
241242
*
242243
* @param charsetName the name of a supported
243-
* {@link java.nio.charset.Charset charset}
244+
* {@link Charset charset}
244245
* @return String decoded from the buffer's contents.
245246
* @throws UnsupportedEncodingException
246247
* If the named charset is not supported
@@ -254,7 +255,7 @@ public synchronized String toString(String charsetName)
254255

255256
/**
256257
* Converts the buffer's contents into a string by decoding the bytes using
257-
* the specified {@link java.nio.charset.Charset charset}. The length of the new
258+
* the specified {@link Charset charset}. The length of the new
258259
* {@code String} is a function of the charset, and hence may not be equal
259260
* to the length of the byte array.
260261
*
@@ -263,7 +264,7 @@ public synchronized String toString(String charsetName)
263264
* java.nio.charset.CharsetDecoder} class should be used when more control
264265
* over the decoding process is required.
265266
*
266-
* @param charset the {@linkplain java.nio.charset.Charset charset}
267+
* @param charset the {@linkplain Charset charset}
267268
* to be used to decode the {@code bytes}
268269
* @return String decoded from the buffer's contents.
269270
* @since 10
@@ -286,14 +287,14 @@ public synchronized String toString(Charset charset) {
286287
* As of JDK&nbsp;1.1, the preferred way to do this is via the
287288
* {@link #toString(String charsetName)} or {@link #toString(Charset charset)}
288289
* method, which takes an encoding-name or charset argument,
289-
* or the {@code toString()} method, which uses the platform's default
290-
* character encoding.
290+
* or the {@code toString()} method, which uses the default charset.
291291
*
292292
* @param hibyte the high byte of each resulting Unicode character.
293293
* @return the current contents of the output stream, as a string.
294294
* @see java.io.ByteArrayOutputStream#size()
295295
* @see java.io.ByteArrayOutputStream#toString(String)
296296
* @see java.io.ByteArrayOutputStream#toString()
297+
* @see Charset#defaultCharset()
297298
*/
298299
@Deprecated
299300
public synchronized String toString(int hibyte) {

‎src/java.base/share/classes/java/io/Console.java

+21-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.charset.Charset;
3030
import jdk.internal.access.JavaIOAccess;
3131
import jdk.internal.access.SharedSecrets;
32+
import jdk.internal.util.StaticProperty;
3233
import sun.nio.cs.StreamDecoder;
3334
import sun.nio.cs.StreamEncoder;
3435
import sun.security.action.GetPropertyAction;
@@ -572,22 +573,34 @@ public int read(char[] cbuf, int offset, int length)
572573

573574
private static final Charset CHARSET;
574575
static {
575-
String csname = encoding();
576576
Charset cs = null;
577-
if (csname == null) {
578-
csname = GetPropertyAction.privilegedGetProperty("sun.stdout.encoding");
577+
boolean istty = istty();
578+
579+
if (istty) {
580+
String csname = encoding();
581+
if (csname == null) {
582+
csname = GetPropertyAction.privilegedGetProperty("sun.stdout.encoding");
583+
}
584+
if (csname != null) {
585+
try {
586+
cs = Charset.forName(csname);
587+
} catch (Exception ignored) { }
588+
}
579589
}
580-
if (csname != null) {
590+
if (cs == null) {
581591
try {
582-
cs = Charset.forName(csname);
583-
} catch (Exception ignored) { }
592+
cs = Charset.forName(StaticProperty.nativeEncoding());
593+
} catch (Exception ignored) {
594+
cs = Charset.defaultCharset();
595+
}
584596
}
585-
CHARSET = cs == null ? Charset.defaultCharset() : cs;
597+
598+
CHARSET = cs;
586599

587600
// Set up JavaIOAccess in SharedSecrets
588601
SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
589602
public Console console() {
590-
if (istty()) {
603+
if (istty) {
591604
if (cons == null)
592605
cons = new Console();
593606
return cons;

‎src/java.base/share/classes/java/io/FileReader.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2021, 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
@@ -29,16 +29,16 @@
2929

3030
/**
3131
* Reads text from character files using a default buffer size. Decoding from bytes
32-
* to characters uses either a specified {@linkplain java.nio.charset.Charset charset}
33-
* or the platform's
34-
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
32+
* to characters uses either a specified {@linkplain Charset charset}
33+
* or the {@linkplain Charset#defaultCharset() default charset}.
3534
*
3635
* <p>
3736
* The {@code FileReader} is meant for reading streams of characters. For reading
3837
* streams of raw bytes, consider using a {@code FileInputStream}.
3938
*
4039
* @see InputStreamReader
4140
* @see FileInputStream
41+
* @see Charset#defaultCharset()
4242
*
4343
* @author Mark Reinhold
4444
* @since 1.1
@@ -47,51 +47,51 @@ public class FileReader extends InputStreamReader {
4747

4848
/**
4949
* Creates a new {@code FileReader}, given the name of the file to read,
50-
* using the platform's
51-
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
50+
* using the {@linkplain Charset#defaultCharset() default charset}.
5251
*
5352
* @param fileName the name of the file to read
5453
* @throws FileNotFoundException if the named file does not exist,
5554
* is a directory rather than a regular file,
5655
* or for some other reason cannot be opened for
5756
* reading.
57+
* @see Charset#defaultCharset()
5858
*/
5959
public FileReader(String fileName) throws FileNotFoundException {
6060
super(new FileInputStream(fileName));
6161
}
6262

6363
/**
6464
* Creates a new {@code FileReader}, given the {@code File} to read,
65-
* using the platform's
66-
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
65+
* using the {@linkplain Charset#defaultCharset() default charset}.
6766
*
6867
* @param file the {@code File} to read
6968
* @throws FileNotFoundException if the file does not exist,
7069
* is a directory rather than a regular file,
7170
* or for some other reason cannot be opened for
7271
* reading.
72+
* @see Charset#defaultCharset()
7373
*/
7474
public FileReader(File file) throws FileNotFoundException {
7575
super(new FileInputStream(file));
7676
}
7777

7878
/**
7979
* Creates a new {@code FileReader}, given the {@code FileDescriptor} to read,
80-
* using the platform's
81-
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
80+
* using the {@linkplain Charset#defaultCharset() default charset}.
8281
*
8382
* @param fd the {@code FileDescriptor} to read
83+
* @see Charset#defaultCharset()
8484
*/
8585
public FileReader(FileDescriptor fd) {
8686
super(new FileInputStream(fd));
8787
}
8888

8989
/**
9090
* Creates a new {@code FileReader}, given the name of the file to read
91-
* and the {@linkplain java.nio.charset.Charset charset}.
91+
* and the {@linkplain Charset charset}.
9292
*
9393
* @param fileName the name of the file to read
94-
* @param charset the {@linkplain java.nio.charset.Charset charset}
94+
* @param charset the {@linkplain Charset charset}
9595
* @throws IOException if the named file does not exist,
9696
* is a directory rather than a regular file,
9797
* or for some other reason cannot be opened for
@@ -105,10 +105,10 @@ public FileReader(String fileName, Charset charset) throws IOException {
105105

106106
/**
107107
* Creates a new {@code FileReader}, given the {@code File} to read and
108-
* the {@linkplain java.nio.charset.Charset charset}.
108+
* the {@linkplain Charset charset}.
109109
*
110110
* @param file the {@code File} to read
111-
* @param charset the {@linkplain java.nio.charset.Charset charset}
111+
* @param charset the {@linkplain Charset charset}
112112
* @throws IOException if the file does not exist,
113113
* is a directory rather than a regular file,
114114
* or for some other reason cannot be opened for

0 commit comments

Comments
 (0)
Please sign in to comment.