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

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