/*
* Copyright (C) 2014 The Android Open Source Project
* Copyright (c) 2000, 2013, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio.charset;
import java.io.UnsupportedEncodingException;
import libcore.icu.NativeConverter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.spi.CharsetProvider;
import java.security.AccessController;
import java.security.AccessControlException;
import java.security.PrivilegedAction;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.ServiceLoader;
import java.util.ServiceConfigurationError;
import java.util.SortedMap;
import java.util.TreeMap;
import sun.misc.ASCIICaseInsensitiveComparator;
import sun.nio.cs.ThreadLocalCoders;
import sun.security.action.GetPropertyAction;
/**
* A named mapping between sequences of sixteen-bit Unicode code units and sequences of
* bytes. This class defines methods for creating decoders and encoders and
* for retrieving the various names associated with a charset. Instances of
* this class are immutable.
*
*
This class also defines static methods for testing whether a particular
* charset is supported, for locating charset instances by name, and for
* constructing a map that contains every charset for which support is
* available in the current Java virtual machine. Support for new charsets can
* be added via the service-provider interface defined in the {@link
* java.nio.charset.spi.CharsetProvider} class.
*
*
All of the methods defined in this class are safe for use by multiple
* concurrent threads.
*
*
*
*
Charset names
*
*
Charsets are named by strings composed of the following characters:
*
*
*
*
The uppercase letters 'A' through 'Z'
* ('\u0041' through '\u005a'),
*
*
The lowercase letters 'a' through 'z'
* ('\u0061' through '\u007a'),
*
*
The digits '0' through '9'
* ('\u0030' through '\u0039'),
*
*
The dash character '-'
* ('\u002d', HYPHEN-MINUS),
*
*
The plus character '+'
* ('\u002b', PLUS SIGN),
*
*
The period character '.'
* ('\u002e', FULL STOP),
*
*
The colon character ':'
* ('\u003a', COLON), and
*
*
The underscore character '_'
* ('\u005f', LOW LINE).
*
*
*
* A charset name must begin with either a letter or a digit. The empty string
* is not a legal charset name. Charset names are not case-sensitive; that is,
* case is always ignored when comparing charset names. Charset names
* generally follow the conventions documented in RFC 2278: IANA Charset
* Registration Procedures.
*
*
Every charset has a canonical name and may also have one or more
* aliases. The canonical name is returned by the {@link #name() name} method
* of this class. Canonical names are, by convention, usually in upper case.
* The aliases of a charset are returned by the {@link #aliases() aliases}
* method.
*
*
If a charset listed in the IANA Charset
* Registry is supported by an implementation of the Java platform then
* its canonical name must be the name listed in the registry. Many charsets
* are given more than one name in the registry, in which case the registry
* identifies one of the names as MIME-preferred. If a charset has more
* than one registry name then its canonical name must be the MIME-preferred
* name and the other names in the registry must be valid aliases. If a
* supported charset is not listed in the IANA registry then its canonical name
* must begin with one of the strings "X-" or "x-".
*
*
The IANA charset registry does change over time, and so the canonical
* name and the aliases of a particular charset may also change over time. To
* ensure compatibility it is recommended that no alias ever be removed from a
* charset, and that if the canonical name of a charset is changed then its
* previous canonical name be made into an alias.
*
*
*
Seven-bit ASCII, a.k.a. ISO646-US,
* a.k.a. the Basic Latin block of the Unicode character set
*
ISO-8859-1
*
ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
*
UTF-8
*
Eight-bit UCS Transformation Format
*
UTF-16BE
*
Sixteen-bit UCS Transformation Format,
* big-endian byte order
*
UTF-16LE
*
Sixteen-bit UCS Transformation Format,
* little-endian byte order
*
UTF-16
*
Sixteen-bit UCS Transformation Format,
* byte order identified by an optional byte-order mark
*
*
*
The UTF-8 charset is specified by RFC 2279; the
* transformation format upon which it is based is specified in
* Amendment 2 of ISO 10646-1 and is also described in the Unicode
* Standard.
*
*
The UTF-16 charsets are specified by RFC 2781; the
* transformation formats upon which they are based are specified in
* Amendment 1 of ISO 10646-1 and are also described in the Unicode
* Standard.
*
*
The UTF-16 charsets use sixteen-bit quantities and are
* therefore sensitive to byte order. In these encodings the byte order of a
* stream may be indicated by an initial byte-order mark represented by
* the Unicode character '\uFEFF'. Byte-order marks are handled
* as follows:
*
*
*
*
When decoding, the UTF-16BE and UTF-16LE
* charsets interpret the initial byte-order marks as a ZERO-WIDTH
* NON-BREAKING SPACE; when encoding, they do not write
* byte-order marks.
*
*
When decoding, the UTF-16 charset interprets the
* byte-order mark at the beginning of the input stream to indicate the
* byte-order of the stream but defaults to big-endian if there is no
* byte-order mark; when encoding, it uses big-endian byte order and writes
* a big-endian byte-order mark.
*
*
*
* In any case, byte order marks occurring after the first element of an
* input sequence are not omitted since the same code is used to represent
* ZERO-WIDTH NON-BREAKING SPACE.
*
*
Android note: The Android platform default is always UTF-8.
*
*
The {@link StandardCharsets} class defines constants for each of the
* standard charsets.
*
*
Terminology
*
*
The name of this class is taken from the terms used in
* RFC 2278.
* In that document a charset is defined as the combination of
* one or more coded character sets and a character-encoding scheme.
* (This definition is confusing; some other software systems define
* charset as a synonym for coded character set.)
*
*
A coded character set is a mapping between a set of abstract
* characters and a set of integers. US-ASCII, ISO 8859-1,
* JIS X 0201, and Unicode are examples of coded character sets.
*
*
Some standards have defined a character set to be simply a
* set of abstract characters without an associated assigned numbering.
* An alphabet is an example of such a character set. However, the subtle
* distinction between character set and coded character set
* is rarely used in practice; the former has become a short form for the
* latter, including in the Java API specification.
*
*
A character-encoding scheme is a mapping between one or more
* coded character sets and a set of octet (eight-bit byte) sequences.
* UTF-8, UTF-16, ISO 2022, and EUC are examples of
* character-encoding schemes. Encoding schemes are often associated with
* a particular coded character set; UTF-8, for example, is used only to
* encode Unicode. Some schemes, however, are associated with multiple
* coded character sets; EUC, for example, can be used to encode
* characters in a variety of Asian coded character sets.
*
*
When a coded character set is used exclusively with a single
* character-encoding scheme then the corresponding charset is usually
* named for the coded character set; otherwise a charset is usually named
* for the encoding scheme and, possibly, the locale of the coded
* character sets that it supports. Hence US-ASCII is both the
* name of a coded character set and of the charset that encodes it, while
* EUC-JP is the name of the charset that encodes the
* JIS X 0201, JIS X 0208, and JIS X 0212
* coded character sets for the Japanese language.
*
*
The native character encoding of the Java programming language is
* UTF-16. A charset in the Java platform therefore defines a mapping
* between sequences of sixteen-bit UTF-16 code units (that is, sequences
* of chars) and sequences of bytes.
*
*
* @author Mark Reinhold
* @author JSR-51 Expert Group
* @since 1.4
*
* @see CharsetDecoder
* @see CharsetEncoder
* @see java.nio.charset.spi.CharsetProvider
* @see java.lang.Character
*/
public abstract class Charset
implements Comparable
{
/* -- Static methods -- */
private static volatile String bugLevel = null;
static boolean atBugLevel(String bl) { // package-private
String level = bugLevel;
if (level == null) {
if (!sun.misc.VM.isBooted())
return false;
bugLevel = level = AccessController.doPrivileged(
new GetPropertyAction("sun.nio.cs.bugLevel", ""));
}
return level.equals(bl);
}
/**
* Checks that the given string is a legal charset name.
*
* @param s
* A purported charset name
*
* @throws IllegalCharsetNameException
* If the given name is not a legal charset name
*/
private static void checkName(String s) {
int n = s.length();
if (!atBugLevel("1.4")) {
if (n == 0)
throw new IllegalCharsetNameException(s);
}
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
if (c >= 'A' && c <= 'Z') continue;
if (c >= 'a' && c <= 'z') continue;
if (c >= '0' && c <= '9') continue;
if (c == '-' && i != 0) continue;
if (c == '+' && i != 0) continue;
if (c == ':' && i != 0) continue;
if (c == '_' && i != 0) continue;
if (c == '.' && i != 0) continue;
throw new IllegalCharsetNameException(s);
}
}
/* The standard set of charsets */
// Android-removed: We use ICU's list of standard charsets.
// private static CharsetProvider standardProvider = new StandardCharsets();
// Cache of the most-recently-returned charsets,
// along with the names that were used to find them
//
// cache1/2 usage is explained in the lookup method
//
private static volatile Map.Entry cache1 = null; // "Level 1" cache
private static final HashMap cache2 = new HashMap<>(); // "Level 2" cache
private static void cache(String charsetName, Charset cs) {
synchronized(cache2) {
String canonicalName = cs.name();
Charset canonicalCharset = cache2.get(canonicalName);
if (canonicalCharset != null) {
cs = canonicalCharset;
} else {
cache2.put(canonicalName, cs);
for (String alias : cs.aliases()) {
cache2.put(alias, cs);
}
}
cache2.put(charsetName, cs);
}
cache1 = new AbstractMap.SimpleImmutableEntry<>(charsetName, cs);
}
// Creates an iterator that walks over the available providers, ignoring
// those whose lookup or instantiation causes a security exception to be
// thrown. Should be invoked with full privileges.
//
private static Iterator providers() {
return new Iterator() {
ServiceLoader sl =
ServiceLoader.load(CharsetProvider.class);
Iterator i = sl.iterator();
CharsetProvider next = null;
private boolean getNext() {
while (next == null) {
try {
if (!i.hasNext())
return false;
next = i.next();
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore security exceptions
continue;
}
throw sce;
}
}
return true;
}
public boolean hasNext() {
return getNext();
}
public CharsetProvider next() {
if (!getNext())
throw new NoSuchElementException();
CharsetProvider n = next;
next = null;
return n;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
// Thread-local gate to prevent recursive provider lookups
private static ThreadLocal> gate =
new ThreadLocal>();
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction() {
public Charset run() {
for (Iterator i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
// Android-removed: Remove support for the extended charset provider.
//
/* The extended set of charsets */
// private static Object extendedProviderLock = new Object();
// private static boolean extendedProviderProbed = false;
// private static CharsetProvider extendedProvider = null;
//
// private static void probeExtendedProvider() {
// AccessController.doPrivileged(new PrivilegedAction