2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package java.nio.charset;
28 //import java.nio.ByteBuffer;
29 //import java.nio.CharBuffer;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.Locale;
36 import java.util.SortedMap;
37 import java.util.TreeMap;
41 * A named mapping between sequences of sixteen-bit Unicode <a
42 * href="../../lang/Character.html#unicode">code units</a> and sequences of
43 * bytes. This class defines methods for creating decoders and encoders and
44 * for retrieving the various names associated with a charset. Instances of
45 * this class are immutable.
47 * <p> This class also defines static methods for testing whether a particular
48 * charset is supported, for locating charset instances by name, and for
49 * constructing a map that contains every charset for which support is
50 * available in the current Java virtual machine. Support for new charsets can
51 * be added via the service-provider interface defined in the {@link
52 * java.nio.charset.spi.CharsetProvider} class.
54 * <p> All of the methods defined in this class are safe for use by multiple
58 * <a name="names"><a name="charenc">
59 * <h4>Charset names</h4>
61 * <p> Charsets are named by strings composed of the following characters:
65 * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
66 * (<tt>'\u0041'</tt> through <tt>'\u005a'</tt>),
68 * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
69 * (<tt>'\u0061'</tt> through <tt>'\u007a'</tt>),
71 * <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
72 * (<tt>'\u0030'</tt> through <tt>'\u0039'</tt>),
74 * <li> The dash character <tt>'-'</tt>
75 * (<tt>'\u002d'</tt>, <small>HYPHEN-MINUS</small>),
77 * <li> The plus character <tt>'+'</tt>
78 * (<tt>'\u002b'</tt>, <small>PLUS SIGN</small>),
80 * <li> The period character <tt>'.'</tt>
81 * (<tt>'\u002e'</tt>, <small>FULL STOP</small>),
83 * <li> The colon character <tt>':'</tt>
84 * (<tt>'\u003a'</tt>, <small>COLON</small>), and
86 * <li> The underscore character <tt>'_'</tt>
87 * (<tt>'\u005f'</tt>, <small>LOW LINE</small>).
91 * A charset name must begin with either a letter or a digit. The empty string
92 * is not a legal charset name. Charset names are not case-sensitive; that is,
93 * case is always ignored when comparing charset names. Charset names
94 * generally follow the conventions documented in <a
95 * href="http://www.ietf.org/rfc/rfc2278.txt"><i>RFC 2278: IANA Charset
96 * Registration Procedures</i></a>.
98 * <p> Every charset has a <i>canonical name</i> and may also have one or more
99 * <i>aliases</i>. The canonical name is returned by the {@link #name() name} method
100 * of this class. Canonical names are, by convention, usually in upper case.
101 * The aliases of a charset are returned by the {@link #aliases() aliases}
106 * <p> Some charsets have an <i>historical name</i> that is defined for
107 * compatibility with previous versions of the Java platform. A charset's
108 * historical name is either its canonical name or one of its aliases. The
109 * historical name is returned by the <tt>getEncoding()</tt> methods of the
110 * {@link java.io.InputStreamReader#getEncoding InputStreamReader} and {@link
111 * java.io.OutputStreamWriter#getEncoding OutputStreamWriter} classes.
115 * <p> If a charset listed in the <a
116 * href="http://www.iana.org/assignments/character-sets"><i>IANA Charset
117 * Registry</i></a> is supported by an implementation of the Java platform then
118 * its canonical name must be the name listed in the registry. Many charsets
119 * are given more than one name in the registry, in which case the registry
120 * identifies one of the names as <i>MIME-preferred</i>. If a charset has more
121 * than one registry name then its canonical name must be the MIME-preferred
122 * name and the other names in the registry must be valid aliases. If a
123 * supported charset is not listed in the IANA registry then its canonical name
124 * must begin with one of the strings <tt>"X-"</tt> or <tt>"x-"</tt>.
126 * <p> The IANA charset registry does change over time, and so the canonical
127 * name and the aliases of a particular charset may also change over time. To
128 * ensure compatibility it is recommended that no alias ever be removed from a
129 * charset, and that if the canonical name of a charset is changed then its
130 * previous canonical name be made into an alias.
133 * <h4>Standard charsets</h4>
135 * <a name="standard">
137 * <p> Every implementation of the Java platform is required to support the
138 * following standard charsets. Consult the release documentation for your
139 * implementation to see if any other charsets are supported. The behavior
140 * of such optional charsets may differ between implementations.
142 * <blockquote><table width="80%" summary="Description of standard charsets">
143 * <tr><th><p align="left">Charset</p></th><th><p align="left">Description</p></th></tr>
144 * <tr><td valign=top><tt>US-ASCII</tt></td>
145 * <td>Seven-bit ASCII, a.k.a. <tt>ISO646-US</tt>,
146 * a.k.a. the Basic Latin block of the Unicode character set</td></tr>
147 * <tr><td valign=top><tt>ISO-8859-1 </tt></td>
148 * <td>ISO Latin Alphabet No. 1, a.k.a. <tt>ISO-LATIN-1</tt></td></tr>
149 * <tr><td valign=top><tt>UTF-8</tt></td>
150 * <td>Eight-bit UCS Transformation Format</td></tr>
151 * <tr><td valign=top><tt>UTF-16BE</tt></td>
152 * <td>Sixteen-bit UCS Transformation Format,
153 * big-endian byte order</td></tr>
154 * <tr><td valign=top><tt>UTF-16LE</tt></td>
155 * <td>Sixteen-bit UCS Transformation Format,
156 * little-endian byte order</td></tr>
157 * <tr><td valign=top><tt>UTF-16</tt></td>
158 * <td>Sixteen-bit UCS Transformation Format,
159 * byte order identified by an optional byte-order mark</td></tr>
160 * </table></blockquote>
162 * <p> The <tt>UTF-8</tt> charset is specified by <a
163 * href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC 2279</i></a>; the
164 * transformation format upon which it is based is specified in
165 * Amendment 2 of ISO 10646-1 and is also described in the <a
166 * href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
169 * <p> The <tt>UTF-16</tt> charsets are specified by <a
170 * href="http://www.ietf.org/rfc/rfc2781.txt"><i>RFC 2781</i></a>; the
171 * transformation formats upon which they are based are specified in
172 * Amendment 1 of ISO 10646-1 and are also described in the <a
173 * href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
176 * <p> The <tt>UTF-16</tt> charsets use sixteen-bit quantities and are
177 * therefore sensitive to byte order. In these encodings the byte order of a
178 * stream may be indicated by an initial <i>byte-order mark</i> represented by
179 * the Unicode character <tt>'\uFEFF'</tt>. Byte-order marks are handled
184 * <li><p> When decoding, the <tt>UTF-16BE</tt> and <tt>UTF-16LE</tt>
185 * charsets interpret the initial byte-order marks as a <small>ZERO-WIDTH
186 * NON-BREAKING SPACE</small>; when encoding, they do not write
187 * byte-order marks. </p></li>
190 * <li><p> When decoding, the <tt>UTF-16</tt> charset interprets the
191 * byte-order mark at the beginning of the input stream to indicate the
192 * byte-order of the stream but defaults to big-endian if there is no
193 * byte-order mark; when encoding, it uses big-endian byte order and writes
194 * a big-endian byte-order mark. </p></li>
198 * In any case, byte order marks occuring after the first element of an
199 * input sequence are not omitted since the same code is used to represent
200 * <small>ZERO-WIDTH NON-BREAKING SPACE</small>.
202 * <p> Every instance of the Java virtual machine has a default charset, which
203 * may or may not be one of the standard charsets. The default charset is
204 * determined during virtual-machine startup and typically depends upon the
205 * locale and charset being used by the underlying operating system. </p>
207 * <p>The {@link StandardCharsets} class defines constants for each of the
210 * <h4>Terminology</h4>
212 * <p> The name of this class is taken from the terms used in
213 * <a href="http://www.ietf.org/rfc/rfc2278.txt"><i>RFC 2278</i></a>.
214 * In that document a <i>charset</i> is defined as the combination of
215 * one or more coded character sets and a character-encoding scheme.
216 * (This definition is confusing; some other software systems define
217 * <i>charset</i> as a synonym for <i>coded character set</i>.)
219 * <p> A <i>coded character set</i> is a mapping between a set of abstract
220 * characters and a set of integers. US-ASCII, ISO 8859-1,
221 * JIS X 0201, and Unicode are examples of coded character sets.
223 * <p> Some standards have defined a <i>character set</i> to be simply a
224 * set of abstract characters without an associated assigned numbering.
225 * An alphabet is an example of such a character set. However, the subtle
226 * distinction between <i>character set</i> and <i>coded character set</i>
227 * is rarely used in practice; the former has become a short form for the
228 * latter, including in the Java API specification.
230 * <p> A <i>character-encoding scheme</i> is a mapping between one or more
231 * coded character sets and a set of octet (eight-bit byte) sequences.
232 * UTF-8, UTF-16, ISO 2022, and EUC are examples of
233 * character-encoding schemes. Encoding schemes are often associated with
234 * a particular coded character set; UTF-8, for example, is used only to
235 * encode Unicode. Some schemes, however, are associated with multiple
236 * coded character sets; EUC, for example, can be used to encode
237 * characters in a variety of Asian coded character sets.
239 * <p> When a coded character set is used exclusively with a single
240 * character-encoding scheme then the corresponding charset is usually
241 * named for the coded character set; otherwise a charset is usually named
242 * for the encoding scheme and, possibly, the locale of the coded
243 * character sets that it supports. Hence <tt>US-ASCII</tt> is both the
244 * name of a coded character set and of the charset that encodes it, while
245 * <tt>EUC-JP</tt> is the name of the charset that encodes the
246 * JIS X 0201, JIS X 0208, and JIS X 0212
247 * coded character sets for the Japanese language.
249 * <p> The native character encoding of the Java programming language is
250 * UTF-16. A charset in the Java platform therefore defines a mapping
251 * between sequences of sixteen-bit UTF-16 code units (that is, sequences
252 * of chars) and sequences of bytes. </p>
255 * @author Mark Reinhold
256 * @author JSR-51 Expert Group
259 * @see CharsetDecoder
260 * @see CharsetEncoder
261 * @see java.nio.charset.spi.CharsetProvider
262 * @see java.lang.Character
265 public abstract class Charset
266 implements Comparable<Charset>
269 /* -- Static methods -- */
271 private static volatile String bugLevel = null;
274 * Checks that the given string is a legal charset name. </p>
277 * A purported charset name
279 * @throws IllegalCharsetNameException
280 * If the given name is not a legal charset name
282 private static void checkName(String s) {
285 throw new IllegalCharsetNameException(s);
286 for (int i = 0; i < n; i++) {
287 char c = s.charAt(i);
288 if (c >= 'A' && c <= 'Z') continue;
289 if (c >= 'a' && c <= 'z') continue;
290 if (c >= '0' && c <= '9') continue;
291 if (c == '-' && i != 0) continue;
292 if (c == '+' && i != 0) continue;
293 if (c == ':' && i != 0) continue;
294 if (c == '_' && i != 0) continue;
295 if (c == '.' && i != 0) continue;
296 throw new IllegalCharsetNameException(s);
300 // Cache of the most-recently-returned charsets,
301 // along with the names that were used to find them
303 private static volatile Object[] cache1 = null; // "Level 1" cache
304 private static volatile Object[] cache2 = null; // "Level 2" cache
306 private static void cache(String charsetName, Charset cs) {
308 cache1 = new Object[] { charsetName, cs };
311 // Creates an iterator that walks over the available providers, ignoring
312 // those whose lookup or instantiation causes a security exception to be
313 // thrown. Should be invoked with full privileges.
315 private static Iterator providers() {
316 return Collections.emptyIterator();
319 // Thread-local gate to prevent recursive provider lookups
320 private static ThreadLocal<ThreadLocal> gate = new ThreadLocal<ThreadLocal>();
322 private static Charset lookupViaProviders(final String charsetName) {
326 /* The extended set of charsets */
327 private static Object extendedProviderLock = new Object();
328 private static boolean extendedProviderProbed = false;
331 private static Charset lookupExtendedCharset(String charsetName) {
335 private static Charset lookup(String charsetName) {
336 if (charsetName == null)
337 throw new IllegalArgumentException("Null charset name");
340 if ((a = cache1) != null && charsetName.equals(a[0]))
341 return (Charset)a[1];
342 // We expect most programs to use one Charset repeatedly.
343 // We convey a hint to this effect to the VM by putting the
344 // level 1 cache miss code in a separate method.
345 return lookup2(charsetName);
348 private static Charset lookup2(String charsetName) {
350 if ((a = cache2) != null && charsetName.equals(a[0])) {
353 return (Charset)a[1];
356 /* Only need to check the name if we didn't find a charset for it */
357 checkName(charsetName);
362 * Tells whether the named charset is supported. </p>
365 * The name of the requested charset; may be either
366 * a canonical name or an alias
368 * @return <tt>true</tt> if, and only if, support for the named charset
369 * is available in the current Java virtual machine
371 * @throws IllegalCharsetNameException
372 * If the given charset name is illegal
374 * @throws IllegalArgumentException
375 * If the given <tt>charsetName</tt> is null
377 public static boolean isSupported(String charsetName) {
378 return (lookup(charsetName) != null);
382 * Returns a charset object for the named charset. </p>
385 * The name of the requested charset; may be either
386 * a canonical name or an alias
388 * @return A charset object for the named charset
390 * @throws IllegalCharsetNameException
391 * If the given charset name is illegal
393 * @throws IllegalArgumentException
394 * If the given <tt>charsetName</tt> is null
396 * @throws UnsupportedCharsetException
397 * If no support for the named charset is available
398 * in this instance of the Java virtual machine
400 public static Charset forName(String charsetName) {
401 Charset cs = lookup(charsetName);
404 throw new UnsupportedCharsetException(charsetName);
407 // Fold charsets from the given iterator into the given map, ignoring
408 // charsets whose names already have entries in the map.
410 private static void put(Iterator<Charset> i, Map<String,Charset> m) {
411 while (i.hasNext()) {
412 Charset cs = i.next();
413 if (!m.containsKey(cs.name()))
414 m.put(cs.name(), cs);
419 * Constructs a sorted map from canonical charset names to charset objects.
421 * <p> The map returned by this method will have one entry for each charset
422 * for which support is available in the current Java virtual machine. If
423 * two or more supported charsets have the same canonical name then the
424 * resulting map will contain just one of them; which one it will contain
425 * is not specified. </p>
427 * <p> The invocation of this method, and the subsequent use of the
428 * resulting map, may cause time-consuming disk or network I/O operations
429 * to occur. This method is provided for applications that need to
430 * enumerate all of the available charsets, for example to allow user
431 * charset selection. This method is not used by the {@link #forName
432 * forName} method, which instead employs an efficient incremental lookup
435 * <p> This method may return different results at different times if new
436 * charset providers are dynamically made available to the current Java
437 * virtual machine. In the absence of such changes, the charsets returned
438 * by this method are exactly those that can be retrieved via the {@link
439 * #forName forName} method. </p>
441 * @return An immutable, case-insensitive map from canonical charset names
444 public static SortedMap<String,Charset> availableCharsets() {
445 TreeMap<String, Charset> tm = new TreeMap<String,Charset>();
446 tm.put("UTF-8", Charset.defaultCharset());
450 private static volatile Charset defaultCharset;
453 * Returns the default charset of this Java virtual machine.
455 * <p> The default charset is determined during virtual-machine startup and
456 * typically depends upon the locale and charset of the underlying
459 * @return A charset object for the default charset
463 public static Charset defaultCharset() {
464 if (defaultCharset == null) {
465 defaultCharset = forName("UTF-8");
467 return defaultCharset;
471 /* -- Instance fields and methods -- */
473 private final String name; // tickles a bug in oldjavac
474 private final String[] aliases; // tickles a bug in oldjavac
475 private Set<String> aliasSet = null;
478 * Initializes a new charset with the given canonical name and alias
481 * @param canonicalName
482 * The canonical name of this charset
485 * An array of this charset's aliases, or null if it has no aliases
487 * @throws IllegalCharsetNameException
488 * If the canonical name or any of the aliases are illegal
490 protected Charset(String canonicalName, String[] aliases) {
491 checkName(canonicalName);
492 String[] as = (aliases == null) ? new String[0] : aliases;
493 for (int i = 0; i < as.length; i++)
495 this.name = canonicalName;
500 * Returns this charset's canonical name. </p>
502 * @return The canonical name of this charset
504 public final String name() {
509 * Returns a set containing this charset's aliases. </p>
511 * @return An immutable set of this charset's aliases
513 public final Set<String> aliases() {
514 if (aliasSet != null)
516 int n = aliases.length;
517 HashSet<String> hs = new HashSet<String>(n);
518 for (int i = 0; i < n; i++)
520 aliasSet = Collections.unmodifiableSet(hs);
525 * Returns this charset's human-readable name for the default locale.
527 * <p> The default implementation of this method simply returns this
528 * charset's canonical name. Concrete subclasses of this class may
529 * override this method in order to provide a localized display name. </p>
531 * @return The display name of this charset in the default locale
533 public String displayName() {
538 * Tells whether or not this charset is registered in the <a
539 * href="http://www.iana.org/assignments/character-sets">IANA Charset
542 * @return <tt>true</tt> if, and only if, this charset is known by its
543 * implementor to be registered with the IANA
545 public final boolean isRegistered() {
546 return !name.startsWith("X-") && !name.startsWith("x-");
550 * Returns this charset's human-readable name for the given locale.
552 * <p> The default implementation of this method simply returns this
553 * charset's canonical name. Concrete subclasses of this class may
554 * override this method in order to provide a localized display name. </p>
557 * The locale for which the display name is to be retrieved
559 * @return The display name of this charset in the given locale
561 public String displayName(Locale locale) {
566 * Tells whether or not this charset contains the given charset.
568 * <p> A charset <i>C</i> is said to <i>contain</i> a charset <i>D</i> if,
569 * and only if, every character representable in <i>D</i> is also
570 * representable in <i>C</i>. If this relationship holds then it is
571 * guaranteed that every string that can be encoded in <i>D</i> can also be
572 * encoded in <i>C</i> without performing any replacements.
574 * <p> That <i>C</i> contains <i>D</i> does not imply that each character
575 * representable in <i>C</i> by a particular byte sequence is represented
576 * in <i>D</i> by the same byte sequence, although sometimes this is the
579 * <p> Every charset contains itself.
581 * <p> This method computes an approximation of the containment relation:
582 * If it returns <tt>true</tt> then the given charset is known to be
583 * contained by this charset; if it returns <tt>false</tt>, however, then
584 * it is not necessarily the case that the given charset is not contained
587 * @return <tt>true</tt> if the given charset is contained in this charset
589 public abstract boolean contains(Charset cs);
592 * Constructs a new decoder for this charset. </p>
594 * @return A new decoder for this charset
596 public abstract CharsetDecoder newDecoder();
599 * Constructs a new encoder for this charset. </p>
601 * @return A new encoder for this charset
603 * @throws UnsupportedOperationException
604 * If this charset does not support encoding
606 public abstract CharsetEncoder newEncoder();
609 * Tells whether or not this charset supports encoding.
611 * <p> Nearly all charsets support encoding. The primary exceptions are
612 * special-purpose <i>auto-detect</i> charsets whose decoders can determine
613 * which of several possible encoding schemes is in use by examining the
614 * input byte sequence. Such charsets do not support encoding because
615 * there is no way to determine which encoding should be used on output.
616 * Implementations of such charsets should override this method to return
617 * <tt>false</tt>. </p>
619 * @return <tt>true</tt> if, and only if, this charset supports encoding
621 public boolean canEncode() {
626 * Convenience method that decodes bytes in this charset into Unicode
629 * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
630 * same result as the expression
634 * .onMalformedInput(CodingErrorAction.REPLACE)
635 * .onUnmappableCharacter(CodingErrorAction.REPLACE)
636 * .decode(bb); </pre>
638 * except that it is potentially more efficient because it can cache
639 * decoders between successive invocations.
641 * <p> This method always replaces malformed-input and unmappable-character
642 * sequences with this charset's default replacement byte array. In order
643 * to detect such sequences, use the {@link
644 * CharsetDecoder#decode(java.nio.ByteBuffer)} method directly. </p>
646 * @param bb The byte buffer to be decoded
648 * @return A char buffer containing the decoded characters
650 // public final CharBuffer decode(ByteBuffer bb) {
652 // return ThreadLocalCoders.decoderFor(this)
653 // .onMalformedInput(CodingErrorAction.REPLACE)
654 // .onUnmappableCharacter(CodingErrorAction.REPLACE)
656 // } catch (CharacterCodingException x) {
657 // throw new Error(x); // Can't happen
662 * Convenience method that encodes Unicode characters into bytes in this
665 * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
666 * same result as the expression
670 * .onMalformedInput(CodingErrorAction.REPLACE)
671 * .onUnmappableCharacter(CodingErrorAction.REPLACE)
672 * .encode(bb); </pre>
674 * except that it is potentially more efficient because it can cache
675 * encoders between successive invocations.
677 * <p> This method always replaces malformed-input and unmappable-character
678 * sequences with this charset's default replacement string. In order to
679 * detect such sequences, use the {@link
680 * CharsetEncoder#encode(java.nio.CharBuffer)} method directly. </p>
682 * @param cb The char buffer to be encoded
684 * @return A byte buffer containing the encoded characters
686 // public final ByteBuffer encode(CharBuffer cb) {
688 // return ThreadLocalCoders.encoderFor(this)
689 // .onMalformedInput(CodingErrorAction.REPLACE)
690 // .onUnmappableCharacter(CodingErrorAction.REPLACE)
692 // } catch (CharacterCodingException x) {
693 // throw new Error(x); // Can't happen
698 * Convenience method that encodes a string into bytes in this charset.
700 * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
701 * same result as the expression
704 * cs.encode(CharBuffer.wrap(s)); </pre>
706 * @param str The string to be encoded
708 * @return A byte buffer containing the encoded characters
710 // public final ByteBuffer encode(String str) {
711 // return encode(CharBuffer.wrap(str));
715 * Compares this charset to another.
717 * <p> Charsets are ordered by their canonical names, without regard to
721 * The charset to which this charset is to be compared
723 * @return A negative integer, zero, or a positive integer as this charset
724 * is less than, equal to, or greater than the specified charset
726 public final int compareTo(Charset that) {
727 return (name().compareToIgnoreCase(that.name()));
731 * Computes a hashcode for this charset. </p>
733 * @return An integer hashcode
735 public final int hashCode() {
736 return name().hashCode();
740 * Tells whether or not this object is equal to another.
742 * <p> Two charsets are equal if, and only if, they have the same canonical
743 * names. A charset is never equal to any other type of object. </p>
745 * @return <tt>true</tt> if, and only if, this charset is equal to the
748 public final boolean equals(Object ob) {
749 if (!(ob instanceof Charset))
753 return name.equals(((Charset)ob).name());
757 * Returns a string describing this charset. </p>
759 * @return A string describing this charset
761 public final String toString() {