mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets
Reviewed-by: sherman, ulfzibis
This commit is contained in:
parent
08adfe31b3
commit
4546512c19
11 changed files with 31 additions and 30 deletions
|
@ -52,6 +52,9 @@ import static java.lang.Character.highSurrogate;
|
||||||
import static java.lang.Character.lowSurrogate;
|
import static java.lang.Character.lowSurrogate;
|
||||||
import static java.lang.Character.isSupplementaryCodePoint;
|
import static java.lang.Character.isSupplementaryCodePoint;
|
||||||
import static java.lang.StringUTF16.putChar;
|
import static java.lang.StringUTF16.putChar;
|
||||||
|
import static java.nio.charset.StandardCharsets.ISO_8859_1;
|
||||||
|
import static java.nio.charset.StandardCharsets.US_ASCII;
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for string encoding and decoding.
|
* Utility class for string encoding and decoding.
|
||||||
|
@ -67,10 +70,6 @@ class StringCoding {
|
||||||
private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
|
private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
|
||||||
new ThreadLocal<>();
|
new ThreadLocal<>();
|
||||||
|
|
||||||
private static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE;
|
|
||||||
private static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE;
|
|
||||||
private static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE;
|
|
||||||
|
|
||||||
private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
|
private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
|
||||||
SoftReference<T> sr = tl.get();
|
SoftReference<T> sr = tl.get();
|
||||||
if (sr == null)
|
if (sr == null)
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
package java.nio.charset;
|
package java.nio.charset;
|
||||||
|
|
||||||
import jdk.internal.misc.VM;
|
import jdk.internal.misc.VM;
|
||||||
import sun.nio.cs.StandardCharsets;
|
|
||||||
import sun.nio.cs.ThreadLocalCoders;
|
import sun.nio.cs.ThreadLocalCoders;
|
||||||
import sun.security.action.GetPropertyAction;
|
import sun.security.action.GetPropertyAction;
|
||||||
|
|
||||||
|
@ -311,7 +310,8 @@ public abstract class Charset
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The standard set of charsets */
|
/* The standard set of charsets */
|
||||||
private static final CharsetProvider standardProvider = new StandardCharsets();
|
private static final CharsetProvider standardProvider
|
||||||
|
= new sun.nio.cs.StandardCharsets();
|
||||||
|
|
||||||
private static final String[] zeroAliases = new String[0];
|
private static final String[] zeroAliases = new String[0];
|
||||||
|
|
||||||
|
@ -609,7 +609,7 @@ public abstract class Charset
|
||||||
if (cs != null)
|
if (cs != null)
|
||||||
defaultCharset = cs;
|
defaultCharset = cs;
|
||||||
else
|
else
|
||||||
defaultCharset = sun.nio.cs.UTF_8.INSTANCE;
|
defaultCharset = StandardCharsets.UTF_8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return defaultCharset;
|
return defaultCharset;
|
||||||
|
|
|
@ -41,26 +41,26 @@ public final class StandardCharsets {
|
||||||
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
|
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
|
||||||
* Unicode character set
|
* Unicode character set
|
||||||
*/
|
*/
|
||||||
public static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE;
|
public static final Charset US_ASCII = new sun.nio.cs.US_ASCII();
|
||||||
/**
|
/**
|
||||||
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
|
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
|
||||||
*/
|
*/
|
||||||
public static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE;
|
public static final Charset ISO_8859_1 = new sun.nio.cs.ISO_8859_1();
|
||||||
/**
|
/**
|
||||||
* Eight-bit UCS Transformation Format
|
* Eight-bit UCS Transformation Format
|
||||||
*/
|
*/
|
||||||
public static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE;
|
public static final Charset UTF_8 = new sun.nio.cs.UTF_8();
|
||||||
/**
|
/**
|
||||||
* Sixteen-bit UCS Transformation Format, big-endian byte order
|
* Sixteen-bit UCS Transformation Format, big-endian byte order
|
||||||
*/
|
*/
|
||||||
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
|
public static final Charset UTF_16BE = new sun.nio.cs.UTF_16BE();
|
||||||
/**
|
/**
|
||||||
* Sixteen-bit UCS Transformation Format, little-endian byte order
|
* Sixteen-bit UCS Transformation Format, little-endian byte order
|
||||||
*/
|
*/
|
||||||
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
|
public static final Charset UTF_16LE = new sun.nio.cs.UTF_16LE();
|
||||||
/**
|
/**
|
||||||
* Sixteen-bit UCS Transformation Format, byte order identified by an
|
* Sixteen-bit UCS Transformation Format, byte order identified by an
|
||||||
* optional byte-order mark
|
* optional byte-order mark
|
||||||
*/
|
*/
|
||||||
public static final Charset UTF_16 = Charset.forName("UTF-16");
|
public static final Charset UTF_16 = new sun.nio.cs.UTF_16();
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,6 @@ public class ISO_8859_1
|
||||||
extends Charset
|
extends Charset
|
||||||
implements HistoricallyNamedCharset
|
implements HistoricallyNamedCharset
|
||||||
{
|
{
|
||||||
|
|
||||||
public static final ISO_8859_1 INSTANCE = new ISO_8859_1();
|
|
||||||
|
|
||||||
public ISO_8859_1() {
|
public ISO_8859_1() {
|
||||||
super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1());
|
super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1());
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,9 +83,12 @@ public class StandardCharsets extends CharsetProvider {
|
||||||
Map<String,Charset> map = cache;
|
Map<String,Charset> map = cache;
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
map = new Cache();
|
map = new Cache();
|
||||||
map.put("utf-8", UTF_8.INSTANCE);
|
map.put("utf-8", java.nio.charset.StandardCharsets.UTF_8);
|
||||||
map.put("iso-8859-1", ISO_8859_1.INSTANCE);
|
map.put("iso-8859-1", java.nio.charset.StandardCharsets.ISO_8859_1);
|
||||||
map.put("us-ascii", US_ASCII.INSTANCE);
|
map.put("us-ascii", java.nio.charset.StandardCharsets.US_ASCII);
|
||||||
|
map.put("utf-16", java.nio.charset.StandardCharsets.UTF_16);
|
||||||
|
map.put("utf-16be", java.nio.charset.StandardCharsets.UTF_16BE);
|
||||||
|
map.put("utf-16le", java.nio.charset.StandardCharsets.UTF_16LE);
|
||||||
cache = map;
|
cache = map;
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
|
@ -123,11 +126,11 @@ public class StandardCharsets extends CharsetProvider {
|
||||||
// Classes eagerly during bootstrap
|
// Classes eagerly during bootstrap
|
||||||
String csn;
|
String csn;
|
||||||
if (charsetName.equals("UTF-8")) {
|
if (charsetName.equals("UTF-8")) {
|
||||||
return UTF_8.INSTANCE;
|
return java.nio.charset.StandardCharsets.UTF_8;
|
||||||
} else if (charsetName.equals("US-ASCII")) {
|
} else if (charsetName.equals("US-ASCII")) {
|
||||||
return US_ASCII.INSTANCE;
|
return java.nio.charset.StandardCharsets.US_ASCII;
|
||||||
} else if (charsetName.equals("ISO-8859-1")) {
|
} else if (charsetName.equals("ISO-8859-1")) {
|
||||||
return ISO_8859_1.INSTANCE;
|
return java.nio.charset.StandardCharsets.ISO_8859_1;
|
||||||
} else {
|
} else {
|
||||||
csn = canonicalize(toLower(charsetName));
|
csn = canonicalize(toLower(charsetName));
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,6 @@ public class US_ASCII
|
||||||
extends Charset
|
extends Charset
|
||||||
implements HistoricallyNamedCharset
|
implements HistoricallyNamedCharset
|
||||||
{
|
{
|
||||||
public static final US_ASCII INSTANCE = new US_ASCII();
|
|
||||||
|
|
||||||
public US_ASCII() {
|
public US_ASCII() {
|
||||||
super("US-ASCII", StandardCharsets.aliases_US_ASCII());
|
super("US-ASCII", StandardCharsets.aliases_US_ASCII());
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
import java.nio.charset.CharsetDecoder;
|
||||||
import java.nio.charset.CharsetEncoder;
|
import java.nio.charset.CharsetEncoder;
|
||||||
|
|
||||||
class UTF_16 extends Unicode
|
public class UTF_16 extends Unicode
|
||||||
{
|
{
|
||||||
|
|
||||||
public UTF_16() {
|
public UTF_16() {
|
||||||
|
|
|
@ -29,7 +29,7 @@ import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
import java.nio.charset.CharsetDecoder;
|
||||||
import java.nio.charset.CharsetEncoder;
|
import java.nio.charset.CharsetEncoder;
|
||||||
|
|
||||||
class UTF_16BE extends Unicode
|
public class UTF_16BE extends Unicode
|
||||||
{
|
{
|
||||||
|
|
||||||
public UTF_16BE() {
|
public UTF_16BE() {
|
||||||
|
|
|
@ -29,7 +29,7 @@ import java.nio.charset.Charset;
|
||||||
import java.nio.charset.CharsetDecoder;
|
import java.nio.charset.CharsetDecoder;
|
||||||
import java.nio.charset.CharsetEncoder;
|
import java.nio.charset.CharsetEncoder;
|
||||||
|
|
||||||
class UTF_16LE extends Unicode
|
public class UTF_16LE extends Unicode
|
||||||
{
|
{
|
||||||
|
|
||||||
public UTF_16LE() {
|
public UTF_16LE() {
|
||||||
|
|
|
@ -55,9 +55,6 @@ import java.nio.charset.CodingErrorAction;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final class UTF_8 extends Unicode {
|
public final class UTF_8 extends Unicode {
|
||||||
|
|
||||||
public static final UTF_8 INSTANCE = new UTF_8();
|
|
||||||
|
|
||||||
public UTF_8() {
|
public UTF_8() {
|
||||||
super("UTF-8", StandardCharsets.aliases_UTF_8());
|
super("UTF-8", StandardCharsets.aliases_UTF_8());
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,13 @@ public class Standard {
|
||||||
check("UTF-16LE".equals(StandardCharsets.UTF_16LE.name()));
|
check("UTF-16LE".equals(StandardCharsets.UTF_16LE.name()));
|
||||||
check("UTF-16".equals(StandardCharsets.UTF_16.name()));
|
check("UTF-16".equals(StandardCharsets.UTF_16.name()));
|
||||||
|
|
||||||
|
check(Charset.forName("US-ASCII") == StandardCharsets.US_ASCII);
|
||||||
|
check(Charset.forName("ISO-8859-1") == StandardCharsets.ISO_8859_1);
|
||||||
|
check(Charset.forName("UTF-8") == StandardCharsets.UTF_8);
|
||||||
|
check(Charset.forName("UTF-16BE") == StandardCharsets.UTF_16BE);
|
||||||
|
check(Charset.forName("UTF-16LE") == StandardCharsets.UTF_16LE);
|
||||||
|
check(Charset.forName("UTF-16") == StandardCharsets.UTF_16);
|
||||||
|
|
||||||
Set<String> charsets = new HashSet<>();
|
Set<String> charsets = new HashSet<>();
|
||||||
Field standardCharsetFields[] = StandardCharsets.class.getFields();
|
Field standardCharsetFields[] = StandardCharsets.class.getFields();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue