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

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 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
@ -26,19 +26,20 @@
package sun.net.www;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import sun.nio.cs.ThreadLocalCoders;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import sun.nio.cs.ThreadLocalCoders;
import sun.nio.cs.UTF_8;
/**
* A class that contains useful routines common to sun.net.www
* @author Mike McCloskey
@ -176,7 +177,7 @@ public final class ParseUtil {
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.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
@ -496,7 +497,7 @@ public final class ParseUtil {
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;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@ -29,6 +29,7 @@ import java.util.*;
import sun.net.*;
import sun.net.www.*;
import sun.nio.cs.US_ASCII;
/**
* A <code>ChunkedInputStream</code> provides a stream for reading a body of
@ -307,7 +308,8 @@ class ChunkedInputStream extends InputStream implements Hurryable {
/*
* Extract the chunk size from the header (ignoring extensions).
*/
String header = new String(rawData, rawPos, pos-rawPos+1, "US-ASCII");
String header = new String(rawData, rawPos, pos-rawPos+1,
US_ASCII.INSTANCE);
for (i=0; i < header.length(); i++) {
if (Character.digit(header.charAt(i), 16) == -1)
break;
@ -461,7 +463,8 @@ class ChunkedInputStream extends InputStream implements Hurryable {
* Extract any tailers and append them to the message
* headers.
*/
String trailer = new String(rawData, rawPos, pos-rawPos, "US-ASCII");
String trailer = new String(rawData, rawPos, pos-rawPos,
US_ASCII.INSTANCE);
i = trailer.indexOf(':');
if (i == -1) {
throw new IOException("Malformed tailer - format should be key:value");

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 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
@ -26,6 +26,8 @@ package sun.net.www.http;
import java.io.*;
import sun.nio.cs.US_ASCII;
/**
* OutputStream that sends the output to the underlying stream using chunked
* encoding as specified in RFC 2068.
@ -67,20 +69,15 @@ public class ChunkedOutputStream extends PrintStream {
}
/* return a header for a particular chunk size */
private static byte[] getHeader(int size){
try {
String hexStr = Integer.toHexString(size);
byte[] hexBytes = hexStr.getBytes("US-ASCII");
byte[] header = new byte[getHeaderSize(size)];
for (int i=0; i<hexBytes.length; i++)
header[i] = hexBytes[i];
header[hexBytes.length] = CRLF[0];
header[hexBytes.length+1] = CRLF[1];
return header;
} catch (java.io.UnsupportedEncodingException e) {
/* This should never happen */
throw new InternalError(e.getMessage(), e);
}
private static byte[] getHeader(int size) {
String hexStr = Integer.toHexString(size);
byte[] hexBytes = hexStr.getBytes(US_ASCII.INSTANCE);
byte[] header = new byte[getHeaderSize(size)];
for (int i=0; i<hexBytes.length; i++)
header[i] = hexBytes[i];
header[hexBytes.length] = CRLF[0];
header[hexBytes.length+1] = CRLF[1];
return header;
}
public ChunkedOutputStream(PrintStream o) {

View file

@ -38,8 +38,8 @@ import java.util.Arrays;
import java.util.Base64;
import java.util.Objects;
import sun.net.www.HeaderParser;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import sun.nio.cs.ISO_8859_1;
import sun.nio.cs.UTF_8;
/**
* BasicAuthentication: Encapsulate an http server authentication using
@ -101,10 +101,11 @@ class BasicAuthentication extends AuthenticationInfo {
char[] password = pw.getPassword();
CharBuffer cbuf = CharBuffer.allocate(plain.length() + password.length);
cbuf.put(plain).put(password).flip();
Charset charset = isUTF8 ? UTF_8 : ISO_8859_1;
Charset charset = isUTF8 ? UTF_8.INSTANCE : ISO_8859_1.INSTANCE;
ByteBuffer buf = charset.encode(cbuf);
ByteBuffer enc = Base64.getEncoder().encode(buf);
String ret = "Basic " + new String(enc.array(), enc.position(), enc.remaining(), ISO_8859_1);
String ret = "Basic " + new String(enc.array(), enc.position(), enc.remaining(),
ISO_8859_1.INSTANCE);
Arrays.fill(buf.array(), (byte) 0);
Arrays.fill(enc.array(), (byte) 0);
Arrays.fill(cbuf.array(), (char) 0);

View file

@ -26,19 +26,21 @@
package sun.net.www.protocol.http;
import java.io.*;
import java.net.URL;
import java.net.ProtocolException;
import java.net.PasswordAuthentication;
import java.util.Arrays;
import java.util.Random;
import sun.net.www.HeaderParser;
import sun.net.NetProperties;
import java.net.ProtocolException;
import java.net.URL;
import java.security.AccessController;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivilegedAction;
import java.security.AccessController;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;
import sun.net.NetProperties;
import sun.net.www.HeaderParser;
import sun.nio.cs.ISO_8859_1;
import static sun.net.www.protocol.http.HttpURLConnection.HTTP_CONNECT;
/**
@ -521,11 +523,7 @@ class DigestAuthentication extends AuthenticationInfo {
};
private String encode(String src, char[] passwd, MessageDigest md) {
try {
md.update(src.getBytes("ISO-8859-1"));
} catch (java.io.UnsupportedEncodingException uee) {
assert false;
}
md.update(src.getBytes(ISO_8859_1.INSTANCE));
if (passwd != null) {
byte[] passwdBytes = new byte[passwd.length];
for (int i=0; i<passwd.length; i++)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
@ -87,17 +87,17 @@ public class ThreadLocalCoders {
private static Cache decoderCache = new Cache(CACHE_SIZE) {
boolean hasName(Object ob, Object name) {
if (name instanceof String)
return (((CharsetDecoder)ob).charset().name().equals(name));
if (name instanceof Charset)
return ((CharsetDecoder)ob).charset().equals(name);
if (name instanceof String)
return (((CharsetDecoder)ob).charset().name().equals(name));
return false;
}
Object create(Object name) {
if (name instanceof String)
return Charset.forName((String)name).newDecoder();
if (name instanceof Charset)
return ((Charset)name).newDecoder();
if (name instanceof String)
return Charset.forName((String)name).newDecoder();
assert false;
return null;
}
@ -111,17 +111,17 @@ public class ThreadLocalCoders {
private static Cache encoderCache = new Cache(CACHE_SIZE) {
boolean hasName(Object ob, Object name) {
if (name instanceof String)
return (((CharsetEncoder)ob).charset().name().equals(name));
if (name instanceof Charset)
return ((CharsetEncoder)ob).charset().equals(name);
if (name instanceof String)
return (((CharsetEncoder)ob).charset().name().equals(name));
return false;
}
Object create(Object name) {
if (name instanceof String)
return Charset.forName((String)name).newEncoder();
if (name instanceof Charset)
return ((Charset)name).newEncoder();
if (name instanceof String)
return Charset.forName((String)name).newEncoder();
assert false;
return null;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2016, 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
@ -25,6 +25,9 @@
package sun.util;
import sun.nio.cs.ISO_8859_1;
import sun.nio.cs.UTF_8;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
@ -70,7 +73,7 @@ public class PropertyResourceBundleCharset extends Charset {
private final class PropertiesFileDecoder extends CharsetDecoder {
private CharsetDecoder cdUTF_8 = StandardCharsets.UTF_8.newDecoder()
private CharsetDecoder cdUTF_8 = UTF_8.INSTANCE.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
private CharsetDecoder cdISO_8859_1 = null;
@ -98,7 +101,7 @@ public class PropertyResourceBundleCharset extends Charset {
assert cr.isMalformed() || cr.isUnmappable();
in.reset();
out.reset();
cdISO_8859_1 = StandardCharsets.ISO_8859_1.newDecoder();
cdISO_8859_1 = ISO_8859_1.INSTANCE.newDecoder();
return cdISO_8859_1.decode(in, out, false);
}
}