8234147: Avoid looking up standard charsets in core libraries

Reviewed-by: alanb
This commit is contained in:
Ivan Gerasimov 2019-12-01 15:29:37 -08:00
parent 4e64af81a2
commit cd589d8469
36 changed files with 200 additions and 237 deletions

View file

@ -70,12 +70,8 @@ public class InputStreamReader extends Reader {
*/
public InputStreamReader(InputStream in) {
super(in);
try {
sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
} catch (UnsupportedEncodingException e) {
// The default encoding should always be available
throw new Error(e);
}
sd = StreamDecoder.forInputStreamReader(in, this,
Charset.defaultCharset()); // ## check lock object
}
/**

View file

@ -106,11 +106,8 @@ public class OutputStreamWriter extends Writer {
*/
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
se = StreamEncoder.forOutputStreamWriter(out, this,
Charset.defaultCharset());
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2019, 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
@ -151,7 +151,7 @@ class CharacterName {
}
public int getCodePoint(String name) {
byte[] bname = name.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1);
byte[] bname = name.getBytes(sun.nio.cs.ISO_8859_1.INSTANCE);
int hsh = hashN(bname, 0, bname.length);
int idx = hsIndices[(hsh & 0x7fffffff) % hsIndices.length];
while (idx != -1) {

View file

@ -31,7 +31,6 @@ import java.util.Objects;
import java.util.Scanner;
import java.security.AccessController;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.ObjectStreamException;
import java.io.ObjectStreamField;
import java.io.IOException;
@ -52,6 +51,7 @@ import jdk.internal.access.SharedSecrets;
import sun.security.action.*;
import sun.net.InetAddressCachePolicy;
import sun.net.util.IPAddressUtil;
import sun.nio.cs.UTF_8;
/**
* This class represents an Internet Protocol (IP) address.
@ -984,7 +984,9 @@ public class InetAddress implements java.io.Serializable {
String hostEntry;
String host = null;
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile),
UTF_8.INSTANCE))
{
while (hostsFileScanner.hasNextLine()) {
hostEntry = hostsFileScanner.nextLine();
if (!hostEntry.startsWith("#")) {
@ -997,7 +999,7 @@ public class InetAddress implements java.io.Serializable {
}
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
throw new UnknownHostException("Unable to resolve address "
+ Arrays.toString(addr) + " as hosts file " + hostsFile
+ " not found ");
@ -1033,7 +1035,9 @@ public class InetAddress implements java.io.Serializable {
ArrayList<InetAddress> inetAddresses = null;
// lookup the file and create a list InetAddress for the specified host
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile), "UTF-8")) {
try (Scanner hostsFileScanner = new Scanner(new File(hostsFile),
UTF_8.INSTANCE))
{
while (hostsFileScanner.hasNextLine()) {
hostEntry = hostsFileScanner.nextLine();
if (!hostEntry.startsWith("#")) {
@ -1052,7 +1056,7 @@ public class InetAddress implements java.io.Serializable {
}
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
throw new UnknownHostException("Unable to resolve host " + host
+ " as hosts file " + hostsFile + " not found ");
}

View file

@ -28,6 +28,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.util.Iterator;
@ -166,18 +167,10 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
return false;
out.write(1);
out.write(userName.length());
try {
out.write(userName.getBytes("ISO-8859-1"));
} catch (java.io.UnsupportedEncodingException uee) {
assert false;
}
out.write(userName.getBytes(StandardCharsets.ISO_8859_1));
if (password != null) {
out.write(password.length());
try {
out.write(password.getBytes("ISO-8859-1"));
} catch (java.io.UnsupportedEncodingException uee) {
assert false;
}
out.write(password.getBytes(StandardCharsets.ISO_8859_1));
} else
out.write(0);
out.flush();
@ -208,11 +201,7 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
out.write((endpoint.getPort() >> 0) & 0xff);
out.write(endpoint.getAddress().getAddress());
String userName = getUserName();
try {
out.write(userName.getBytes("ISO-8859-1"));
} catch (java.io.UnsupportedEncodingException uee) {
assert false;
}
out.write(userName.getBytes(StandardCharsets.ISO_8859_1));
out.write(0);
out.flush();
byte[] data = new byte[8];
@ -427,11 +416,7 @@ class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
if (epoint.isUnresolved()) {
out.write(DOMAIN_NAME);
out.write(epoint.getHostName().length());
try {
out.write(epoint.getHostName().getBytes("ISO-8859-1"));
} catch (java.io.UnsupportedEncodingException uee) {
assert false;
}
out.write(epoint.getHostName().getBytes(StandardCharsets.ISO_8859_1));
out.write((epoint.getPort() >> 8) & 0xff);
out.write((epoint.getPort() >> 0) & 0xff);
} else if (epoint.getAddress() instanceof Inet6Address) {

View file

@ -42,11 +42,11 @@ import java.text.Normalizer;
import jdk.internal.access.JavaNetUriAccess;
import jdk.internal.access.SharedSecrets;
import sun.nio.cs.ThreadLocalCoders;
import sun.nio.cs.UTF_8;
import java.lang.Character; // for javadoc
import java.lang.NullPointerException; // for javadoc
/**
* Represents a Uniform Resource Identifier (URI) reference.
*
@ -2739,7 +2739,7 @@ public final class URI
private static void appendEncoded(StringBuilder sb, char c) {
ByteBuffer bb = null;
try {
bb = ThreadLocalCoders.encoderFor("UTF-8")
bb = ThreadLocalCoders.encoderFor(UTF_8.INSTANCE)
.encode(CharBuffer.wrap("" + c));
} catch (CharacterCodingException x) {
assert false;
@ -2807,7 +2807,7 @@ public final class URI
String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
ByteBuffer bb = null;
try {
bb = ThreadLocalCoders.encoderFor("UTF-8")
bb = ThreadLocalCoders.encoderFor(UTF_8.INSTANCE)
.encode(CharBuffer.wrap(ns));
} catch (CharacterCodingException x) {
assert false;
@ -2865,7 +2865,7 @@ public final class URI
StringBuilder sb = new StringBuilder(n);
ByteBuffer bb = ByteBuffer.allocate(n);
CharBuffer cb = CharBuffer.allocate(n);
CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
CharsetDecoder dec = ThreadLocalCoders.decoderFor(UTF_8.INSTANCE)
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, 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
@ -24,6 +24,10 @@
*/
package java.nio.file;
import sun.nio.cs.ISO_8859_1;
import sun.nio.cs.UTF_8;
import sun.nio.cs.US_ASCII;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UncheckedIOException;
@ -32,7 +36,6 @@ import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import java.util.Spliterator;
@ -66,9 +69,9 @@ final class FileChannelLinesSpliterator implements Spliterator<String> {
static final Set<String> SUPPORTED_CHARSET_NAMES;
static {
SUPPORTED_CHARSET_NAMES = new HashSet<>();
SUPPORTED_CHARSET_NAMES.add(StandardCharsets.UTF_8.name());
SUPPORTED_CHARSET_NAMES.add(StandardCharsets.ISO_8859_1.name());
SUPPORTED_CHARSET_NAMES.add(StandardCharsets.US_ASCII.name());
SUPPORTED_CHARSET_NAMES.add(UTF_8.INSTANCE.name());
SUPPORTED_CHARSET_NAMES.add(ISO_8859_1.INSTANCE.name());
SUPPORTED_CHARSET_NAMES.add(US_ASCII.INSTANCE.name());
}
private final FileChannel fc;

View file

@ -79,6 +79,7 @@ import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import sun.nio.ch.FileChannelImpl;
import sun.nio.cs.UTF_8;
import sun.nio.fs.AbstractFileSystemProvider;
/**
@ -2944,7 +2945,7 @@ public final class Files {
* @since 1.8
*/
public static BufferedReader newBufferedReader(Path path) throws IOException {
return newBufferedReader(path, StandardCharsets.UTF_8);
return newBufferedReader(path, UTF_8.INSTANCE);
}
/**
@ -3036,7 +3037,7 @@ public final class Files {
public static BufferedWriter newBufferedWriter(Path path, OpenOption... options)
throws IOException
{
return newBufferedWriter(path, StandardCharsets.UTF_8, options);
return newBufferedWriter(path, UTF_8.INSTANCE, options);
}
/**
@ -3305,7 +3306,7 @@ public final class Files {
* @since 11
*/
public static String readString(Path path) throws IOException {
return readString(path, StandardCharsets.UTF_8);
return readString(path, UTF_8.INSTANCE);
}
/**
@ -3430,7 +3431,7 @@ public final class Files {
* @since 1.8
*/
public static List<String> readAllLines(Path path) throws IOException {
return readAllLines(path, StandardCharsets.UTF_8);
return readAllLines(path, UTF_8.INSTANCE);
}
/**
@ -3601,7 +3602,7 @@ public final class Files {
OpenOption... options)
throws IOException
{
return write(path, lines, StandardCharsets.UTF_8, options);
return write(path, lines, UTF_8.INSTANCE, options);
}
/**
@ -3641,7 +3642,7 @@ public final class Files {
public static Path writeString(Path path, CharSequence csq, OpenOption... options)
throws IOException
{
return writeString(path, csq, StandardCharsets.UTF_8, options);
return writeString(path, csq, UTF_8.INSTANCE, options);
}
/**
@ -4188,6 +4189,6 @@ public final class Files {
* @since 1.8
*/
public static Stream<String> lines(Path path) throws IOException {
return lines(path, StandardCharsets.UTF_8);
return lines(path, UTF_8.INSTANCE);
}
}

View file

@ -31,6 +31,9 @@ import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import sun.nio.cs.ISO_8859_1;
import jdk.internal.HotSpotIntrinsicCandidate;
/**
@ -584,7 +587,7 @@ public class Base64 {
* if {@code src} is not in valid Base64 scheme
*/
public byte[] decode(String src) {
return decode(src.getBytes(StandardCharsets.ISO_8859_1));
return decode(src.getBytes(ISO_8859_1.INSTANCE));
}
/**

View file

@ -46,6 +46,9 @@ import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import sun.nio.cs.ISO_8859_1;
import sun.nio.cs.UTF_8;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.ArraysSupport;
@ -909,7 +912,7 @@ public class Properties extends Hashtable<Object,Object> {
public void store(OutputStream out, String comments)
throws IOException
{
store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
store0(new BufferedWriter(new OutputStreamWriter(out, ISO_8859_1.INSTANCE)),
comments,
true);
}
@ -1001,7 +1004,7 @@ public class Properties extends Hashtable<Object,Object> {
public void storeToXML(OutputStream os, String comment)
throws IOException
{
storeToXML(os, comment, "UTF-8");
storeToXML(os, comment, UTF_8.INSTANCE);
}
/**

View file

@ -44,8 +44,8 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnmappableCharacterException;
import sun.nio.cs.ISO_8859_1;
import sun.security.action.GetPropertyAction;
import sun.util.PropertyResourceBundleCharset;
import sun.util.ResourceBundleEnumeration;
@ -176,7 +176,7 @@ public class PropertyResourceBundle extends ResourceBundle {
public PropertyResourceBundle (InputStream stream) throws IOException {
this(new InputStreamReader(stream,
"ISO-8859-1".equals(encoding) ?
StandardCharsets.ISO_8859_1.newDecoder() :
ISO_8859_1.INSTANCE.newDecoder() :
new PropertyResourceBundleCharset("UTF-8".equals(encoding)).newDecoder()));
}

View file

@ -45,6 +45,8 @@ import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import sun.nio.cs.UTF_8;
import jdk.internal.loader.BootLoader;
import jdk.internal.loader.ClassLoaders;
import jdk.internal.access.JavaLangAccess;
@ -55,7 +57,6 @@ import jdk.internal.module.ServicesCatalog.ServiceProvider;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
/**
* A facility to load implementations of a service.
*
@ -1164,7 +1165,7 @@ public final class ServiceLoader<S>
uc.setUseCaches(false);
try (InputStream in = uc.getInputStream();
BufferedReader r
= new BufferedReader(new InputStreamReader(in, "utf-8")))
= new BufferedReader(new InputStreamReader(in, UTF_8.INSTANCE)))
{
int lc = 1;
while ((lc = parseLine(u, r, lc, names)) >= 0);

View file

@ -36,9 +36,9 @@ import java.util.Set;
import jdk.internal.misc.VM;
import jdk.internal.vm.annotation.Stable;
import sun.util.logging.PlatformLogger;
import static java.nio.charset.StandardCharsets.UTF_8;
import sun.nio.cs.UTF_8;
import sun.util.logging.PlatformLogger;
/**
* The Attributes class maps Manifest attribute names to associated string
@ -337,7 +337,7 @@ public class Attributes implements Map<Object,Object>, Cloneable {
buffer.append(vername);
buffer.append(": ");
buffer.append(version);
out.write(buffer.toString().getBytes(UTF_8));
out.write(buffer.toString().getBytes(UTF_8.INSTANCE));
Manifest.println(out);
}
@ -399,7 +399,7 @@ public class Attributes implements Map<Object,Object>, Cloneable {
lastline = buf;
continue;
}
value = new String(buf, 0, buf.length, UTF_8);
value = new String(buf, 0, buf.length, UTF_8.INSTANCE);
lastline = null;
} else {
while (lbuf[i++] != ':') {
@ -412,13 +412,13 @@ public class Attributes implements Map<Object,Object>, Cloneable {
throw new IOException("invalid header field ("
+ Manifest.getErrorPosition(filename, lineNumber) + ")");
}
name = new String(lbuf, 0, i - 2, UTF_8);
name = new String(lbuf, 0, i - 2, UTF_8.INSTANCE);
if (is.peek() == ' ') {
lastline = new byte[len - i];
System.arraycopy(lbuf, i, lastline, 0, len - i);
continue;
}
value = new String(lbuf, i, len - i, UTF_8);
value = new String(lbuf, i, len - i, UTF_8.INSTANCE);
}
try {
if ((putValue(name, value) != null) && (!lineContinued)) {

View file

@ -33,10 +33,9 @@ import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import sun.nio.cs.UTF_8;
import sun.security.util.SecurityProperties;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* The Manifest class is used to maintain Manifest entry names and their
* associated Attributes. There are main Manifest Attributes as well as
@ -237,7 +236,7 @@ public class Manifest implements Cloneable {
*/
static void println72(OutputStream out, String line) throws IOException {
if (!line.isEmpty()) {
byte[] lineBytes = line.getBytes(UTF_8);
byte[] lineBytes = line.getBytes(UTF_8.INSTANCE);
int length = lineBytes.length;
// first line can hold one byte more than subsequent lines which
// start with a continuation line break space
@ -337,7 +336,7 @@ public class Manifest implements Cloneable {
lastline = buf;
continue;
}
name = new String(buf, 0, buf.length, UTF_8);
name = new String(buf, 0, buf.length, UTF_8.INSTANCE);
lastline = null;
}
Attributes attr = getAttributes(name);
@ -362,11 +361,7 @@ public class Manifest implements Cloneable {
if (toLower(lbuf[0]) == 'n' && toLower(lbuf[1]) == 'a' &&
toLower(lbuf[2]) == 'm' && toLower(lbuf[3]) == 'e' &&
lbuf[4] == ':' && lbuf[5] == ' ') {
try {
return new String(lbuf, 6, len - 6, UTF_8);
}
catch (Exception e) {
}
return new String(lbuf, 6, len - 6, UTF_8.INSTANCE);
}
return null;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2019, 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
@ -33,7 +33,7 @@ import java.nio.charset.CharsetEncoder;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import static java.nio.charset.StandardCharsets.UTF_8;
import sun.nio.cs.UTF_8;
/**
* Utility class for zipfile name and comment decoding and encoding
@ -67,10 +67,10 @@ class ZipCoder {
}
// UTF_8.ArrayEn/Decoder is stateless, so make it singleton.
private static ZipCoder utf8 = new UTF8(UTF_8);
private static ZipCoder utf8 = new UTF8(UTF_8.INSTANCE);
public static ZipCoder get(Charset charset) {
if (charset == UTF_8)
if (charset == UTF_8.INSTANCE)
return utf8;
return new ZipCoder(charset);
}

View file

@ -34,7 +34,6 @@ import java.io.RandomAccessFile;
import java.io.UncheckedIOException;
import java.lang.ref.Cleaner.Cleanable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.InvalidPathException;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;
@ -65,6 +64,7 @@ import jdk.internal.misc.VM;
import jdk.internal.perf.PerfCounter;
import jdk.internal.ref.CleanerFactory;
import jdk.internal.vm.annotation.Stable;
import sun.nio.cs.UTF_8;
import static java.util.zip.ZipConstants64.*;
import static java.util.zip.ZipUtils.*;
@ -165,7 +165,7 @@ public class ZipFile implements ZipConstants, Closeable {
* @since 1.3
*/
public ZipFile(File file, int mode) throws IOException {
this(file, mode, StandardCharsets.UTF_8);
this(file, mode, UTF_8.INSTANCE);
}
/**
@ -1042,7 +1042,7 @@ public class ZipFile implements ZipConstants, Closeable {
for (int i = 0; i < names.length; i++) {
int pos = zsrc.metanames[i];
names[i] = new String(cen, pos + CENHDR, CENNAM(cen, pos),
StandardCharsets.UTF_8);
UTF_8.INSTANCE);
}
return names;
}

View file

@ -30,7 +30,9 @@ import java.io.IOException;
import java.io.EOFException;
import java.io.PushbackInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import sun.nio.cs.UTF_8;
import static java.util.zip.ZipConstants64.*;
import static java.util.zip.ZipUtils.*;
@ -77,7 +79,7 @@ public class ZipInputStream extends InflaterInputStream implements ZipConstants
* @param in the actual input stream
*/
public ZipInputStream(InputStream in) {
this(in, StandardCharsets.UTF_8);
this(in, UTF_8.INSTANCE);
}
/**

View file

@ -28,11 +28,11 @@ package java.util.zip;
import java.io.OutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Vector;
import java.util.HashSet;
import static java.util.zip.ZipConstants64.*;
import static java.util.zip.ZipUtils.*;
import sun.nio.cs.UTF_8;
import sun.security.action.GetPropertyAction;
/**
@ -116,7 +116,7 @@ public class ZipOutputStream extends DeflaterOutputStream implements ZipConstant
* @param out the actual output stream
*/
public ZipOutputStream(OutputStream out) {
this(out, StandardCharsets.UTF_8);
this(out, UTF_8.INSTANCE);
}
/**