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) 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 java.nio.charset.Charset#defaultCharset()
* @return String decoded from the buffer's contents.
* @since 1.1
*/
@@ -286,14 +287,15 @@ 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.
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.

Fold to previous line.

*
* @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 java.nio.charset.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;
@@ -570,24 +571,36 @@ public int read(char cbuf[], int offset, int length)
}
}

private static final Charset CHARSET;
static final Charset CHARSET; // shared with PrintStream
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
@@ -30,15 +30,15 @@
/**
* 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}.
* or the {@linkplain java.nio.charset.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 java.nio.charset.Charset#defaultCharset()
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.

The @ see duplicates the link above, the javadoc can do without the @ see.

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 I remove that @see, I don't see the link in See Also section. Am I missing something?

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.

In my view the @ linkplain is sufficient to allow the reader to navigate; but YMMV.

*
* @author Mark Reinhold
* @since 1.1
@@ -47,40 +47,40 @@ 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 java.nio.charset.Charset#defaultCharset() default charset}.
Copy link
Contributor

@AlanBateman AlanBateman Jul 20, 2021

Choose a reason for hiding this comment

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

The java.io classes import java.nio.charset.Charset so don't need to use the fully qualified class name everywhere.

Copy link
Member Author

@naotoj naotoj Jul 20, 2021

Choose a reason for hiding this comment

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

Corrected, as well as other locations.

*
* @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 java.nio.charset.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 java.nio.charset.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 java.nio.charset.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 java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param fd the {@code FileDescriptor} to read
* @see java.nio.charset.Charset#defaultCharset()
*/
public FileReader(FileDescriptor fd) {
super(new FileInputStream(fd));
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2018, 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
@@ -30,8 +30,7 @@
/**
* Writes text to character files using a default buffer size. Encoding from characters
* to bytes uses either a specified {@linkplain java.nio.charset.Charset charset}
* or the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* or the {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* <p>
* Whether or not a file is available or may be created depends upon the
@@ -46,6 +45,7 @@
*
* @see OutputStreamWriter
* @see FileOutputStream
* @see java.nio.charset.Charset#defaultCharset()
*
* @author Mark Reinhold
* @since 1.1
@@ -54,21 +54,22 @@
public class FileWriter extends OutputStreamWriter {

/**
* Constructs a {@code FileWriter} given a file name, using the platform's
* Constructs a {@code FileWriter} given a file name, using the
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
*
* @param fileName String The system-dependent filename.
* @throws IOException if the named file exists but is a directory rather
* than a regular file, does not exist but cannot be
* created, or cannot be opened for any other reason
* @see java.nio.charset.Charset#defaultCharset()
*/
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}

/**
* Constructs a {@code FileWriter} given a file name and a boolean indicating
* whether to append the data written, using the platform's
* whether to append the data written, using the
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param fileName String The system-dependent filename.
@@ -77,28 +78,29 @@ public FileWriter(String fileName) throws IOException {
* @throws IOException if the named file exists but is a directory rather
* than a regular file, does not exist but cannot be
* created, or cannot be opened for any other reason
* @see java.nio.charset.Charset#defaultCharset()
*/
public FileWriter(String fileName, boolean append) throws IOException {
super(new FileOutputStream(fileName, append));
}

/**
* Constructs a {@code FileWriter} given the {@code File} to write,
* using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
* using the {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
*
* @param file the {@code File} to write.
* @throws IOException if the file exists but is a directory rather than
* a regular file, does not exist but cannot be created,
* or cannot be opened for any other reason
* @see java.nio.charset.Charset#defaultCharset()
*/
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}

/**
* Constructs a {@code FileWriter} given the {@code File} to write and
* a boolean indicating whether to append the data written, using the platform's
* a boolean indicating whether to append the data written, using the
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param file the {@code File} to write
@@ -107,6 +109,7 @@ public FileWriter(File file) throws IOException {
* @throws IOException if the file exists but is a directory rather than
* a regular file, does not exist but cannot be created,
* or cannot be opened for any other reason
* @see java.nio.charset.Charset#defaultCharset()
* @since 1.4
*/
public FileWriter(File file, boolean append) throws IOException {
@@ -115,10 +118,10 @@ public FileWriter(File file, boolean append) throws IOException {

/**
* Constructs a {@code FileWriter} given a file descriptor,
* using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* using the {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param fd the {@code FileDescriptor} to write.
* @see java.nio.charset.Charset#defaultCharset()
*/
public FileWriter(FileDescriptor fd) {
super(new FileOutputStream(fd));
@@ -35,7 +35,7 @@
* An InputStreamReader is a bridge from byte streams to character streams: It
* reads bytes and decodes them into characters using a specified {@link
* java.nio.charset.Charset charset}. The charset that it uses
* may be specified by name or may be given explicitly, or the platform's
* may be specified by name or may be given explicitly, or the
* {@link Charset#defaultCharset() default charset} may be accepted.
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.

"may be accepted" seems like the API has some choice in the matter.
Perhaps "accepted" -> "used".
And in other classes below if there's a suitable replacement.

*
* <p> Each invocation of one of an InputStreamReader's read() methods may
@@ -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
@@ -35,7 +35,7 @@
* An OutputStreamWriter is a bridge from character streams to byte streams:
* Characters written to it are encoded into bytes using a specified {@link
* java.nio.charset.Charset charset}. The charset that it uses
* may be specified by name or may be given explicitly, or the platform's
* may be specified by name or may be given explicitly, or the
* default charset may be accepted.
*
* <p> Each invocation of a write() method causes the encoding converter to be
@@ -103,6 +103,7 @@ public OutputStreamWriter(OutputStream out, String charsetName)
* Creates an OutputStreamWriter that uses the default character encoding.
*
* @param out An OutputStream
* @see java.nio.charset.Charset#defaultCharset()
*/
public OutputStreamWriter(OutputStream out) {
super(out);