8184665: Skip name and alias checks for standard Charsets

Reviewed-by: sherman, rriggs, forax
This commit is contained in:
Claes Redestad 2017-07-19 14:40:50 +02:00
parent c67a6c326c
commit 17fad0eecc
6 changed files with 50 additions and 24 deletions

View File

@ -25,6 +25,11 @@
package java.nio.charset;
import jdk.internal.misc.VM;
import sun.nio.cs.StandardCharsets;
import sun.nio.cs.ThreadLocalCoders;
import sun.security.action.GetPropertyAction;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.spi.CharsetProvider;
@ -38,15 +43,11 @@ import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.ServiceLoader;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import jdk.internal.misc.VM;
import sun.nio.cs.StandardCharsets;
import sun.nio.cs.ThreadLocalCoders;
import sun.security.action.GetPropertyAction;
/**
@ -635,10 +636,19 @@ public abstract class Charset
* If the canonical name or any of the aliases are illegal
*/
protected Charset(String canonicalName, String[] aliases) {
checkName(canonicalName);
String[] as = Objects.requireNonNullElse(aliases, zeroAliases);
for (int i = 0; i < as.length; i++)
checkName(as[i]);
// Skip checks for the standard, built-in Charsets we always load
// during initialization. Use of identity is intentional to be
// consistent with sun.nio.cs.StandardCharsets
if (canonicalName != StandardCharsets.ISO_8859_1
&& canonicalName != StandardCharsets.US_ASCII
&& canonicalName != StandardCharsets.UTF_8) {
checkName(canonicalName);
for (int i = 0; i < as.length; i++) {
checkName(as[i]);
}
}
this.name = canonicalName;
this.aliases = as;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2017, 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
@ -42,7 +42,7 @@ class ISO_8859_1
{
public ISO_8859_1() {
super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1);
super(StandardCharsets.ISO_8859_1, StandardCharsets.aliases_ISO_8859_1);
}
public String historicalName() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -51,6 +51,12 @@ public class StandardCharsets extends CharsetProvider {
private static final String packagePrefix = "sun.nio.cs";
public static final String US_ASCII = "US-ASCII";
public static final String ISO_8859_1 = "ISO-8859-1";
public static final String UTF_8 = "UTF-8";
public StandardCharsets() {
this.aliasMap = new Aliases();
this.classMap = new Classes();
@ -103,13 +109,13 @@ public class StandardCharsets extends CharsetProvider {
// As all charset class names added to classMap are string literals we
// can check identity here as an optimization
if (cln == "US_ASCII") {
if (cln == US_ASCII) {
return cache(csn, new US_ASCII());
}
if (cln == "ISO_8859_1") {
if (cln == ISO_8859_1) {
return cache(csn, new ISO_8859_1());
}
if (cln == "UTF_8") {
if (cln == UTF_8) {
return cache(csn, new UTF_8());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2017, 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
@ -38,7 +38,7 @@ public class US_ASCII
{
public US_ASCII() {
super("US-ASCII", StandardCharsets.aliases_US_ASCII);
super(StandardCharsets.US_ASCII, StandardCharsets.aliases_US_ASCII);
}
public String historicalName() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2017, 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
@ -57,7 +57,7 @@ import java.nio.charset.CodingErrorAction;
class UTF_8 extends Unicode
{
public UTF_8() {
super("UTF-8", StandardCharsets.aliases_UTF_8);
super(StandardCharsets.UTF_8, StandardCharsets.aliases_UTF_8);
}
public String historicalName() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2017, 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,15 +22,12 @@
*/
/* @test
* @bug 6330020
* @bug 6330020 8184665
* @summary Ensure Charset.forName/isSupport throws the correct exception
* if the charset names passed in are illegal.
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.util.*;
public class IllegalCharsetName {
public static void main(String[] args) throws Exception {
@ -59,5 +56,18 @@ public class IllegalCharsetName {
} catch (IllegalCharsetNameException x) { //expected
}
}
// Standard charsets may bypass alias checking during startup, test that
// they're all well-behaved as a sanity test
checkAliases(StandardCharsets.ISO_8859_1);
checkAliases(StandardCharsets.US_ASCII);
checkAliases(StandardCharsets.UTF_8);
}
private static void checkAliases(Charset cs) {
for (String alias : cs.aliases()) {
Charset.forName(alias);
Charset.isSupported(alias);
}
}
}