rt/emul/compact/src/main/java/java/nio/charset/Charset.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Oct 2013 15:02:17 +0200
changeset 1343 802e5d2da9f6
parent 1334 588d5bf7a560
permissions -rw-r--r--
Charset can be compiled now
     1 /*
     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.
     4  *
     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.
    10  *
    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).
    16  *
    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.
    20  *
    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
    23  * questions.
    24  */
    25 
    26 package java.nio.charset;
    27 
    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;
    34 import java.util.Map;
    35 import java.util.Set;
    36 import java.util.SortedMap;
    37 import java.util.TreeMap;
    38 
    39 
    40 /**
    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.
    46  *
    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.
    53  *
    54  * <p> All of the methods defined in this class are safe for use by multiple
    55  * concurrent threads.
    56  *
    57  *
    58  * <a name="names"><a name="charenc">
    59  * <h4>Charset names</h4>
    60  *
    61  * <p> Charsets are named by strings composed of the following characters:
    62  *
    63  * <ul>
    64  *
    65  *   <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
    66  *        (<tt>'&#92;u0041'</tt>&nbsp;through&nbsp;<tt>'&#92;u005a'</tt>),
    67  *
    68  *   <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
    69  *        (<tt>'&#92;u0061'</tt>&nbsp;through&nbsp;<tt>'&#92;u007a'</tt>),
    70  *
    71  *   <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
    72  *        (<tt>'&#92;u0030'</tt>&nbsp;through&nbsp;<tt>'&#92;u0039'</tt>),
    73  *
    74  *   <li> The dash character <tt>'-'</tt>
    75  *        (<tt>'&#92;u002d'</tt>,&nbsp;<small>HYPHEN-MINUS</small>),
    76  *
    77  *   <li> The plus character <tt>'+'</tt>
    78  *        (<tt>'&#92;u002b'</tt>,&nbsp;<small>PLUS SIGN</small>),
    79  *
    80  *   <li> The period character <tt>'.'</tt>
    81  *        (<tt>'&#92;u002e'</tt>,&nbsp;<small>FULL STOP</small>),
    82  *
    83  *   <li> The colon character <tt>':'</tt>
    84  *        (<tt>'&#92;u003a'</tt>,&nbsp;<small>COLON</small>), and
    85  *
    86  *   <li> The underscore character <tt>'_'</tt>
    87  *        (<tt>'&#92;u005f'</tt>,&nbsp;<small>LOW&nbsp;LINE</small>).
    88  *
    89  * </ul>
    90  *
    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&nbsp;2278:&nbsp;IANA Charset
    96  * Registration Procedures</i></a>.
    97  *
    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}
   102  * method.
   103  *
   104  * <a name="hn">
   105  *
   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.
   112  *
   113  * <a name="iana">
   114  *
   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>.
   125  *
   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.
   131  *
   132  *
   133  * <h4>Standard charsets</h4>
   134  *
   135  * <a name="standard">
   136  *
   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.
   141  *
   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&nbsp;&nbsp;</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&nbsp;order</td></tr>
   154  * <tr><td valign=top><tt>UTF-16LE</tt></td>
   155  *     <td>Sixteen-bit UCS Transformation Format,
   156  *         little-endian byte&nbsp;order</td></tr>
   157  * <tr><td valign=top><tt>UTF-16</tt></td>
   158  *     <td>Sixteen-bit UCS Transformation Format,
   159  *         byte&nbsp;order identified by an optional byte-order mark</td></tr>
   160  * </table></blockquote>
   161  *
   162  * <p> The <tt>UTF-8</tt> charset is specified by <a
   163  * href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC&nbsp;2279</i></a>; the
   164  * transformation format upon which it is based is specified in
   165  * Amendment&nbsp;2 of ISO&nbsp;10646-1 and is also described in the <a
   166  * href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
   167  * Standard</i></a>.
   168  *
   169  * <p> The <tt>UTF-16</tt> charsets are specified by <a
   170  * href="http://www.ietf.org/rfc/rfc2781.txt"><i>RFC&nbsp;2781</i></a>; the
   171  * transformation formats upon which they are based are specified in
   172  * Amendment&nbsp;1 of ISO&nbsp;10646-1 and are also described in the <a
   173  * href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
   174  * Standard</i></a>.
   175  *
   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>'&#92;uFEFF'</tt>.  Byte-order marks are handled
   180  * as follows:
   181  *
   182  * <ul>
   183  *
   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>
   188 
   189  *
   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>
   195  *
   196  * </ul>
   197  *
   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>.
   201  *
   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>
   206  *
   207  * <p>The {@link StandardCharsets} class defines constants for each of the
   208  * standard charsets.
   209  *
   210  * <h4>Terminology</h4>
   211  *
   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&nbsp;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>.)
   218  *
   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&nbsp;8859-1,
   221  * JIS&nbsp;X&nbsp;0201, and Unicode are examples of coded character sets.
   222  *
   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.
   229  *
   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&nbsp;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.
   238  *
   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&nbsp;X&nbsp;0201, JIS&nbsp;X&nbsp;0208, and JIS&nbsp;X&nbsp;0212
   247  * coded character sets for the Japanese language.
   248  *
   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>
   253  *
   254  *
   255  * @author Mark Reinhold
   256  * @author JSR-51 Expert Group
   257  * @since 1.4
   258  *
   259  * @see CharsetDecoder
   260  * @see CharsetEncoder
   261  * @see java.nio.charset.spi.CharsetProvider
   262  * @see java.lang.Character
   263  */
   264 
   265 public abstract class Charset
   266     implements Comparable<Charset>
   267 {
   268 
   269     /* -- Static methods -- */
   270 
   271     private static volatile String bugLevel = null;
   272 
   273     /**
   274      * Checks that the given string is a legal charset name. </p>
   275      *
   276      * @param  s
   277      *         A purported charset name
   278      *
   279      * @throws  IllegalCharsetNameException
   280      *          If the given name is not a legal charset name
   281      */
   282     private static void checkName(String s) {
   283         int n = s.length();
   284             if (n == 0)
   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);
   297         }
   298     }
   299 
   300     // Cache of the most-recently-returned charsets,
   301     // along with the names that were used to find them
   302     //
   303     private static volatile Object[] cache1 = null; // "Level 1" cache
   304     private static volatile Object[] cache2 = null; // "Level 2" cache
   305 
   306     private static void cache(String charsetName, Charset cs) {
   307         cache2 = cache1;
   308         cache1 = new Object[] { charsetName, cs };
   309     }
   310 
   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.
   314     //
   315     private static Iterator providers() {
   316         return Collections.emptyIterator();
   317     }
   318 
   319     // Thread-local gate to prevent recursive provider lookups
   320     private static ThreadLocal<ThreadLocal> gate = new ThreadLocal<ThreadLocal>();
   321 
   322     private static Charset lookupViaProviders(final String charsetName) {
   323         return null;
   324     }
   325 
   326     /* The extended set of charsets */
   327     private static Object extendedProviderLock = new Object();
   328     private static boolean extendedProviderProbed = false;
   329 
   330 
   331     private static Charset lookupExtendedCharset(String charsetName) {
   332         return null;
   333     }
   334 
   335     private static Charset lookup(String charsetName) {
   336         if (charsetName == null)
   337             throw new IllegalArgumentException("Null charset name");
   338 
   339         Object[] a;
   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);
   346     }
   347 
   348     private static Charset lookup2(String charsetName) {
   349         Object[] a;
   350         if ((a = cache2) != null && charsetName.equals(a[0])) {
   351             cache2 = cache1;
   352             cache1 = a;
   353             return (Charset)a[1];
   354         }
   355 
   356         /* Only need to check the name if we didn't find a charset for it */
   357         checkName(charsetName);
   358         return null;
   359     }
   360 
   361     /**
   362      * Tells whether the named charset is supported. </p>
   363      *
   364      * @param  charsetName
   365      *         The name of the requested charset; may be either
   366      *         a canonical name or an alias
   367      *
   368      * @return  <tt>true</tt> if, and only if, support for the named charset
   369      *          is available in the current Java virtual machine
   370      *
   371      * @throws IllegalCharsetNameException
   372      *         If the given charset name is illegal
   373      *
   374      * @throws  IllegalArgumentException
   375      *          If the given <tt>charsetName</tt> is null
   376      */
   377     public static boolean isSupported(String charsetName) {
   378         return (lookup(charsetName) != null);
   379     }
   380 
   381     /**
   382      * Returns a charset object for the named charset. </p>
   383      *
   384      * @param  charsetName
   385      *         The name of the requested charset; may be either
   386      *         a canonical name or an alias
   387      *
   388      * @return  A charset object for the named charset
   389      *
   390      * @throws  IllegalCharsetNameException
   391      *          If the given charset name is illegal
   392      *
   393      * @throws  IllegalArgumentException
   394      *          If the given <tt>charsetName</tt> is null
   395      *
   396      * @throws  UnsupportedCharsetException
   397      *          If no support for the named charset is available
   398      *          in this instance of the Java virtual machine
   399      */
   400     public static Charset forName(String charsetName) {
   401         Charset cs = lookup(charsetName);
   402         if (cs != null)
   403             return cs;
   404         throw new UnsupportedCharsetException(charsetName);
   405     }
   406 
   407     // Fold charsets from the given iterator into the given map, ignoring
   408     // charsets whose names already have entries in the map.
   409     //
   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);
   415         }
   416     }
   417 
   418     /**
   419      * Constructs a sorted map from canonical charset names to charset objects.
   420      *
   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>
   426      *
   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
   433      * algorithm.
   434      *
   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>
   440      *
   441      * @return An immutable, case-insensitive map from canonical charset names
   442      *         to charset objects
   443      */
   444     public static SortedMap<String,Charset> availableCharsets() {
   445         TreeMap<String, Charset> tm = new TreeMap<String,Charset>();
   446         tm.put("UTF-8", Charset.defaultCharset());
   447         return tm;
   448     }
   449 
   450     private static volatile Charset defaultCharset;
   451 
   452     /**
   453      * Returns the default charset of this Java virtual machine.
   454      *
   455      * <p> The default charset is determined during virtual-machine startup and
   456      * typically depends upon the locale and charset of the underlying
   457      * operating system.
   458      *
   459      * @return  A charset object for the default charset
   460      *
   461      * @since 1.5
   462      */
   463     public static Charset defaultCharset() {
   464         if (defaultCharset == null) {
   465             defaultCharset = forName("UTF-8");
   466         }
   467         return defaultCharset;
   468     }
   469 
   470 
   471     /* -- Instance fields and methods -- */
   472 
   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;
   476 
   477     /**
   478      * Initializes a new charset with the given canonical name and alias
   479      * set. </p>
   480      *
   481      * @param  canonicalName
   482      *         The canonical name of this charset
   483      *
   484      * @param  aliases
   485      *         An array of this charset's aliases, or null if it has no aliases
   486      *
   487      * @throws IllegalCharsetNameException
   488      *         If the canonical name or any of the aliases are illegal
   489      */
   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++)
   494             checkName(as[i]);
   495         this.name = canonicalName;
   496         this.aliases = as;
   497     }
   498 
   499     /**
   500      * Returns this charset's canonical name. </p>
   501      *
   502      * @return  The canonical name of this charset
   503      */
   504     public final String name() {
   505         return name;
   506     }
   507 
   508     /**
   509      * Returns a set containing this charset's aliases. </p>
   510      *
   511      * @return  An immutable set of this charset's aliases
   512      */
   513     public final Set<String> aliases() {
   514         if (aliasSet != null)
   515             return aliasSet;
   516         int n = aliases.length;
   517         HashSet<String> hs = new HashSet<String>(n);
   518         for (int i = 0; i < n; i++)
   519             hs.add(aliases[i]);
   520         aliasSet = Collections.unmodifiableSet(hs);
   521         return aliasSet;
   522     }
   523 
   524     /**
   525      * Returns this charset's human-readable name for the default locale.
   526      *
   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>
   530      *
   531      * @return  The display name of this charset in the default locale
   532      */
   533     public String displayName() {
   534         return name;
   535     }
   536 
   537     /**
   538      * Tells whether or not this charset is registered in the <a
   539      * href="http://www.iana.org/assignments/character-sets">IANA Charset
   540      * Registry</a>.  </p>
   541      *
   542      * @return  <tt>true</tt> if, and only if, this charset is known by its
   543      *          implementor to be registered with the IANA
   544      */
   545     public final boolean isRegistered() {
   546         return !name.startsWith("X-") && !name.startsWith("x-");
   547     }
   548 
   549     /**
   550      * Returns this charset's human-readable name for the given locale.
   551      *
   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>
   555      *
   556      * @param  locale
   557      *         The locale for which the display name is to be retrieved
   558      *
   559      * @return  The display name of this charset in the given locale
   560      */
   561     public String displayName(Locale locale) {
   562         return name;
   563     }
   564 
   565     /**
   566      * Tells whether or not this charset contains the given charset.
   567      *
   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.
   573      *
   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
   577      * case.
   578      *
   579      * <p> Every charset contains itself.
   580      *
   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
   585      * in this charset.
   586      *
   587      * @return  <tt>true</tt> if the given charset is contained in this charset
   588      */
   589     public abstract boolean contains(Charset cs);
   590 
   591     /**
   592      * Constructs a new decoder for this charset. </p>
   593      *
   594      * @return  A new decoder for this charset
   595      */
   596     public abstract CharsetDecoder newDecoder();
   597 
   598     /**
   599      * Constructs a new encoder for this charset. </p>
   600      *
   601      * @return  A new encoder for this charset
   602      *
   603      * @throws  UnsupportedOperationException
   604      *          If this charset does not support encoding
   605      */
   606     public abstract CharsetEncoder newEncoder();
   607 
   608     /**
   609      * Tells whether or not this charset supports encoding.
   610      *
   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>
   618      *
   619      * @return  <tt>true</tt> if, and only if, this charset supports encoding
   620      */
   621     public boolean canEncode() {
   622         return true;
   623     }
   624 
   625     /**
   626      * Convenience method that decodes bytes in this charset into Unicode
   627      * characters.
   628      *
   629      * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
   630      * same result as the expression
   631      *
   632      * <pre>
   633      *     cs.newDecoder()
   634      *       .onMalformedInput(CodingErrorAction.REPLACE)
   635      *       .onUnmappableCharacter(CodingErrorAction.REPLACE)
   636      *       .decode(bb); </pre>
   637      *
   638      * except that it is potentially more efficient because it can cache
   639      * decoders between successive invocations.
   640      *
   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>
   645      *
   646      * @param  bb  The byte buffer to be decoded
   647      *
   648      * @return  A char buffer containing the decoded characters
   649      */
   650 //    public final CharBuffer decode(ByteBuffer bb) {
   651 //        try {
   652 //            return ThreadLocalCoders.decoderFor(this)
   653 //                .onMalformedInput(CodingErrorAction.REPLACE)
   654 //                .onUnmappableCharacter(CodingErrorAction.REPLACE)
   655 //                .decode(bb);
   656 //        } catch (CharacterCodingException x) {
   657 //            throw new Error(x);         // Can't happen
   658 //        }
   659 //    }
   660 
   661     /**
   662      * Convenience method that encodes Unicode characters into bytes in this
   663      * charset.
   664      *
   665      * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
   666      * same result as the expression
   667      *
   668      * <pre>
   669      *     cs.newEncoder()
   670      *       .onMalformedInput(CodingErrorAction.REPLACE)
   671      *       .onUnmappableCharacter(CodingErrorAction.REPLACE)
   672      *       .encode(bb); </pre>
   673      *
   674      * except that it is potentially more efficient because it can cache
   675      * encoders between successive invocations.
   676      *
   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>
   681      *
   682      * @param  cb  The char buffer to be encoded
   683      *
   684      * @return  A byte buffer containing the encoded characters
   685      */
   686 //    public final ByteBuffer encode(CharBuffer cb) {
   687 //        try {
   688 //            return ThreadLocalCoders.encoderFor(this)
   689 //                .onMalformedInput(CodingErrorAction.REPLACE)
   690 //                .onUnmappableCharacter(CodingErrorAction.REPLACE)
   691 //                .encode(cb);
   692 //        } catch (CharacterCodingException x) {
   693 //            throw new Error(x);         // Can't happen
   694 //        }
   695 //    }
   696 
   697     /**
   698      * Convenience method that encodes a string into bytes in this charset.
   699      *
   700      * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
   701      * same result as the expression
   702      *
   703      * <pre>
   704      *     cs.encode(CharBuffer.wrap(s)); </pre>
   705      *
   706      * @param  str  The string to be encoded
   707      *
   708      * @return  A byte buffer containing the encoded characters
   709      */
   710 //    public final ByteBuffer encode(String str) {
   711 //        return encode(CharBuffer.wrap(str));
   712 //    }
   713 
   714     /**
   715      * Compares this charset to another.
   716      *
   717      * <p> Charsets are ordered by their canonical names, without regard to
   718      * case. </p>
   719      *
   720      * @param  that
   721      *         The charset to which this charset is to be compared
   722      *
   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
   725      */
   726     public final int compareTo(Charset that) {
   727         return (name().compareToIgnoreCase(that.name()));
   728     }
   729 
   730     /**
   731      * Computes a hashcode for this charset. </p>
   732      *
   733      * @return  An integer hashcode
   734      */
   735     public final int hashCode() {
   736         return name().hashCode();
   737     }
   738 
   739     /**
   740      * Tells whether or not this object is equal to another.
   741      *
   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>
   744      *
   745      * @return  <tt>true</tt> if, and only if, this charset is equal to the
   746      *          given object
   747      */
   748     public final boolean equals(Object ob) {
   749         if (!(ob instanceof Charset))
   750             return false;
   751         if (this == ob)
   752             return true;
   753         return name.equals(((Charset)ob).name());
   754     }
   755 
   756     /**
   757      * Returns a string describing this charset. </p>
   758      *
   759      * @return  A string describing this charset
   760      */
   761     public final String toString() {
   762         return name();
   763     }
   764 
   765 }