8260265: UTF-8 by Default

Reviewed-by: alanb, rriggs
This commit is contained in:
Naoto Sato 2021-08-30 21:13:59 +00:00
parent 32048536e9
commit 7fc8540907
22 changed files with 385 additions and 201 deletions

View File

@ -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

View File

@ -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 class ByteArrayOutputStream extends OutputStream {
/**
* 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 class ByteArrayOutputStream extends OutputStream {
/**
* 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 class ByteArrayOutputStream extends OutputStream {
*
*
* @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 class ByteArrayOutputStream extends OutputStream {
/**
* 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 class ByteArrayOutputStream extends OutputStream {
* 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 class ByteArrayOutputStream extends OutputStream {
* 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) {

View File

@ -29,6 +29,7 @@ import java.util.*;
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 final class Console implements Flushable
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) { }
}
}
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;

View File

@ -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,9 +29,8 @@ import java.nio.charset.Charset;
/**
* 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
@ -39,6 +38,7 @@ import java.nio.charset.Charset;
*
* @see InputStreamReader
* @see FileInputStream
* @see Charset#defaultCharset()
*
* @author Mark Reinhold
* @since 1.1
@ -47,14 +47,14 @@ 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));
@ -62,14 +62,14 @@ public class FileReader extends InputStreamReader {
/**
* 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));
@ -77,10 +77,10 @@ public class FileReader extends InputStreamReader {
/**
* 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));
@ -88,10 +88,10 @@ public class FileReader extends InputStreamReader {
/**
* 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 class FileReader extends InputStreamReader {
/**
* 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

View File

@ -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
@ -29,9 +29,8 @@ import java.nio.charset.Charset;
/**
* 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}.
* to bytes uses either a specified {@linkplain Charset charset}
* or the {@linkplain Charset#defaultCharset() default charset}.
*
* <p>
* Whether or not a file is available or may be created depends upon the
@ -46,6 +45,7 @@ import java.nio.charset.Charset;
*
* @see OutputStreamWriter
* @see FileOutputStream
* @see Charset#defaultCharset()
*
* @author Mark Reinhold
* @since 1.1
@ -54,13 +54,14 @@ import java.nio.charset.Charset;
public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given a file name, using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
* Constructs a {@code FileWriter} given a file name, using the
* {@linkplain 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 Charset#defaultCharset()
*/
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
@ -68,8 +69,8 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given a file name and a boolean indicating
* whether to append the data written, using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* whether to append the data written, using the
* {@linkplain Charset#defaultCharset() default charset}.
*
* @param fileName String The system-dependent filename.
* @param append boolean if {@code true}, then data will be written
@ -77,6 +78,7 @@ public class FileWriter extends OutputStreamWriter {
* @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 Charset#defaultCharset()
*/
public FileWriter(String fileName, boolean append) throws IOException {
super(new FileOutputStream(fileName, append));
@ -84,13 +86,13 @@ public class FileWriter extends OutputStreamWriter {
/**
* 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 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 Charset#defaultCharset()
*/
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
@ -98,8 +100,8 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given the {@code File} to write and
* a boolean indicating whether to append the data written, using the platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* a boolean indicating whether to append the data written, using the
* {@linkplain Charset#defaultCharset() default charset}.
*
* @param file the {@code File} to write
* @param append if {@code true}, then bytes will be written
@ -107,6 +109,7 @@ public class FileWriter extends OutputStreamWriter {
* @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 Charset#defaultCharset()
* @since 1.4
*/
public FileWriter(File file, boolean append) throws IOException {
@ -115,10 +118,10 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given a file descriptor,
* 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 write.
* @see Charset#defaultCharset()
*/
public FileWriter(FileDescriptor fd) {
super(new FileOutputStream(fd));
@ -127,10 +130,10 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given a file name and
* {@linkplain java.nio.charset.Charset charset}.
* {@linkplain Charset charset}.
*
* @param fileName the name of the file to write
* @param charset the {@linkplain java.nio.charset.Charset charset}
* @param charset the {@linkplain Charset charset}
* @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
@ -143,11 +146,11 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given a file name,
* {@linkplain java.nio.charset.Charset charset} and a boolean indicating
* {@linkplain Charset charset} and a boolean indicating
* whether to append the data written.
*
* @param fileName the name of the file to write
* @param charset the {@linkplain java.nio.charset.Charset charset}
* @param charset the {@linkplain Charset charset}
* @param append a boolean. If {@code true}, the writer will write the data
* to the end of the file rather than the beginning.
* @throws IOException if the named file exists but is a directory rather
@ -162,10 +165,10 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given the {@code File} to write and
* {@linkplain java.nio.charset.Charset charset}.
* {@linkplain Charset charset}.
*
* @param file the {@code File} to write
* @param charset the {@linkplain java.nio.charset.Charset charset}
* @param charset the {@linkplain Charset charset}
* @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
@ -178,11 +181,11 @@ public class FileWriter extends OutputStreamWriter {
/**
* Constructs a {@code FileWriter} given the {@code File} to write,
* {@linkplain java.nio.charset.Charset charset} and a boolean indicating
* {@linkplain Charset charset} and a boolean indicating
* whether to append the data written.
*
* @param file the {@code File} to write
* @param charset the {@linkplain java.nio.charset.Charset charset}
* @param charset the {@linkplain Charset charset}
* @param append a boolean. If {@code true}, the writer will write the data
* to the end of the file rather than the beginning.
* @throws IOException if the file exists but is a directory rather than

View File

@ -34,9 +34,9 @@ import sun.nio.cs.StreamDecoder;
/**
* 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
* {@link Charset#defaultCharset() default charset} may be accepted.
* Charset charset}. The charset that it uses
* may be specified by name or may be given explicitly, or the
* {@link Charset#defaultCharset() default charset} may be used.
*
* <p> Each invocation of one of an InputStreamReader's read() methods may
* cause one or more bytes to be read from the underlying byte-input stream.
@ -54,7 +54,7 @@ import sun.nio.cs.StreamDecoder;
*
* @see BufferedReader
* @see InputStream
* @see java.nio.charset.Charset
* @see Charset
*
* @author Mark Reinhold
* @since 1.1
@ -85,8 +85,7 @@ public class InputStreamReader extends Reader {
* An InputStream
*
* @param charsetName
* The name of a supported
* {@link java.nio.charset.Charset charset}
* The name of a supported {@link Charset charset}
*
* @throws UnsupportedEncodingException
* If the named charset is not supported
@ -145,7 +144,7 @@ public class InputStreamReader extends Reader {
* @return The historical name of this encoding, or
* {@code null} if the stream has been closed
*
* @see java.nio.charset.Charset
* @see Charset
*
* @revised 1.4
*/

View File

@ -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
@ -34,8 +34,8 @@ import sun.nio.cs.StreamEncoder;
/**
* 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
* Charset charset}. The charset that it uses
* 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
@ -48,7 +48,7 @@ import sun.nio.cs.StreamEncoder;
*
* <pre>
* Writer out
* = new BufferedWriter(new OutputStreamWriter(System.out));
* = new BufferedWriter(new OutputStreamWriter(anOutputStream));
* </pre>
*
* <p> A <i>surrogate pair</i> is a character represented by a sequence of two
@ -62,12 +62,12 @@ import sun.nio.cs.StreamEncoder;
*
* <p> This class always replaces malformed surrogate elements and unmappable
* character sequences with the charset's default <i>substitution sequence</i>.
* The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
* The {@linkplain CharsetEncoder} class should be used when more
* control over the encoding process is required.
*
* @see BufferedWriter
* @see OutputStream
* @see java.nio.charset.Charset
* @see Charset
*
* @author Mark Reinhold
* @since 1.1
@ -84,8 +84,7 @@ public class OutputStreamWriter extends Writer {
* An OutputStream
*
* @param charsetName
* The name of a supported
* {@link java.nio.charset.Charset charset}
* The name of a supported {@link Charset charset}
*
* @throws UnsupportedEncodingException
* If the named encoding is not supported
@ -103,6 +102,7 @@ public class OutputStreamWriter extends Writer {
* Creates an OutputStreamWriter that uses the default character encoding.
*
* @param out An OutputStream
* @see Charset#defaultCharset()
*/
public OutputStreamWriter(OutputStream out) {
super(out);
@ -161,7 +161,7 @@ public class OutputStreamWriter extends Writer {
* @return The historical name of this encoding, or possibly
* {@code null} if the stream has been closed
*
* @see java.nio.charset.Charset
* @see Charset
*
* @revised 1.4
*/

View File

@ -45,8 +45,8 @@ import java.nio.charset.UnsupportedCharsetException;
* ({@code '\n'}) is written.
*
* <p> All characters printed by a {@code PrintStream} are converted into
* bytes using the given encoding or charset, or the platform's default
* character encoding if not specified.
* bytes using the given encoding or charset, or the default charset if not
* specified.
* The {@link PrintWriter} class should be used in situations that require
* writing characters rather than bytes.
*
@ -58,6 +58,7 @@ import java.nio.charset.UnsupportedCharsetException;
* @author Frank Yellin
* @author Mark Reinhold
* @since 1.0
* @see Charset#defaultCharset()
*/
public class PrintStream extends FilterOutputStream
@ -123,12 +124,13 @@ public class PrintStream extends FilterOutputStream
/**
* Creates a new print stream, without automatic line flushing, with the
* specified OutputStream. Characters written to the stream are converted
* to bytes using the platform's default character encoding.
* to bytes using the default charset.
*
* @param out The output stream to which values and objects will be
* printed
*
* @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
* @see Charset#defaultCharset()
*/
public PrintStream(OutputStream out) {
this(out, false);
@ -137,7 +139,7 @@ public class PrintStream extends FilterOutputStream
/**
* Creates a new print stream, with the specified OutputStream and line
* flushing. Characters written to the stream are converted to bytes using
* the platform's default character encoding.
* the default charset.
*
* @param out The output stream to which values and objects will be
* printed
@ -147,6 +149,7 @@ public class PrintStream extends FilterOutputStream
* character or byte ({@code '\n'}) is written
*
* @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
* @see Charset#defaultCharset()
*/
public PrintStream(OutputStream out, boolean autoFlush) {
this(autoFlush, requireNonNull(out, "Null output stream"));
@ -189,7 +192,7 @@ public class PrintStream extends FilterOutputStream
* whenever a byte array is written, one of the
* {@code println} methods is invoked, or a newline
* character or byte ({@code '\n'}) is written
* @param charset A {@linkplain java.nio.charset.Charset charset}
* @param charset A {@linkplain Charset charset}
*
* @since 10
*/
@ -205,7 +208,7 @@ public class PrintStream extends FilterOutputStream
* specified file name. This convenience constructor creates
* the necessary intermediate {@link java.io.OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
* {@linkplain Charset#defaultCharset() default charset}
* for this instance of the Java virtual machine.
*
* @param fileName
@ -224,6 +227,7 @@ public class PrintStream extends FilterOutputStream
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(fileName)} denies write
* access to the file
* @see Charset#defaultCharset()
*
* @since 1.5
*/
@ -245,8 +249,7 @@ public class PrintStream extends FilterOutputStream
* will be written to the file and is buffered.
*
* @param csn
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
* The name of a supported {@linkplain Charset charset}
*
* @throws FileNotFoundException
* If the given file object does not denote an existing, writable
@ -285,7 +288,7 @@ public class PrintStream extends FilterOutputStream
* will be written to the file and is buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
* A {@linkplain Charset charset}
*
* @throws IOException
* if an I/O error occurs while opening or creating the file
@ -306,7 +309,7 @@ public class PrintStream extends FilterOutputStream
* specified file. This convenience constructor creates the necessary
* intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
* which will encode characters using the {@linkplain
* java.nio.charset.Charset#defaultCharset() default charset} for this
* Charset#defaultCharset() default charset} for this
* instance of the Java virtual machine.
*
* @param file
@ -325,6 +328,7 @@ public class PrintStream extends FilterOutputStream
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(file.getPath())}
* denies write access to the file
* @see Charset#defaultCharset()
*
* @since 1.5
*/
@ -346,8 +350,7 @@ public class PrintStream extends FilterOutputStream
* file and is buffered.
*
* @param csn
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
* The name of a supported {@linkplain Charset charset}
*
* @throws FileNotFoundException
* If the given file object does not denote an existing, writable
@ -387,7 +390,7 @@ public class PrintStream extends FilterOutputStream
* file and is buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
* A {@linkplain Charset charset}
*
* @throws IOException
* if an I/O error occurs while opening or creating the file
@ -519,9 +522,8 @@ public class PrintStream extends FilterOutputStream
* invoked on the underlying output stream.
*
* <p> Note that the byte is written as given; to write a character that
* will be translated according to the platform's default character
* encoding, use the {@code print(char)} or {@code println(char)}
* methods.
* will be translated according to the default charset, use the
* {@code print(char)} or {@code println(char)} methods.
*
* @param b The byte to be written
* @see #print(char)
@ -552,9 +554,8 @@ public class PrintStream extends FilterOutputStream
* output stream.
*
* <p> Note that the bytes will be written as given; to write characters
* that will be translated according to the platform's default character
* encoding, use the {@code print(char)} or {@code println(char)}
* methods.
* that will be translated according to the default charset, use the
* {@code print(char)} or {@code println(char)} methods.
*
* @param buf A byte array
* @param off Offset from which to start taking bytes
@ -584,9 +585,8 @@ public class PrintStream extends FilterOutputStream
* invoked on the underlying output stream.
*
* <p> Note that the bytes will be written as given; to write characters
* that will be translated according to the platform's default character
* encoding, use the {@code print(char[])} or {@code println(char[])}
* methods.
* that will be translated according to the default charset, use the
* {@code print(char[])} or {@code println(char[])} methods.
*
* @apiNote
* Although declared to throw {@code IOException}, this method never
@ -622,9 +622,8 @@ public class PrintStream extends FilterOutputStream
* will be invoked.
*
* <p> Note that the bytes will be written as given; to write characters
* that will be translated according to the platform's default character
* encoding, use the {@code print(char[])} or {@code println(char[])}
* methods.
* that will be translated according to the default charset, use the
* {@code print(char[])} or {@code println(char[])} methods.
*
* @implSpec
* This method is equivalent to
@ -757,11 +756,12 @@ public class PrintStream extends FilterOutputStream
/**
* Prints a boolean value. The string produced by {@link
* java.lang.String#valueOf(boolean)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param b The {@code boolean} to be printed
* @see Charset#defaultCharset()
*/
public void print(boolean b) {
write(String.valueOf(b));
@ -770,10 +770,11 @@ public class PrintStream extends FilterOutputStream
/**
* Prints a character. The character is translated into one or more bytes
* according to the character encoding given to the constructor, or the
* platform's default character encoding if none specified. These bytes
* default charset if none specified. These bytes
* are written in exactly the manner of the {@link #write(int)} method.
*
* @param c The {@code char} to be printed
* @see Charset#defaultCharset()
*/
public void print(char c) {
write(String.valueOf(c));
@ -782,12 +783,13 @@ public class PrintStream extends FilterOutputStream
/**
* Prints an integer. The string produced by {@link
* java.lang.String#valueOf(int)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param i The {@code int} to be printed
* @see java.lang.Integer#toString(int)
* @see Charset#defaultCharset()
*/
public void print(int i) {
write(String.valueOf(i));
@ -796,12 +798,13 @@ public class PrintStream extends FilterOutputStream
/**
* Prints a long integer. The string produced by {@link
* java.lang.String#valueOf(long)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param l The {@code long} to be printed
* @see java.lang.Long#toString(long)
* @see Charset#defaultCharset()
*/
public void print(long l) {
write(String.valueOf(l));
@ -810,12 +813,13 @@ public class PrintStream extends FilterOutputStream
/**
* Prints a floating-point number. The string produced by {@link
* java.lang.String#valueOf(float)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param f The {@code float} to be printed
* @see java.lang.Float#toString(float)
* @see Charset#defaultCharset()
*/
public void print(float f) {
write(String.valueOf(f));
@ -824,12 +828,13 @@ public class PrintStream extends FilterOutputStream
/**
* Prints a double-precision floating-point number. The string produced by
* {@link java.lang.String#valueOf(double)} is translated into
* bytes according to the platform's default character encoding, and these
* bytes according to the default charset, and these
* bytes are written in exactly the manner of the {@link
* #write(int)} method.
*
* @param d The {@code double} to be printed
* @see java.lang.Double#toString(double)
* @see Charset#defaultCharset()
*/
public void print(double d) {
write(String.valueOf(d));
@ -838,10 +843,11 @@ public class PrintStream extends FilterOutputStream
/**
* Prints an array of characters. The characters are converted into bytes
* according to the character encoding given to the constructor, or the
* platform's default character encoding if none specified. These bytes
* default charset if none specified. These bytes
* are written in exactly the manner of the {@link #write(int)} method.
*
* @param s The array of chars to be printed
* @see Charset#defaultCharset()
*
* @throws NullPointerException If {@code s} is {@code null}
*/
@ -853,11 +859,12 @@ public class PrintStream extends FilterOutputStream
* Prints a string. If the argument is {@code null} then the string
* {@code "null"} is printed. Otherwise, the string's characters are
* converted into bytes according to the character encoding given to the
* constructor, or the platform's default character encoding if none
* constructor, or the default charset if none
* specified. These bytes are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param s The {@code String} to be printed
* @see Charset#defaultCharset()
*/
public void print(String s) {
write(String.valueOf(s));
@ -866,12 +873,13 @@ public class PrintStream extends FilterOutputStream
/**
* Prints an object. The string produced by the {@link
* java.lang.String#valueOf(Object)} method is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param obj The {@code Object} to be printed
* @see java.lang.Object#toString()
* @see Charset#defaultCharset()
*/
public void print(Object obj) {
write(String.valueOf(obj));

View File

@ -118,11 +118,12 @@ public class PrintWriter extends Writer {
* Creates a new PrintWriter, without automatic line flushing, from an
* existing OutputStream. This convenience constructor creates the
* necessary intermediate OutputStreamWriter, which will convert characters
* into bytes using the default character encoding.
* into bytes using the default charset.
*
* @param out An output stream
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
* @see OutputStreamWriter#OutputStreamWriter(OutputStream)
* @see Charset#defaultCharset()
*/
public PrintWriter(OutputStream out) {
this(out, false);
@ -132,14 +133,15 @@ public class PrintWriter extends Writer {
* Creates a new PrintWriter from an existing OutputStream. This
* convenience constructor creates the necessary intermediate
* OutputStreamWriter, which will convert characters into bytes using the
* default character encoding.
* default charset.
*
* @param out An output stream
* @param autoFlush A boolean; if true, the {@code println},
* {@code printf}, or {@code format} methods will
* flush the output buffer
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
* @see OutputStreamWriter#OutputStreamWriter(OutputStream)
* @see Charset#defaultCharset()
*/
public PrintWriter(OutputStream out, boolean autoFlush) {
this(out, autoFlush, Charset.defaultCharset());
@ -156,7 +158,7 @@ public class PrintWriter extends Writer {
* {@code printf}, or {@code format} methods will
* flush the output buffer
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
* A {@linkplain Charset charset}
*
* @since 10
*/
@ -172,9 +174,9 @@ public class PrintWriter extends Writer {
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file name. This convenience constructor creates the necessary
* intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
* intermediate {@link OutputStreamWriter OutputStreamWriter},
* which will encode characters using the {@linkplain
* java.nio.charset.Charset#defaultCharset() default charset} for this
* Charset#defaultCharset() default charset} for this
* instance of the Java virtual machine.
*
* @param fileName
@ -193,6 +195,7 @@ public class PrintWriter extends Writer {
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(fileName)} denies write
* access to the file
* @see Charset#defaultCharset()
*
* @since 1.5
*/
@ -212,7 +215,7 @@ public class PrintWriter extends Writer {
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file name and charset. This convenience constructor creates
* the necessary intermediate {@link java.io.OutputStreamWriter
* the necessary intermediate {@link OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
@ -223,8 +226,7 @@ public class PrintWriter extends Writer {
* written to the file and is buffered.
*
* @param csn
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
* The name of a supported {@linkplain Charset charset}
*
* @throws FileNotFoundException
* If the given string does not denote an existing, writable
@ -251,7 +253,7 @@ public class PrintWriter extends Writer {
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file name and charset. This convenience constructor creates
* the necessary intermediate {@link java.io.OutputStreamWriter
* the necessary intermediate {@link OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
@ -262,7 +264,7 @@ public class PrintWriter extends Writer {
* written to the file and is buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
* A {@linkplain Charset charset}
*
* @throws IOException
* if an I/O error occurs while opening or creating the file
@ -281,9 +283,9 @@ public class PrintWriter extends Writer {
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file. This convenience constructor creates the necessary
* intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
* intermediate {@link OutputStreamWriter OutputStreamWriter},
* which will encode characters using the {@linkplain
* java.nio.charset.Charset#defaultCharset() default charset} for this
* Charset#defaultCharset() default charset} for this
* instance of the Java virtual machine.
*
* @param file
@ -302,6 +304,7 @@ public class PrintWriter extends Writer {
* If a security manager is present and {@link
* SecurityManager#checkWrite checkWrite(file.getPath())}
* denies write access to the file
* @see Charset#defaultCharset()
*
* @since 1.5
*/
@ -313,7 +316,7 @@ public class PrintWriter extends Writer {
/**
* Creates a new PrintWriter, without automatic line flushing, with the
* specified file and charset. This convenience constructor creates the
* necessary intermediate {@link java.io.OutputStreamWriter
* necessary intermediate {@link OutputStreamWriter
* OutputStreamWriter}, which will encode characters using the provided
* charset.
*
@ -324,8 +327,7 @@ public class PrintWriter extends Writer {
* and is buffered.
*
* @param csn
* The name of a supported {@linkplain java.nio.charset.Charset
* charset}
* The name of a supported {@linkplain Charset charset}
*
* @throws FileNotFoundException
* If the given file object does not denote an existing, writable
@ -363,7 +365,7 @@ public class PrintWriter extends Writer {
* and is buffered.
*
* @param charset
* A {@linkplain java.nio.charset.Charset charset}
* A {@linkplain Charset charset}
*
* @throws IOException
* if an I/O error occurs while opening or creating the file
@ -580,11 +582,12 @@ public class PrintWriter extends Writer {
/**
* Prints a boolean value. The string produced by {@link
* java.lang.String#valueOf(boolean)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the {@link
* #write(int)} method.
*
* @param b The {@code boolean} to be printed
* @see Charset#defaultCharset()
*/
public void print(boolean b) {
write(String.valueOf(b));
@ -592,11 +595,12 @@ public class PrintWriter extends Writer {
/**
* Prints a character. The character is translated into one or more bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the {@link
* #write(int)} method.
*
* @param c The {@code char} to be printed
* @see Charset#defaultCharset()
*/
public void print(char c) {
write(c);
@ -605,12 +609,13 @@ public class PrintWriter extends Writer {
/**
* Prints an integer. The string produced by {@link
* java.lang.String#valueOf(int)} is translated into bytes according
* to the platform's default character encoding, and these bytes are
* to the default charset, and these bytes are
* written in exactly the manner of the {@link #write(int)}
* method.
*
* @param i The {@code int} to be printed
* @see java.lang.Integer#toString(int)
* @see Charset#defaultCharset()
*/
public void print(int i) {
write(String.valueOf(i));
@ -619,12 +624,13 @@ public class PrintWriter extends Writer {
/**
* Prints a long integer. The string produced by {@link
* java.lang.String#valueOf(long)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the {@link #write(int)}
* method.
*
* @param l The {@code long} to be printed
* @see java.lang.Long#toString(long)
* @see Charset#defaultCharset()
*/
public void print(long l) {
write(String.valueOf(l));
@ -633,12 +639,13 @@ public class PrintWriter extends Writer {
/**
* Prints a floating-point number. The string produced by {@link
* java.lang.String#valueOf(float)} is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the {@link #write(int)}
* method.
*
* @param f The {@code float} to be printed
* @see java.lang.Float#toString(float)
* @see Charset#defaultCharset()
*/
public void print(float f) {
write(String.valueOf(f));
@ -647,12 +654,13 @@ public class PrintWriter extends Writer {
/**
* Prints a double-precision floating-point number. The string produced by
* {@link java.lang.String#valueOf(double)} is translated into
* bytes according to the platform's default character encoding, and these
* bytes according to the default charset, and these
* bytes are written in exactly the manner of the {@link
* #write(int)} method.
*
* @param d The {@code double} to be printed
* @see java.lang.Double#toString(double)
* @see Charset#defaultCharset()
*/
public void print(double d) {
write(String.valueOf(d));
@ -660,11 +668,12 @@ public class PrintWriter extends Writer {
/**
* Prints an array of characters. The characters are converted into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the {@link #write(int)}
* method.
*
* @param s The array of chars to be printed
* @see Charset#defaultCharset()
*
* @throws NullPointerException If {@code s} is {@code null}
*/
@ -675,11 +684,12 @@ public class PrintWriter extends Writer {
/**
* Prints a string. If the argument is {@code null} then the string
* {@code "null"} is printed. Otherwise, the string's characters are
* converted into bytes according to the platform's default character
* encoding, and these bytes are written in exactly the manner of the
* converted into bytes according to the default charset,
* and these bytes are written in exactly the manner of the
* {@link #write(int)} method.
*
* @param s The {@code String} to be printed
* @see Charset#defaultCharset()
*/
public void print(String s) {
write(String.valueOf(s));
@ -688,12 +698,13 @@ public class PrintWriter extends Writer {
/**
* Prints an object. The string produced by the {@link
* java.lang.String#valueOf(Object)} method is translated into bytes
* according to the platform's default character encoding, and these bytes
* according to the default charset, and these bytes
* are written in exactly the manner of the {@link #write(int)}
* method.
*
* @param obj The {@code Object} to be printed
* @see java.lang.Object#toString()
* @see Charset#defaultCharset()
*/
public void print(Object obj) {
write(String.valueOf(obj));

View File

@ -797,6 +797,15 @@ public final class System {
* <td>The module name of the initial/main module</td></tr>
* <tr><th scope="row">{@systemProperty jdk.module.main.class}</th>
* <td>The main class name of the initial module</td></tr>
* <tr><th scope="row">{@systemProperty file.encoding}</th>
* <td>The name of the default charset, defaults to {@code UTF-8}.
* The property may be set on the command line to the value
* {@code UTF-8} or {@code COMPAT}. If set on the command line to
* the value {@code COMPAT} then the value is replaced with the
* value of the {@code native.encoding} property during startup.
* Setting the property to a value other than {@code UTF-8} or
* {@code COMPAT} leads to unspecified behavior.
* </td></tr>
* </tbody>
* </table>
*
@ -2112,9 +2121,9 @@ public final class System {
setIn0(new BufferedInputStream(fdIn));
// sun.stdout/err.encoding are set when the VM is associated with the terminal,
// thus they are equivalent to Console.charset(), otherwise the encoding
// defaults to Charset.defaultCharset()
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
// defaults to native.encoding
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding", StaticProperty.nativeEncoding())));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding", StaticProperty.nativeEncoding())));
// Setup Java signal handlers for HUP, TERM, and INT (where available).
Terminator.setup();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 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
@ -65,7 +65,7 @@ import java.util.Objects;
* will be replaced by the character(s) whose encoding would result
* in those consecutive bytes.
* The encoding scheme used to decode these characters may be specified,
* or if unspecified, the default encoding of the platform will be used.
* or if unspecified, the default charset will be used.
* </ul>
* <p>
* There are two possible ways in which this decoder could deal with
@ -74,6 +74,8 @@ import java.util.Objects;
* Which approach the decoder takes is left to the
* implementation.
*
* @see Charset#defaultCharset()
*
* @author Mark Chamness
* @author Michael McCloskey
* @since 1.2
@ -86,17 +88,17 @@ public class URLDecoder {
*/
private URLDecoder() {}
// The platform default encoding
// The default charset
static String dfltEncName = URLEncoder.dfltEncName;
/**
* Decodes a {@code x-www-form-urlencoded} string.
* The platform's default encoding is used to determine what characters
* The default charset is used to determine what characters
* are represented by any consecutive sequences of the form
* "<i>{@code %xy}</i>".
* @param s the {@code String} to decode
* @deprecated The resulting string may vary depending on the platform's
* default encoding. Instead, use the decode(String,String) method
* @deprecated The resulting string may vary depending on the
* default charset. Instead, use the decode(String,String) method
* to specify the encoding.
* @return the newly decoded {@code String}
*/
@ -108,7 +110,7 @@ public class URLDecoder {
try {
str = decode(s, dfltEncName);
} catch (UnsupportedEncodingException e) {
// The system should always have the platform default
// The system should always have the default charset
}
return str;
@ -120,7 +122,7 @@ public class URLDecoder {
*
* <p>
* This method behaves the same as {@linkplain decode(String s, Charset charset)}
* except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
* except that it will {@linkplain Charset#forName look up the charset}
* using the given encoding name.
*
* @implNote This implementation will throw an {@link java.lang.IllegalArgumentException}
@ -152,7 +154,7 @@ public class URLDecoder {
/**
* Decodes an {@code application/x-www-form-urlencoded} string using
* a specific {@linkplain java.nio.charset.Charset Charset}.
* a specific {@linkplain Charset Charset}.
* The supplied charset is used to determine
* what characters are represented by any consecutive sequences of the
* form "<i>{@code %xy}</i>".
@ -172,7 +174,7 @@ public class URLDecoder {
* @throws NullPointerException if {@code s} or {@code charset} is {@code null}
* @throws IllegalArgumentException if the implementation encounters illegal
* characters
* @see URLEncoder#encode(java.lang.String, java.nio.charset.Charset)
* @see URLEncoder#encode(java.lang.String, Charset)
* @since 10
*/
public static String decode(String s, Charset charset) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 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
@ -32,7 +32,8 @@ import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException ;
import java.util.BitSet;
import java.util.Objects;
import sun.security.action.GetPropertyAction;
import jdk.internal.util.StaticProperty;
/**
* Utility class for HTML form encoding. This class contains static methods
@ -60,7 +61,7 @@ import sun.security.action.GetPropertyAction;
* two-digit hexadecimal representation of the byte.
* The recommended encoding scheme to use is UTF-8. However,
* for compatibility reasons, if an encoding is not specified,
* then the default encoding of the platform is used.
* then the default charset is used.
* </ul>
*
* <p>
@ -70,6 +71,8 @@ import sun.security.action.GetPropertyAction;
* &#252; is encoded as two bytes C3 (hex) and BC (hex), and the
* character @ is encoded as one byte 40 (hex).
*
* @see Charset#defaultCharset()
*
* @author Herb Jellinek
* @since 1.0
*/
@ -134,7 +137,7 @@ public class URLEncoder {
dontNeedEncoding.set('.');
dontNeedEncoding.set('*');
dfltEncName = GetPropertyAction.privilegedGetProperty("file.encoding");
dfltEncName = StaticProperty.fileEncoding();
}
/**
@ -144,12 +147,12 @@ public class URLEncoder {
/**
* Translates a string into {@code x-www-form-urlencoded}
* format. This method uses the platform's default encoding
* format. This method uses the default charset
* as the encoding scheme to obtain the bytes for unsafe characters.
*
* @param s {@code String} to be translated.
* @deprecated The resulting string may vary depending on the platform's
* default encoding. Instead, use the encode(String,String)
* @deprecated The resulting string may vary depending on the
* default charset. Instead, use the encode(String,String)
* method to specify the encoding.
* @return the translated {@code String}.
*/
@ -161,7 +164,7 @@ public class URLEncoder {
try {
str = encode(s, dfltEncName);
} catch (UnsupportedEncodingException e) {
// The system should always have the platform default
// The system should always have the default charset
}
return str;
@ -172,7 +175,7 @@ public class URLEncoder {
* format using a specific encoding scheme.
* <p>
* This method behaves the same as {@linkplain #encode(String s, Charset charset)}
* except that it will {@linkplain java.nio.charset.Charset#forName look up the charset}
* except that it will {@linkplain Charset#forName look up the charset}
* using the given encoding name.
*
* @param s {@code String} to be translated.
@ -201,7 +204,7 @@ public class URLEncoder {
/**
* Translates a string into {@code application/x-www-form-urlencoded}
* format using a specific {@linkplain java.nio.charset.Charset Charset}.
* format using a specific {@linkplain Charset Charset}.
* This method uses the supplied charset to obtain the bytes for unsafe
* characters.
* <p>
@ -214,7 +217,7 @@ public class URLEncoder {
* @param charset the given charset
* @return the translated {@code String}.
* @throws NullPointerException if {@code s} or {@code charset} is {@code null}.
* @see URLDecoder#decode(java.lang.String, java.nio.charset.Charset)
* @see URLDecoder#decode(java.lang.String, Charset)
* @since 10
*/
public static String encode(String s, Charset charset) {

View File

@ -210,9 +210,8 @@ import java.util.TreeMap;
* <small>ZERO-WIDTH NON-BREAKING SPACE</small>.
*
* <p> Every instance of the Java virtual machine has a default charset, which
* may or may not be one of the standard charsets. The default charset is
* determined during virtual-machine startup and typically depends upon the
* locale and charset being used by the underlying operating system. </p>
* is {@code UTF-8} unless changed in an implementation specific manner. Refer to
* {@link #defaultCharset()} for more detail.
*
* <p> The {@link StandardCharsets} class defines constants for each of the
* standard charsets.
@ -592,11 +591,18 @@ public abstract class Charset
/**
* Returns the default charset of this Java virtual machine.
*
* <p> The default charset is determined during virtual-machine startup and
* typically depends upon the locale and charset of the underlying
* operating system.
* <p> The default charset is {@code UTF-8}, unless changed in an
* implementation specific manner.
*
* @implNote An implementation may override the default charset with
* the system property {@code file.encoding} on the command line. If the
* value is {@code COMPAT}, the default charset is derived from
* the {@code native.encoding} system property, which typically depends
* upon the locale and charset of the underlying operating system.
*
* @return A charset object for the default charset
* @see <a href="../../lang/System.html#file.encoding">file.encoding</a>
* @see <a href="../../lang/System.html#native.encoding">native.encoding</a>
*
* @since 1.5
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -557,10 +557,11 @@ public final class Scanner implements Iterator<String>, Closeable {
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* into characters using the
* {@linkplain Charset#defaultCharset() default charset}.
*
* @param source An input stream to be scanned
* @see Charset#defaultCharset()
*/
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
@ -629,11 +630,12 @@ public final class Scanner implements Iterator<String>, Closeable {
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* characters using the
* {@linkplain Charset#defaultCharset() default charset}.
*
* @param source A file to be scanned
* @throws FileNotFoundException if source is not found
* @see Charset#defaultCharset()
*/
public Scanner(File source) throws FileNotFoundException {
this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
@ -702,13 +704,14 @@ public final class Scanner implements Iterator<String>, Closeable {
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* characters using the
* {@linkplain Charset#defaultCharset() default charset}.
*
* @param source
* the path to the file to be scanned
* @throws IOException
* if an I/O error occurs opening source
* @see Charset#defaultCharset()
*
* @since 1.7
*/
@ -769,10 +772,11 @@ public final class Scanner implements Iterator<String>, Closeable {
/**
* Constructs a new {@code Scanner} that produces values scanned
* from the specified channel. Bytes from the source are converted into
* characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
* characters using the
* {@linkplain Charset#defaultCharset() default charset}.
*
* @param source A channel to scan
* @see Charset#defaultCharset()
*/
public Scanner(ReadableByteChannel source) {
this(makeReadable(Objects.requireNonNull(source, "source")),

View File

@ -50,6 +50,7 @@ public final class StaticProperty {
private static final String JDK_SERIAL_FILTER_FACTORY;
private static final String JAVA_IO_TMPDIR;
private static final String NATIVE_ENCODING;
private static final String FILE_ENCODING;
private StaticProperty() {}
@ -65,6 +66,7 @@ public final class StaticProperty {
JDK_SERIAL_FILTER = getProperty(props, "jdk.serialFilter", null);
JDK_SERIAL_FILTER_FACTORY = getProperty(props, "jdk.serialFilterFactory", null);
NATIVE_ENCODING = getProperty(props, "native.encoding");
FILE_ENCODING = getProperty(props, "file.encoding");
}
private static String getProperty(Properties props, String key) {
@ -212,4 +214,17 @@ public final class StaticProperty {
public static String nativeEncoding() {
return NATIVE_ENCODING;
}
/**
* Return the {@code file.encoding} system property.
*
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*
* @return the {@code file.encoding} system property
*/
public static String fileEncoding() {
return FILE_ENCODING;
}
}

View File

@ -68,8 +68,14 @@ public final class SystemProps {
: raw.propDefault(Raw._file_encoding_NDX));
put(props, "native.encoding", nativeEncoding);
// Add properties that have not been overridden on the cmdline
putIfAbsent(props, "file.encoding", nativeEncoding);
// "file.encoding" defaults to "UTF-8", unless specified in the command line
// where "COMPAT" designates the native encoding.
var fileEncoding = props.getOrDefault("file.encoding", "UTF-8");
if ("COMPAT".equals(fileEncoding)) {
put(props, "file.encoding", nativeEncoding);
} else {
putIfAbsent(props, "file.encoding", fileEncoding);
}
// Use platform values if not overridden by a commandline -Dkey=value
// In no particular order

View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8260265
* @summary Test file.encoding system property
* @library /test/lib
* @build jdk.test.lib.process.*
* @run testng FileEncodingTest
*/
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
import jdk.test.lib.process.ProcessTools;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class FileEncodingTest {
private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
@DataProvider
public Object[][] fileEncodingToDefault() {
return new Object[][] {
{"UTF-8", "UTF-8"},
{"ISO-8859-1", "ISO-8859-1"},
{"", "UTF-8"},
{"dummy", "UTF-8"},
{"COMPAT", "<should_be_replaced>"}
};
};
@Test(dataProvider = "fileEncodingToDefault")
public void testFileEncodingToDefault(String fileEncoding, String expected) throws Exception {
if (fileEncoding.equals("COMPAT")) {
if (IS_WINDOWS) {
// Only tests on English locales
if (Locale.getDefault().getLanguage().equals("en")) {
expected = "windows-1252";
} else {
System.out.println("Tests only run on Windows with English locales");
return;
}
} else {
expected = "US-ASCII";
}
}
var cmds = fileEncoding.isEmpty()
? List.of(FileEncodingTest.class.getName(), expected)
: List.of("-Dfile.encoding=" + fileEncoding, FileEncodingTest.class.getName(), expected);
var pb = ProcessTools.createTestJvm(cmds);
var env = pb.environment();
env.put("LANG", "C");
env.put("LC_ALL", "C");
ProcessTools.executeProcess(pb)
.outputTo(System.out)
.errorTo(System.err)
.shouldHaveExitValue(0);
}
public static void main(String... args) {
var def = Charset.defaultCharset().name();
var expected = args[0];
System.out.println("Default Charset: " + def + ", expected: " + expected);
if (!def.equals(expected)) {
throw new RuntimeException("default charset is not the one expected.");
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 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
@ -23,16 +23,19 @@
/*
* @test
* @bug 8003228
* @bug 8003228 8260265
* @summary Test the value of sun.jnu.encoding on Mac
* @requires (os.family == "mac")
* @library /test/lib
* @build jdk.test.lib.process.*
* ExpectedEncoding
* @run main MacJNUEncoding US-ASCII UTF-8 C
* @run main MacJNUEncoding UTF-8 UTF-8 C
* @run main MacJNUEncoding US-ASCII UTF-8 C COMPAT
* @run main MacJNUEncoding UTF-8 UTF-8 en_US.UTF-8
* @run main MacJNUEncoding UTF-8 UTF-8 en_US.UTF-8 COMPAT
*/
import java.util.List;
import java.util.Map;
import jdk.test.lib.process.ProcessTools;
@ -40,17 +43,19 @@ import jdk.test.lib.process.ProcessTools;
public class MacJNUEncoding {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
if (args.length != 3 && args.length != 4) {
System.out.println("Usage:");
System.out.println(" java MacJNUEncoding"
+ " <expected file.encoding> <expected sun.jnu.encoding> <locale>");
+ " <expected file.encoding> <expected sun.jnu.encoding> <locale> [<user's file.encoding>]");
throw new IllegalArgumentException("missing arguments");
}
final String locale = args[2];
System.out.println("Running test for locale: " + locale);
ProcessBuilder pb = ProcessTools.createTestJvm(
ExpectedEncoding.class.getName(), args[0], args[1]);
var cmds = (args.length == 4)
? List.of("-Dfile.encoding=" + args[3], ExpectedEncoding.class.getName(), args[0], args[1])
: List.of(ExpectedEncoding.class.getName(), args[0], args[1]);
ProcessBuilder pb = ProcessTools.createTestJvm(cmds);
Map<String, String> env = pb.environment();
env.put("LANG", locale);
env.put("LC_ALL", locale);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@ -25,17 +25,20 @@ import java.util.*;
/*
* @test
* @bug 8011194
* @bug 8011194 8260265
* @summary Test value of file.encoding for corresponding value of LANG, etc
* @library ../../../../tools/launcher/ ../
* @modules jdk.compiler
* @build TestHelper TestFileEncoding ExpectedEncoding
* @run main TestFileEncoding UTF-8
* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding
* @run main/othervm -DuserEncoding=COMPAT TestFileEncoding UTF-8
* @run main TestFileEncoding UTF-8 en_US.UTF-8
* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding en_US.UTF-8
* @run main TestFileEncoding US-ASCII C
* @run main/othervm -DuserEncoding=COMPAT TestFileEncoding UTF-8 en_US.UTF-8
* @run main TestFileEncoding UTF-8 C
* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding C
* @run main/othervm -DuserEncoding=COMPAT TestFileEncoding US-ASCII C
* @author Brent Christian
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -23,15 +23,12 @@
/* @test
* @bug 4473201 4696726 4652234 4482298 4784385 4966197 4267354 5015668
6911753 8071447 8186751 8242541
6911753 8071447 8186751 8242541 8260265
* @summary Check that registered charsets are actually registered
* @modules jdk.charsets
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.util.*;
public class RegisteredCharsets {
@ -206,7 +203,7 @@ public class RegisteredCharsets {
new String[] {"ascii","ANSI_X3.4-1968",
"iso-ir-6","ANSI_X3.4-1986", "ISO_646.irv:1991",
"ASCII", "ISO646-US","us","IBM367","cp367",
"csASCII", "default"});
"csASCII"});
aliasCheck("UTF-8",
new String[] {
@ -1298,5 +1295,13 @@ public class RegisteredCharsets {
new String[] {
"MacDingbat" // JDK historical
});
// Check UnsupportedCharsetException is thrown for the name "default"
try {
Charset.forName("default");
throw new RuntimeException("UnsupportedCharsetException was not thrown for Charset.forName(\"default\")");
} catch (UnsupportedCharsetException uce) {
// success
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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
@ -22,7 +22,7 @@
*/
/* @test
* @bug 4454622
* @bug 4454622 8260265
* @summary Check if all supported sun.io encoding names are supported in nio.charset
* @modules jdk.charsets
*/
@ -89,7 +89,7 @@ public class SunioAlias {
aliasTable.put("iso_646.irv:1983", "ASCII"); // Linux POSIX locale
aliasTable.put("ansi_x3.4-1968", "ASCII"); // Caldera linux
aliasTable.put("iso646-us", "ASCII");
aliasTable.put("default", "ASCII"); // compatibility with obsolete "Default" converters
// aliasTable.put("default", "ASCII"); // compatibility with obsolete "Default" converters
aliasTable.put("ascii7", "ASCII"); // compatibility with obsolete "Default" converters
// Core encodings

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 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
@ -23,7 +23,7 @@
/*
* @test
* @bug 5030265
* @bug 5030265 8260265
* @modules jdk.compiler
* jdk.zipfs
* @compile -XDignore.symbol.file UnicodeTest.java
@ -163,7 +163,7 @@ public class UnicodeTest extends TestHelper {
return commandLineClassName;
}
private static final String defaultEncoding = Charset.defaultCharset().name();
private static final String filePathEncoding = System.getProperty("sun.jnu.encoding");
// language names taken from java.util.Locale.getDisplayLanguage for the respective language
private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
@ -230,7 +230,7 @@ public class UnicodeTest extends TestHelper {
int column = isWindows ? 2 : 1;
for (int i = 0; i < names.length; i++) {
if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
if (names[i][0].equalsIgnoreCase(filePathEncoding)) {
return names[i][column];
}
}
@ -238,7 +238,7 @@ public class UnicodeTest extends TestHelper {
}
private static boolean hasUnicodeFileSystem() {
return (isWindows) ? true : defaultEncoding.equalsIgnoreCase("UTF-8");
return (isWindows) ? true : filePathEncoding.equalsIgnoreCase("UTF-8");
}
private static void generateSource(String thisClass, String otherClass) throws Exception {