Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8260265: UTF-8 by Default #4733

Closed
wants to merge 16 commits into from
Closed
@@ -1,5 +1,5 @@
#
# Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,6 @@ charset US-ASCII US_ASCII
alias IBM367
alias cp367
alias csASCII
alias default
# Other aliases
alias 646 # Solaris POSIX locale
alias iso_646.irv:1983
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -198,16 +198,17 @@ public synchronized int size() {

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

/**
* Converts the buffer's contents into a string by decoding the bytes using
* the named {@link java.nio.charset.Charset charset}.
* the named {@link Charset charset}.
*
* <p> This method is equivalent to {@code #toString(charset)} that takes a
* {@link java.nio.charset.Charset charset}.
* {@link Charset charset}.
*
* <p> An invocation of this method of the form
*
@@ -240,7 +241,7 @@ public synchronized String toString() {
*
*
* @param charsetName the name of a supported
* {@link java.nio.charset.Charset charset}
* {@link Charset charset}
* @return String decoded from the buffer's contents.
* @throws UnsupportedEncodingException
* If the named charset is not supported
@@ -254,7 +255,7 @@ public synchronized String toString(String charsetName)

/**
* Converts the buffer's contents into a string by decoding the bytes using
* the specified {@link java.nio.charset.Charset charset}. The length of the new
* the specified {@link Charset charset}. The length of the new
* {@code String} is a function of the charset, and hence may not be equal
* to the length of the byte array.
*
@@ -263,7 +264,7 @@ public synchronized String toString(String charsetName)
* java.nio.charset.CharsetDecoder} class should be used when more control
* over the decoding process is required.
*
* @param charset the {@linkplain java.nio.charset.Charset charset}
* @param charset the {@linkplain Charset charset}
* to be used to decode the {@code bytes}
* @return String decoded from the buffer's contents.
* @since 10
@@ -286,14 +287,14 @@ public synchronized String toString(Charset charset) {
* As of JDK&nbsp;1.1, the preferred way to do this is via the
* {@link #toString(String charsetName)} or {@link #toString(Charset charset)}
* method, which takes an encoding-name or charset argument,
* or the {@code toString()} method, which uses the platform's default
* character encoding.
* or the {@code toString()} method, which uses the default charset.
*
* @param hibyte the high byte of each resulting Unicode character.
* @return the current contents of the output stream, as a string.
* @see java.io.ByteArrayOutputStream#size()
* @see java.io.ByteArrayOutputStream#toString(String)
* @see java.io.ByteArrayOutputStream#toString()
* @see Charset#defaultCharset()
*/
@Deprecated
public synchronized String toString(int hibyte) {
@@ -29,6 +29,7 @@
import java.nio.charset.Charset;
import jdk.internal.access.JavaIOAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.StaticProperty;
import sun.nio.cs.StreamDecoder;
import sun.nio.cs.StreamEncoder;
import sun.security.action.GetPropertyAction;
@@ -572,22 +573,34 @@ public int read(char[] cbuf, int offset, int length)

private static final Charset CHARSET;
static {
String csname = encoding();
Charset cs = null;
if (csname == null) {
csname = GetPropertyAction.privilegedGetProperty("sun.stdout.encoding");
boolean istty = istty();

if (istty) {
String csname = encoding();
if (csname == null) {
csname = GetPropertyAction.privilegedGetProperty("sun.stdout.encoding");
}
if (csname != null) {
try {
cs = Charset.forName(csname);
} catch (Exception ignored) { }
Copy link
Contributor

@RogerRiggs RogerRiggs Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate enhancement...
I've long thought that should be a way to avoid the exception here.
For example, a Charset.forName(csname, default);
The caller might have a default in mind or supply null and then be able to test for null.

Copy link
Member Author

@naotoj naotoj Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Will file an RFE for this.

Copy link
Member Author

@naotoj naotoj Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
if (csname != null) {
if (cs == null) {
try {
cs = Charset.forName(csname);
} catch (Exception ignored) { }
cs = Charset.forName(StaticProperty.nativeEncoding());
} catch (Exception ignored) {
cs = Charset.defaultCharset();
}
}
CHARSET = cs == null ? Charset.defaultCharset() : cs;

CHARSET = cs;

// Set up JavaIOAccess in SharedSecrets
SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
public Console console() {
if (istty()) {
if (istty) {
if (cons == null)
cons = new Console();
return cons;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,16 +29,16 @@

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

/**
* Creates a new {@code FileReader}, given the name of the file to read,
* using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* using the {@linkplain Charset#defaultCharset() default charset}.
*
* @param fileName the name of the file to read
* @throws FileNotFoundException if the named file does not exist,
* is a directory rather than a regular file,
* or for some other reason cannot be opened for
* reading.
* @see Charset#defaultCharset()
*/
public FileReader(String fileName) throws FileNotFoundException {
super(new FileInputStream(fileName));
}

/**
* Creates a new {@code FileReader}, given the {@code File} to read,
* using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* using the {@linkplain Charset#defaultCharset() default charset}.
*
* @param file the {@code File} to read
* @throws FileNotFoundException if the file does not exist,
* is a directory rather than a regular file,
* or for some other reason cannot be opened for
* reading.
* @see Charset#defaultCharset()
*/
public FileReader(File file) throws FileNotFoundException {
super(new FileInputStream(file));
}

/**
* Creates a new {@code FileReader}, given the {@code FileDescriptor} to read,
* using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* using the {@linkplain Charset#defaultCharset() default charset}.
*
* @param fd the {@code FileDescriptor} to read
* @see Charset#defaultCharset()
*/
public FileReader(FileDescriptor fd) {
super(new FileInputStream(fd));
}

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

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