rt/emul/compact/src/main/java/java/nio/charset/Charset.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1343 802e5d2da9f6
permissions -rw-r--r--
Set of JDK classes needed to run javac
jtulach@1334
     1
/*
jtulach@1334
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
jtulach@1334
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     4
 *
jtulach@1334
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
    10
 *
jtulach@1334
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    15
 * accompanied this code).
jtulach@1334
    16
 *
jtulach@1334
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    20
 *
jtulach@1334
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    23
 * questions.
jtulach@1334
    24
 */
jtulach@1334
    25
jtulach@1334
    26
package java.nio.charset;
jtulach@1334
    27
jtulach@1334
    28
import java.nio.ByteBuffer;
jtulach@1334
    29
import java.nio.CharBuffer;
jtulach@1334
    30
import java.nio.charset.spi.CharsetProvider;
jtulach@1334
    31
import java.security.AccessController;
jtulach@1334
    32
import java.security.AccessControlException;
jtulach@1334
    33
import java.security.PrivilegedAction;
jtulach@1334
    34
import java.util.Collections;
jtulach@1334
    35
import java.util.HashSet;
jtulach@1334
    36
import java.util.Iterator;
jtulach@1334
    37
import java.util.Locale;
jtulach@1334
    38
import java.util.Map;
jtulach@1334
    39
import java.util.NoSuchElementException;
jtulach@1334
    40
import java.util.Set;
jtulach@1334
    41
import java.util.ServiceLoader;
jtulach@1334
    42
import java.util.ServiceConfigurationError;
jtulach@1334
    43
import java.util.SortedMap;
jtulach@1334
    44
import java.util.TreeMap;
jtulach@1334
    45
import sun.misc.ASCIICaseInsensitiveComparator;
jtulach@1334
    46
import sun.nio.cs.StandardCharsets;
jtulach@1334
    47
import sun.nio.cs.ThreadLocalCoders;
jtulach@1334
    48
import sun.security.action.GetPropertyAction;
jtulach@1334
    49
jtulach@1334
    50
jtulach@1334
    51
/**
jtulach@1334
    52
 * A named mapping between sequences of sixteen-bit Unicode <a
jtulach@1334
    53
 * href="../../lang/Character.html#unicode">code units</a> and sequences of
jtulach@1334
    54
 * bytes.  This class defines methods for creating decoders and encoders and
jtulach@1334
    55
 * for retrieving the various names associated with a charset.  Instances of
jtulach@1334
    56
 * this class are immutable.
jtulach@1334
    57
 *
jtulach@1334
    58
 * <p> This class also defines static methods for testing whether a particular
jtulach@1334
    59
 * charset is supported, for locating charset instances by name, and for
jtulach@1334
    60
 * constructing a map that contains every charset for which support is
jtulach@1334
    61
 * available in the current Java virtual machine.  Support for new charsets can
jtulach@1334
    62
 * be added via the service-provider interface defined in the {@link
jtulach@1334
    63
 * java.nio.charset.spi.CharsetProvider} class.
jtulach@1334
    64
 *
jtulach@1334
    65
 * <p> All of the methods defined in this class are safe for use by multiple
jtulach@1334
    66
 * concurrent threads.
jtulach@1334
    67
 *
jtulach@1334
    68
 *
jtulach@1334
    69
 * <a name="names"><a name="charenc">
jtulach@1334
    70
 * <h4>Charset names</h4>
jtulach@1334
    71
 *
jtulach@1334
    72
 * <p> Charsets are named by strings composed of the following characters:
jtulach@1334
    73
 *
jtulach@1334
    74
 * <ul>
jtulach@1334
    75
 *
jtulach@1334
    76
 *   <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
jtulach@1334
    77
 *        (<tt>'&#92;u0041'</tt>&nbsp;through&nbsp;<tt>'&#92;u005a'</tt>),
jtulach@1334
    78
 *
jtulach@1334
    79
 *   <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
jtulach@1334
    80
 *        (<tt>'&#92;u0061'</tt>&nbsp;through&nbsp;<tt>'&#92;u007a'</tt>),
jtulach@1334
    81
 *
jtulach@1334
    82
 *   <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
jtulach@1334
    83
 *        (<tt>'&#92;u0030'</tt>&nbsp;through&nbsp;<tt>'&#92;u0039'</tt>),
jtulach@1334
    84
 *
jtulach@1334
    85
 *   <li> The dash character <tt>'-'</tt>
jtulach@1334
    86
 *        (<tt>'&#92;u002d'</tt>,&nbsp;<small>HYPHEN-MINUS</small>),
jtulach@1334
    87
 *
jtulach@1334
    88
 *   <li> The plus character <tt>'+'</tt>
jtulach@1334
    89
 *        (<tt>'&#92;u002b'</tt>,&nbsp;<small>PLUS SIGN</small>),
jtulach@1334
    90
 *
jtulach@1334
    91
 *   <li> The period character <tt>'.'</tt>
jtulach@1334
    92
 *        (<tt>'&#92;u002e'</tt>,&nbsp;<small>FULL STOP</small>),
jtulach@1334
    93
 *
jtulach@1334
    94
 *   <li> The colon character <tt>':'</tt>
jtulach@1334
    95
 *        (<tt>'&#92;u003a'</tt>,&nbsp;<small>COLON</small>), and
jtulach@1334
    96
 *
jtulach@1334
    97
 *   <li> The underscore character <tt>'_'</tt>
jtulach@1334
    98
 *        (<tt>'&#92;u005f'</tt>,&nbsp;<small>LOW&nbsp;LINE</small>).
jtulach@1334
    99
 *
jtulach@1334
   100
 * </ul>
jtulach@1334
   101
 *
jtulach@1334
   102
 * A charset name must begin with either a letter or a digit.  The empty string
jtulach@1334
   103
 * is not a legal charset name.  Charset names are not case-sensitive; that is,
jtulach@1334
   104
 * case is always ignored when comparing charset names.  Charset names
jtulach@1334
   105
 * generally follow the conventions documented in <a
jtulach@1334
   106
 * href="http://www.ietf.org/rfc/rfc2278.txt"><i>RFC&nbsp;2278:&nbsp;IANA Charset
jtulach@1334
   107
 * Registration Procedures</i></a>.
jtulach@1334
   108
 *
jtulach@1334
   109
 * <p> Every charset has a <i>canonical name</i> and may also have one or more
jtulach@1334
   110
 * <i>aliases</i>.  The canonical name is returned by the {@link #name() name} method
jtulach@1334
   111
 * of this class.  Canonical names are, by convention, usually in upper case.
jtulach@1334
   112
 * The aliases of a charset are returned by the {@link #aliases() aliases}
jtulach@1334
   113
 * method.
jtulach@1334
   114
 *
jtulach@1334
   115
 * <a name="hn">
jtulach@1334
   116
 *
jtulach@1334
   117
 * <p> Some charsets have an <i>historical name</i> that is defined for
jtulach@1334
   118
 * compatibility with previous versions of the Java platform.  A charset's
jtulach@1334
   119
 * historical name is either its canonical name or one of its aliases.  The
jtulach@1334
   120
 * historical name is returned by the <tt>getEncoding()</tt> methods of the
jtulach@1334
   121
 * {@link java.io.InputStreamReader#getEncoding InputStreamReader} and {@link
jtulach@1334
   122
 * java.io.OutputStreamWriter#getEncoding OutputStreamWriter} classes.
jtulach@1334
   123
 *
jtulach@1334
   124
 * <a name="iana">
jtulach@1334
   125
 *
jtulach@1334
   126
 * <p> If a charset listed in the <a
jtulach@1334
   127
 * href="http://www.iana.org/assignments/character-sets"><i>IANA Charset
jtulach@1334
   128
 * Registry</i></a> is supported by an implementation of the Java platform then
jtulach@1334
   129
 * its canonical name must be the name listed in the registry.  Many charsets
jtulach@1334
   130
 * are given more than one name in the registry, in which case the registry
jtulach@1334
   131
 * identifies one of the names as <i>MIME-preferred</i>.  If a charset has more
jtulach@1334
   132
 * than one registry name then its canonical name must be the MIME-preferred
jtulach@1334
   133
 * name and the other names in the registry must be valid aliases.  If a
jtulach@1334
   134
 * supported charset is not listed in the IANA registry then its canonical name
jtulach@1334
   135
 * must begin with one of the strings <tt>"X-"</tt> or <tt>"x-"</tt>.
jtulach@1334
   136
 *
jtulach@1334
   137
 * <p> The IANA charset registry does change over time, and so the canonical
jtulach@1334
   138
 * name and the aliases of a particular charset may also change over time.  To
jtulach@1334
   139
 * ensure compatibility it is recommended that no alias ever be removed from a
jtulach@1334
   140
 * charset, and that if the canonical name of a charset is changed then its
jtulach@1334
   141
 * previous canonical name be made into an alias.
jtulach@1334
   142
 *
jtulach@1334
   143
 *
jtulach@1334
   144
 * <h4>Standard charsets</h4>
jtulach@1334
   145
 *
jtulach@1334
   146
 * <a name="standard">
jtulach@1334
   147
 *
jtulach@1334
   148
 * <p> Every implementation of the Java platform is required to support the
jtulach@1334
   149
 * following standard charsets.  Consult the release documentation for your
jtulach@1334
   150
 * implementation to see if any other charsets are supported.  The behavior
jtulach@1334
   151
 * of such optional charsets may differ between implementations.
jtulach@1334
   152
 *
jtulach@1334
   153
 * <blockquote><table width="80%" summary="Description of standard charsets">
jtulach@1334
   154
 * <tr><th><p align="left">Charset</p></th><th><p align="left">Description</p></th></tr>
jtulach@1334
   155
 * <tr><td valign=top><tt>US-ASCII</tt></td>
jtulach@1334
   156
 *     <td>Seven-bit ASCII, a.k.a. <tt>ISO646-US</tt>,
jtulach@1334
   157
 *         a.k.a. the Basic Latin block of the Unicode character set</td></tr>
jtulach@1334
   158
 * <tr><td valign=top><tt>ISO-8859-1&nbsp;&nbsp;</tt></td>
jtulach@1334
   159
 *     <td>ISO Latin Alphabet No. 1, a.k.a. <tt>ISO-LATIN-1</tt></td></tr>
jtulach@1334
   160
 * <tr><td valign=top><tt>UTF-8</tt></td>
jtulach@1334
   161
 *     <td>Eight-bit UCS Transformation Format</td></tr>
jtulach@1334
   162
 * <tr><td valign=top><tt>UTF-16BE</tt></td>
jtulach@1334
   163
 *     <td>Sixteen-bit UCS Transformation Format,
jtulach@1334
   164
 *         big-endian byte&nbsp;order</td></tr>
jtulach@1334
   165
 * <tr><td valign=top><tt>UTF-16LE</tt></td>
jtulach@1334
   166
 *     <td>Sixteen-bit UCS Transformation Format,
jtulach@1334
   167
 *         little-endian byte&nbsp;order</td></tr>
jtulach@1334
   168
 * <tr><td valign=top><tt>UTF-16</tt></td>
jtulach@1334
   169
 *     <td>Sixteen-bit UCS Transformation Format,
jtulach@1334
   170
 *         byte&nbsp;order identified by an optional byte-order mark</td></tr>
jtulach@1334
   171
 * </table></blockquote>
jtulach@1334
   172
 *
jtulach@1334
   173
 * <p> The <tt>UTF-8</tt> charset is specified by <a
jtulach@1334
   174
 * href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC&nbsp;2279</i></a>; the
jtulach@1334
   175
 * transformation format upon which it is based is specified in
jtulach@1334
   176
 * Amendment&nbsp;2 of ISO&nbsp;10646-1 and is also described in the <a
jtulach@1334
   177
 * href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
jtulach@1334
   178
 * Standard</i></a>.
jtulach@1334
   179
 *
jtulach@1334
   180
 * <p> The <tt>UTF-16</tt> charsets are specified by <a
jtulach@1334
   181
 * href="http://www.ietf.org/rfc/rfc2781.txt"><i>RFC&nbsp;2781</i></a>; the
jtulach@1334
   182
 * transformation formats upon which they are based are specified in
jtulach@1334
   183
 * Amendment&nbsp;1 of ISO&nbsp;10646-1 and are also described in the <a
jtulach@1334
   184
 * href="http://www.unicode.org/unicode/standard/standard.html"><i>Unicode
jtulach@1334
   185
 * Standard</i></a>.
jtulach@1334
   186
 *
jtulach@1334
   187
 * <p> The <tt>UTF-16</tt> charsets use sixteen-bit quantities and are
jtulach@1334
   188
 * therefore sensitive to byte order.  In these encodings the byte order of a
jtulach@1334
   189
 * stream may be indicated by an initial <i>byte-order mark</i> represented by
jtulach@1334
   190
 * the Unicode character <tt>'&#92;uFEFF'</tt>.  Byte-order marks are handled
jtulach@1334
   191
 * as follows:
jtulach@1334
   192
 *
jtulach@1334
   193
 * <ul>
jtulach@1334
   194
 *
jtulach@1334
   195
 *   <li><p> When decoding, the <tt>UTF-16BE</tt> and <tt>UTF-16LE</tt>
jtulach@1334
   196
 *   charsets interpret the initial byte-order marks as a <small>ZERO-WIDTH
jtulach@1334
   197
 *   NON-BREAKING SPACE</small>; when encoding, they do not write
jtulach@1334
   198
 *   byte-order marks. </p></li>
jtulach@1334
   199
jtulach@1334
   200
 *
jtulach@1334
   201
 *   <li><p> When decoding, the <tt>UTF-16</tt> charset interprets the
jtulach@1334
   202
 *   byte-order mark at the beginning of the input stream to indicate the
jtulach@1334
   203
 *   byte-order of the stream but defaults to big-endian if there is no
jtulach@1334
   204
 *   byte-order mark; when encoding, it uses big-endian byte order and writes
jtulach@1334
   205
 *   a big-endian byte-order mark. </p></li>
jtulach@1334
   206
 *
jtulach@1334
   207
 * </ul>
jtulach@1334
   208
 *
jtulach@1334
   209
 * In any case, byte order marks occuring after the first element of an
jtulach@1334
   210
 * input sequence are not omitted since the same code is used to represent
jtulach@1334
   211
 * <small>ZERO-WIDTH NON-BREAKING SPACE</small>.
jtulach@1334
   212
 *
jtulach@1334
   213
 * <p> Every instance of the Java virtual machine has a default charset, which
jtulach@1334
   214
 * may or may not be one of the standard charsets.  The default charset is
jtulach@1334
   215
 * determined during virtual-machine startup and typically depends upon the
jtulach@1334
   216
 * locale and charset being used by the underlying operating system. </p>
jtulach@1334
   217
 *
jtulach@1334
   218
 * <p>The {@link StandardCharsets} class defines constants for each of the
jtulach@1334
   219
 * standard charsets.
jtulach@1334
   220
 *
jtulach@1334
   221
 * <h4>Terminology</h4>
jtulach@1334
   222
 *
jtulach@1334
   223
 * <p> The name of this class is taken from the terms used in
jtulach@1334
   224
 * <a href="http://www.ietf.org/rfc/rfc2278.txt"><i>RFC&nbsp;2278</i></a>.
jtulach@1334
   225
 * In that document a <i>charset</i> is defined as the combination of
jtulach@1334
   226
 * one or more coded character sets and a character-encoding scheme.
jtulach@1334
   227
 * (This definition is confusing; some other software systems define
jtulach@1334
   228
 * <i>charset</i> as a synonym for <i>coded character set</i>.)
jtulach@1334
   229
 *
jtulach@1334
   230
 * <p> A <i>coded character set</i> is a mapping between a set of abstract
jtulach@1334
   231
 * characters and a set of integers.  US-ASCII, ISO&nbsp;8859-1,
jtulach@1334
   232
 * JIS&nbsp;X&nbsp;0201, and Unicode are examples of coded character sets.
jtulach@1334
   233
 *
jtulach@1334
   234
 * <p> Some standards have defined a <i>character set</i> to be simply a
jtulach@1334
   235
 * set of abstract characters without an associated assigned numbering.
jtulach@1334
   236
 * An alphabet is an example of such a character set.  However, the subtle
jtulach@1334
   237
 * distinction between <i>character set</i> and <i>coded character set</i>
jtulach@1334
   238
 * is rarely used in practice; the former has become a short form for the
jtulach@1334
   239
 * latter, including in the Java API specification.
jtulach@1334
   240
 *
jtulach@1334
   241
 * <p> A <i>character-encoding scheme</i> is a mapping between one or more
jtulach@1334
   242
 * coded character sets and a set of octet (eight-bit byte) sequences.
jtulach@1334
   243
 * UTF-8, UTF-16, ISO&nbsp;2022, and EUC are examples of
jtulach@1334
   244
 * character-encoding schemes.  Encoding schemes are often associated with
jtulach@1334
   245
 * a particular coded character set; UTF-8, for example, is used only to
jtulach@1334
   246
 * encode Unicode.  Some schemes, however, are associated with multiple
jtulach@1334
   247
 * coded character sets; EUC, for example, can be used to encode
jtulach@1334
   248
 * characters in a variety of Asian coded character sets.
jtulach@1334
   249
 *
jtulach@1334
   250
 * <p> When a coded character set is used exclusively with a single
jtulach@1334
   251
 * character-encoding scheme then the corresponding charset is usually
jtulach@1334
   252
 * named for the coded character set; otherwise a charset is usually named
jtulach@1334
   253
 * for the encoding scheme and, possibly, the locale of the coded
jtulach@1334
   254
 * character sets that it supports.  Hence <tt>US-ASCII</tt> is both the
jtulach@1334
   255
 * name of a coded character set and of the charset that encodes it, while
jtulach@1334
   256
 * <tt>EUC-JP</tt> is the name of the charset that encodes the
jtulach@1334
   257
 * JIS&nbsp;X&nbsp;0201, JIS&nbsp;X&nbsp;0208, and JIS&nbsp;X&nbsp;0212
jtulach@1334
   258
 * coded character sets for the Japanese language.
jtulach@1334
   259
 *
jtulach@1334
   260
 * <p> The native character encoding of the Java programming language is
jtulach@1334
   261
 * UTF-16.  A charset in the Java platform therefore defines a mapping
jtulach@1334
   262
 * between sequences of sixteen-bit UTF-16 code units (that is, sequences
jtulach@1334
   263
 * of chars) and sequences of bytes. </p>
jtulach@1334
   264
 *
jtulach@1334
   265
 *
jtulach@1334
   266
 * @author Mark Reinhold
jtulach@1334
   267
 * @author JSR-51 Expert Group
jtulach@1334
   268
 * @since 1.4
jtulach@1334
   269
 *
jtulach@1334
   270
 * @see CharsetDecoder
jtulach@1334
   271
 * @see CharsetEncoder
jtulach@1334
   272
 * @see java.nio.charset.spi.CharsetProvider
jtulach@1334
   273
 * @see java.lang.Character
jtulach@1334
   274
 */
jtulach@1334
   275
jtulach@1334
   276
public abstract class Charset
jtulach@1334
   277
    implements Comparable<Charset>
jtulach@1334
   278
{
jtulach@1334
   279
jtulach@1334
   280
    /* -- Static methods -- */
jtulach@1334
   281
jtulach@1334
   282
    private static volatile String bugLevel = null;
jtulach@1334
   283
jtulach@1334
   284
    static boolean atBugLevel(String bl) {              // package-private
jtulach@1334
   285
        String level = bugLevel;
jtulach@1334
   286
        if (level == null) {
jtulach@1334
   287
            if (!sun.misc.VM.isBooted())
jtulach@1334
   288
                return false;
jtulach@1334
   289
            bugLevel = level = AccessController.doPrivileged(
jtulach@1334
   290
                new GetPropertyAction("sun.nio.cs.bugLevel", ""));
jtulach@1334
   291
        }
jtulach@1334
   292
        return level.equals(bl);
jtulach@1334
   293
    }
jtulach@1334
   294
jtulach@1334
   295
    /**
jtulach@1334
   296
     * Checks that the given string is a legal charset name. </p>
jtulach@1334
   297
     *
jtulach@1334
   298
     * @param  s
jtulach@1334
   299
     *         A purported charset name
jtulach@1334
   300
     *
jtulach@1334
   301
     * @throws  IllegalCharsetNameException
jtulach@1334
   302
     *          If the given name is not a legal charset name
jtulach@1334
   303
     */
jtulach@1334
   304
    private static void checkName(String s) {
jtulach@1334
   305
        int n = s.length();
jtulach@1334
   306
        if (!atBugLevel("1.4")) {
jtulach@1334
   307
            if (n == 0)
jtulach@1334
   308
                throw new IllegalCharsetNameException(s);
jtulach@1334
   309
        }
jtulach@1334
   310
        for (int i = 0; i < n; i++) {
jtulach@1334
   311
            char c = s.charAt(i);
jtulach@1334
   312
            if (c >= 'A' && c <= 'Z') continue;
jtulach@1334
   313
            if (c >= 'a' && c <= 'z') continue;
jtulach@1334
   314
            if (c >= '0' && c <= '9') continue;
jtulach@1334
   315
            if (c == '-' && i != 0) continue;
jtulach@1334
   316
            if (c == '+' && i != 0) continue;
jtulach@1334
   317
            if (c == ':' && i != 0) continue;
jtulach@1334
   318
            if (c == '_' && i != 0) continue;
jtulach@1334
   319
            if (c == '.' && i != 0) continue;
jtulach@1334
   320
            throw new IllegalCharsetNameException(s);
jtulach@1334
   321
        }
jtulach@1334
   322
    }
jtulach@1334
   323
jtulach@1334
   324
    /* The standard set of charsets */
jtulach@1334
   325
    private static CharsetProvider standardProvider = new StandardCharsets();
jtulach@1334
   326
jtulach@1334
   327
    // Cache of the most-recently-returned charsets,
jtulach@1334
   328
    // along with the names that were used to find them
jtulach@1334
   329
    //
jtulach@1334
   330
    private static volatile Object[] cache1 = null; // "Level 1" cache
jtulach@1334
   331
    private static volatile Object[] cache2 = null; // "Level 2" cache
jtulach@1334
   332
jtulach@1334
   333
    private static void cache(String charsetName, Charset cs) {
jtulach@1334
   334
        cache2 = cache1;
jtulach@1334
   335
        cache1 = new Object[] { charsetName, cs };
jtulach@1334
   336
    }
jtulach@1334
   337
jtulach@1334
   338
    // Creates an iterator that walks over the available providers, ignoring
jtulach@1334
   339
    // those whose lookup or instantiation causes a security exception to be
jtulach@1334
   340
    // thrown.  Should be invoked with full privileges.
jtulach@1334
   341
    //
jtulach@1334
   342
    private static Iterator providers() {
jtulach@1334
   343
        return new Iterator() {
jtulach@1334
   344
jtulach@1334
   345
                ClassLoader cl = ClassLoader.getSystemClassLoader();
jtulach@1334
   346
                ServiceLoader<CharsetProvider> sl =
jtulach@1334
   347
                    ServiceLoader.load(CharsetProvider.class, cl);
jtulach@1334
   348
                Iterator<CharsetProvider> i = sl.iterator();
jtulach@1334
   349
jtulach@1334
   350
                Object next = null;
jtulach@1334
   351
jtulach@1334
   352
                private boolean getNext() {
jtulach@1334
   353
                    while (next == null) {
jtulach@1334
   354
                        try {
jtulach@1334
   355
                            if (!i.hasNext())
jtulach@1334
   356
                                return false;
jtulach@1334
   357
                            next = i.next();
jtulach@1334
   358
                        } catch (ServiceConfigurationError sce) {
jtulach@1334
   359
                            if (sce.getCause() instanceof SecurityException) {
jtulach@1334
   360
                                // Ignore security exceptions
jtulach@1334
   361
                                continue;
jtulach@1334
   362
                            }
jtulach@1334
   363
                            throw sce;
jtulach@1334
   364
                        }
jtulach@1334
   365
                    }
jtulach@1334
   366
                    return true;
jtulach@1334
   367
                }
jtulach@1334
   368
jtulach@1334
   369
                public boolean hasNext() {
jtulach@1334
   370
                    return getNext();
jtulach@1334
   371
                }
jtulach@1334
   372
jtulach@1334
   373
                public Object next() {
jtulach@1334
   374
                    if (!getNext())
jtulach@1334
   375
                        throw new NoSuchElementException();
jtulach@1334
   376
                    Object n = next;
jtulach@1334
   377
                    next = null;
jtulach@1334
   378
                    return n;
jtulach@1334
   379
                }
jtulach@1334
   380
jtulach@1334
   381
                public void remove() {
jtulach@1334
   382
                    throw new UnsupportedOperationException();
jtulach@1334
   383
                }
jtulach@1334
   384
jtulach@1334
   385
            };
jtulach@1334
   386
    }
jtulach@1334
   387
jtulach@1334
   388
    // Thread-local gate to prevent recursive provider lookups
jtulach@1334
   389
    private static ThreadLocal<ThreadLocal> gate = new ThreadLocal<ThreadLocal>();
jtulach@1334
   390
jtulach@1334
   391
    private static Charset lookupViaProviders(final String charsetName) {
jtulach@1334
   392
jtulach@1334
   393
        // The runtime startup sequence looks up standard charsets as a
jtulach@1334
   394
        // consequence of the VM's invocation of System.initializeSystemClass
jtulach@1334
   395
        // in order to, e.g., set system properties and encode filenames.  At
jtulach@1334
   396
        // that point the application class loader has not been initialized,
jtulach@1334
   397
        // however, so we can't look for providers because doing so will cause
jtulach@1334
   398
        // that loader to be prematurely initialized with incomplete
jtulach@1334
   399
        // information.
jtulach@1334
   400
        //
jtulach@1334
   401
        if (!sun.misc.VM.isBooted())
jtulach@1334
   402
            return null;
jtulach@1334
   403
jtulach@1334
   404
        if (gate.get() != null)
jtulach@1334
   405
            // Avoid recursive provider lookups
jtulach@1334
   406
            return null;
jtulach@1334
   407
        try {
jtulach@1334
   408
            gate.set(gate);
jtulach@1334
   409
jtulach@1334
   410
            return AccessController.doPrivileged(
jtulach@1334
   411
                new PrivilegedAction<Charset>() {
jtulach@1334
   412
                    public Charset run() {
jtulach@1334
   413
                        for (Iterator i = providers(); i.hasNext();) {
jtulach@1334
   414
                            CharsetProvider cp = (CharsetProvider)i.next();
jtulach@1334
   415
                            Charset cs = cp.charsetForName(charsetName);
jtulach@1334
   416
                            if (cs != null)
jtulach@1334
   417
                                return cs;
jtulach@1334
   418
                        }
jtulach@1334
   419
                        return null;
jtulach@1334
   420
                    }
jtulach@1334
   421
                });
jtulach@1334
   422
jtulach@1334
   423
        } finally {
jtulach@1334
   424
            gate.set(null);
jtulach@1334
   425
        }
jtulach@1334
   426
    }
jtulach@1334
   427
jtulach@1334
   428
    /* The extended set of charsets */
jtulach@1334
   429
    private static Object extendedProviderLock = new Object();
jtulach@1334
   430
    private static boolean extendedProviderProbed = false;
jtulach@1334
   431
    private static CharsetProvider extendedProvider = null;
jtulach@1334
   432
jtulach@1334
   433
    private static void probeExtendedProvider() {
jtulach@1334
   434
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
jtulach@1334
   435
                public Object run() {
jtulach@1334
   436
                    try {
jtulach@1334
   437
                        Class epc
jtulach@1334
   438
                            = Class.forName("sun.nio.cs.ext.ExtendedCharsets");
jtulach@1334
   439
                        extendedProvider = (CharsetProvider)epc.newInstance();
jtulach@1334
   440
                    } catch (ClassNotFoundException x) {
jtulach@1334
   441
                        // Extended charsets not available
jtulach@1334
   442
                        // (charsets.jar not present)
jtulach@1334
   443
                    } catch (InstantiationException x) {
jtulach@1334
   444
                        throw new Error(x);
jtulach@1334
   445
                    } catch (IllegalAccessException x) {
jtulach@1334
   446
                        throw new Error(x);
jtulach@1334
   447
                    }
jtulach@1334
   448
                    return null;
jtulach@1334
   449
                }
jtulach@1334
   450
            });
jtulach@1334
   451
    }
jtulach@1334
   452
jtulach@1334
   453
    private static Charset lookupExtendedCharset(String charsetName) {
jtulach@1334
   454
        CharsetProvider ecp = null;
jtulach@1334
   455
        synchronized (extendedProviderLock) {
jtulach@1334
   456
            if (!extendedProviderProbed) {
jtulach@1334
   457
                probeExtendedProvider();
jtulach@1334
   458
                extendedProviderProbed = true;
jtulach@1334
   459
            }
jtulach@1334
   460
            ecp = extendedProvider;
jtulach@1334
   461
        }
jtulach@1334
   462
        return (ecp != null) ? ecp.charsetForName(charsetName) : null;
jtulach@1334
   463
    }
jtulach@1334
   464
jtulach@1334
   465
    private static Charset lookup(String charsetName) {
jtulach@1334
   466
        if (charsetName == null)
jtulach@1334
   467
            throw new IllegalArgumentException("Null charset name");
jtulach@1334
   468
jtulach@1334
   469
        Object[] a;
jtulach@1334
   470
        if ((a = cache1) != null && charsetName.equals(a[0]))
jtulach@1334
   471
            return (Charset)a[1];
jtulach@1334
   472
        // We expect most programs to use one Charset repeatedly.
jtulach@1334
   473
        // We convey a hint to this effect to the VM by putting the
jtulach@1334
   474
        // level 1 cache miss code in a separate method.
jtulach@1334
   475
        return lookup2(charsetName);
jtulach@1334
   476
    }
jtulach@1334
   477
jtulach@1334
   478
    private static Charset lookup2(String charsetName) {
jtulach@1334
   479
        Object[] a;
jtulach@1334
   480
        if ((a = cache2) != null && charsetName.equals(a[0])) {
jtulach@1334
   481
            cache2 = cache1;
jtulach@1334
   482
            cache1 = a;
jtulach@1334
   483
            return (Charset)a[1];
jtulach@1334
   484
        }
jtulach@1334
   485
jtulach@1334
   486
        Charset cs;
jtulach@1334
   487
        if ((cs = standardProvider.charsetForName(charsetName)) != null ||
jtulach@1334
   488
            (cs = lookupExtendedCharset(charsetName))           != null ||
jtulach@1334
   489
            (cs = lookupViaProviders(charsetName))              != null)
jtulach@1334
   490
        {
jtulach@1334
   491
            cache(charsetName, cs);
jtulach@1334
   492
            return cs;
jtulach@1334
   493
        }
jtulach@1334
   494
jtulach@1334
   495
        /* Only need to check the name if we didn't find a charset for it */
jtulach@1334
   496
        checkName(charsetName);
jtulach@1334
   497
        return null;
jtulach@1334
   498
    }
jtulach@1334
   499
jtulach@1334
   500
    /**
jtulach@1334
   501
     * Tells whether the named charset is supported. </p>
jtulach@1334
   502
     *
jtulach@1334
   503
     * @param  charsetName
jtulach@1334
   504
     *         The name of the requested charset; may be either
jtulach@1334
   505
     *         a canonical name or an alias
jtulach@1334
   506
     *
jtulach@1334
   507
     * @return  <tt>true</tt> if, and only if, support for the named charset
jtulach@1334
   508
     *          is available in the current Java virtual machine
jtulach@1334
   509
     *
jtulach@1334
   510
     * @throws IllegalCharsetNameException
jtulach@1334
   511
     *         If the given charset name is illegal
jtulach@1334
   512
     *
jtulach@1334
   513
     * @throws  IllegalArgumentException
jtulach@1334
   514
     *          If the given <tt>charsetName</tt> is null
jtulach@1334
   515
     */
jtulach@1334
   516
    public static boolean isSupported(String charsetName) {
jtulach@1334
   517
        return (lookup(charsetName) != null);
jtulach@1334
   518
    }
jtulach@1334
   519
jtulach@1334
   520
    /**
jtulach@1334
   521
     * Returns a charset object for the named charset. </p>
jtulach@1334
   522
     *
jtulach@1334
   523
     * @param  charsetName
jtulach@1334
   524
     *         The name of the requested charset; may be either
jtulach@1334
   525
     *         a canonical name or an alias
jtulach@1334
   526
     *
jtulach@1334
   527
     * @return  A charset object for the named charset
jtulach@1334
   528
     *
jtulach@1334
   529
     * @throws  IllegalCharsetNameException
jtulach@1334
   530
     *          If the given charset name is illegal
jtulach@1334
   531
     *
jtulach@1334
   532
     * @throws  IllegalArgumentException
jtulach@1334
   533
     *          If the given <tt>charsetName</tt> is null
jtulach@1334
   534
     *
jtulach@1334
   535
     * @throws  UnsupportedCharsetException
jtulach@1334
   536
     *          If no support for the named charset is available
jtulach@1334
   537
     *          in this instance of the Java virtual machine
jtulach@1334
   538
     */
jtulach@1334
   539
    public static Charset forName(String charsetName) {
jtulach@1334
   540
        Charset cs = lookup(charsetName);
jtulach@1334
   541
        if (cs != null)
jtulach@1334
   542
            return cs;
jtulach@1334
   543
        throw new UnsupportedCharsetException(charsetName);
jtulach@1334
   544
    }
jtulach@1334
   545
jtulach@1334
   546
    // Fold charsets from the given iterator into the given map, ignoring
jtulach@1334
   547
    // charsets whose names already have entries in the map.
jtulach@1334
   548
    //
jtulach@1334
   549
    private static void put(Iterator<Charset> i, Map<String,Charset> m) {
jtulach@1334
   550
        while (i.hasNext()) {
jtulach@1334
   551
            Charset cs = i.next();
jtulach@1334
   552
            if (!m.containsKey(cs.name()))
jtulach@1334
   553
                m.put(cs.name(), cs);
jtulach@1334
   554
        }
jtulach@1334
   555
    }
jtulach@1334
   556
jtulach@1334
   557
    /**
jtulach@1334
   558
     * Constructs a sorted map from canonical charset names to charset objects.
jtulach@1334
   559
     *
jtulach@1334
   560
     * <p> The map returned by this method will have one entry for each charset
jtulach@1334
   561
     * for which support is available in the current Java virtual machine.  If
jtulach@1334
   562
     * two or more supported charsets have the same canonical name then the
jtulach@1334
   563
     * resulting map will contain just one of them; which one it will contain
jtulach@1334
   564
     * is not specified. </p>
jtulach@1334
   565
     *
jtulach@1334
   566
     * <p> The invocation of this method, and the subsequent use of the
jtulach@1334
   567
     * resulting map, may cause time-consuming disk or network I/O operations
jtulach@1334
   568
     * to occur.  This method is provided for applications that need to
jtulach@1334
   569
     * enumerate all of the available charsets, for example to allow user
jtulach@1334
   570
     * charset selection.  This method is not used by the {@link #forName
jtulach@1334
   571
     * forName} method, which instead employs an efficient incremental lookup
jtulach@1334
   572
     * algorithm.
jtulach@1334
   573
     *
jtulach@1334
   574
     * <p> This method may return different results at different times if new
jtulach@1334
   575
     * charset providers are dynamically made available to the current Java
jtulach@1334
   576
     * virtual machine.  In the absence of such changes, the charsets returned
jtulach@1334
   577
     * by this method are exactly those that can be retrieved via the {@link
jtulach@1334
   578
     * #forName forName} method.  </p>
jtulach@1334
   579
     *
jtulach@1334
   580
     * @return An immutable, case-insensitive map from canonical charset names
jtulach@1334
   581
     *         to charset objects
jtulach@1334
   582
     */
jtulach@1334
   583
    public static SortedMap<String,Charset> availableCharsets() {
jtulach@1334
   584
        return AccessController.doPrivileged(
jtulach@1334
   585
            new PrivilegedAction<SortedMap<String,Charset>>() {
jtulach@1334
   586
                public SortedMap<String,Charset> run() {
jtulach@1334
   587
                    TreeMap<String,Charset> m =
jtulach@1334
   588
                        new TreeMap<String,Charset>(
jtulach@1334
   589
                            ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
jtulach@1334
   590
                    put(standardProvider.charsets(), m);
jtulach@1334
   591
                    for (Iterator i = providers(); i.hasNext();) {
jtulach@1334
   592
                        CharsetProvider cp = (CharsetProvider)i.next();
jtulach@1334
   593
                        put(cp.charsets(), m);
jtulach@1334
   594
                    }
jtulach@1334
   595
                    return Collections.unmodifiableSortedMap(m);
jtulach@1334
   596
                }
jtulach@1334
   597
            });
jtulach@1334
   598
    }
jtulach@1334
   599
jtulach@1334
   600
    private static volatile Charset defaultCharset;
jtulach@1334
   601
jtulach@1334
   602
    /**
jtulach@1334
   603
     * Returns the default charset of this Java virtual machine.
jtulach@1334
   604
     *
jtulach@1334
   605
     * <p> The default charset is determined during virtual-machine startup and
jtulach@1334
   606
     * typically depends upon the locale and charset of the underlying
jtulach@1334
   607
     * operating system.
jtulach@1334
   608
     *
jtulach@1334
   609
     * @return  A charset object for the default charset
jtulach@1334
   610
     *
jtulach@1334
   611
     * @since 1.5
jtulach@1334
   612
     */
jtulach@1334
   613
    public static Charset defaultCharset() {
jtulach@1334
   614
        if (defaultCharset == null) {
jtulach@1334
   615
            synchronized (Charset.class) {
jtulach@1334
   616
                String csn = AccessController.doPrivileged(
jtulach@1334
   617
                    new GetPropertyAction("file.encoding"));
jtulach@1334
   618
                Charset cs = lookup(csn);
jtulach@1334
   619
                if (cs != null)
jtulach@1334
   620
                    defaultCharset = cs;
jtulach@1334
   621
                else
jtulach@1334
   622
                    defaultCharset = forName("UTF-8");
jtulach@1334
   623
            }
jtulach@1334
   624
        }
jtulach@1334
   625
        return defaultCharset;
jtulach@1334
   626
    }
jtulach@1334
   627
jtulach@1334
   628
jtulach@1334
   629
    /* -- Instance fields and methods -- */
jtulach@1334
   630
jtulach@1334
   631
    private final String name;          // tickles a bug in oldjavac
jtulach@1334
   632
    private final String[] aliases;     // tickles a bug in oldjavac
jtulach@1334
   633
    private Set<String> aliasSet = null;
jtulach@1334
   634
jtulach@1334
   635
    /**
jtulach@1334
   636
     * Initializes a new charset with the given canonical name and alias
jtulach@1334
   637
     * set. </p>
jtulach@1334
   638
     *
jtulach@1334
   639
     * @param  canonicalName
jtulach@1334
   640
     *         The canonical name of this charset
jtulach@1334
   641
     *
jtulach@1334
   642
     * @param  aliases
jtulach@1334
   643
     *         An array of this charset's aliases, or null if it has no aliases
jtulach@1334
   644
     *
jtulach@1334
   645
     * @throws IllegalCharsetNameException
jtulach@1334
   646
     *         If the canonical name or any of the aliases are illegal
jtulach@1334
   647
     */
jtulach@1334
   648
    protected Charset(String canonicalName, String[] aliases) {
jtulach@1334
   649
        checkName(canonicalName);
jtulach@1334
   650
        String[] as = (aliases == null) ? new String[0] : aliases;
jtulach@1334
   651
        for (int i = 0; i < as.length; i++)
jtulach@1334
   652
            checkName(as[i]);
jtulach@1334
   653
        this.name = canonicalName;
jtulach@1334
   654
        this.aliases = as;
jtulach@1334
   655
    }
jtulach@1334
   656
jtulach@1334
   657
    /**
jtulach@1334
   658
     * Returns this charset's canonical name. </p>
jtulach@1334
   659
     *
jtulach@1334
   660
     * @return  The canonical name of this charset
jtulach@1334
   661
     */
jtulach@1334
   662
    public final String name() {
jtulach@1334
   663
        return name;
jtulach@1334
   664
    }
jtulach@1334
   665
jtulach@1334
   666
    /**
jtulach@1334
   667
     * Returns a set containing this charset's aliases. </p>
jtulach@1334
   668
     *
jtulach@1334
   669
     * @return  An immutable set of this charset's aliases
jtulach@1334
   670
     */
jtulach@1334
   671
    public final Set<String> aliases() {
jtulach@1334
   672
        if (aliasSet != null)
jtulach@1334
   673
            return aliasSet;
jtulach@1334
   674
        int n = aliases.length;
jtulach@1334
   675
        HashSet<String> hs = new HashSet<String>(n);
jtulach@1334
   676
        for (int i = 0; i < n; i++)
jtulach@1334
   677
            hs.add(aliases[i]);
jtulach@1334
   678
        aliasSet = Collections.unmodifiableSet(hs);
jtulach@1334
   679
        return aliasSet;
jtulach@1334
   680
    }
jtulach@1334
   681
jtulach@1334
   682
    /**
jtulach@1334
   683
     * Returns this charset's human-readable name for the default locale.
jtulach@1334
   684
     *
jtulach@1334
   685
     * <p> The default implementation of this method simply returns this
jtulach@1334
   686
     * charset's canonical name.  Concrete subclasses of this class may
jtulach@1334
   687
     * override this method in order to provide a localized display name. </p>
jtulach@1334
   688
     *
jtulach@1334
   689
     * @return  The display name of this charset in the default locale
jtulach@1334
   690
     */
jtulach@1334
   691
    public String displayName() {
jtulach@1334
   692
        return name;
jtulach@1334
   693
    }
jtulach@1334
   694
jtulach@1334
   695
    /**
jtulach@1334
   696
     * Tells whether or not this charset is registered in the <a
jtulach@1334
   697
     * href="http://www.iana.org/assignments/character-sets">IANA Charset
jtulach@1334
   698
     * Registry</a>.  </p>
jtulach@1334
   699
     *
jtulach@1334
   700
     * @return  <tt>true</tt> if, and only if, this charset is known by its
jtulach@1334
   701
     *          implementor to be registered with the IANA
jtulach@1334
   702
     */
jtulach@1334
   703
    public final boolean isRegistered() {
jtulach@1334
   704
        return !name.startsWith("X-") && !name.startsWith("x-");
jtulach@1334
   705
    }
jtulach@1334
   706
jtulach@1334
   707
    /**
jtulach@1334
   708
     * Returns this charset's human-readable name for the given locale.
jtulach@1334
   709
     *
jtulach@1334
   710
     * <p> The default implementation of this method simply returns this
jtulach@1334
   711
     * charset's canonical name.  Concrete subclasses of this class may
jtulach@1334
   712
     * override this method in order to provide a localized display name. </p>
jtulach@1334
   713
     *
jtulach@1334
   714
     * @param  locale
jtulach@1334
   715
     *         The locale for which the display name is to be retrieved
jtulach@1334
   716
     *
jtulach@1334
   717
     * @return  The display name of this charset in the given locale
jtulach@1334
   718
     */
jtulach@1334
   719
    public String displayName(Locale locale) {
jtulach@1334
   720
        return name;
jtulach@1334
   721
    }
jtulach@1334
   722
jtulach@1334
   723
    /**
jtulach@1334
   724
     * Tells whether or not this charset contains the given charset.
jtulach@1334
   725
     *
jtulach@1334
   726
     * <p> A charset <i>C</i> is said to <i>contain</i> a charset <i>D</i> if,
jtulach@1334
   727
     * and only if, every character representable in <i>D</i> is also
jtulach@1334
   728
     * representable in <i>C</i>.  If this relationship holds then it is
jtulach@1334
   729
     * guaranteed that every string that can be encoded in <i>D</i> can also be
jtulach@1334
   730
     * encoded in <i>C</i> without performing any replacements.
jtulach@1334
   731
     *
jtulach@1334
   732
     * <p> That <i>C</i> contains <i>D</i> does not imply that each character
jtulach@1334
   733
     * representable in <i>C</i> by a particular byte sequence is represented
jtulach@1334
   734
     * in <i>D</i> by the same byte sequence, although sometimes this is the
jtulach@1334
   735
     * case.
jtulach@1334
   736
     *
jtulach@1334
   737
     * <p> Every charset contains itself.
jtulach@1334
   738
     *
jtulach@1334
   739
     * <p> This method computes an approximation of the containment relation:
jtulach@1334
   740
     * If it returns <tt>true</tt> then the given charset is known to be
jtulach@1334
   741
     * contained by this charset; if it returns <tt>false</tt>, however, then
jtulach@1334
   742
     * it is not necessarily the case that the given charset is not contained
jtulach@1334
   743
     * in this charset.
jtulach@1334
   744
     *
jtulach@1334
   745
     * @return  <tt>true</tt> if the given charset is contained in this charset
jtulach@1334
   746
     */
jtulach@1334
   747
    public abstract boolean contains(Charset cs);
jtulach@1334
   748
jtulach@1334
   749
    /**
jtulach@1334
   750
     * Constructs a new decoder for this charset. </p>
jtulach@1334
   751
     *
jtulach@1334
   752
     * @return  A new decoder for this charset
jtulach@1334
   753
     */
jtulach@1334
   754
    public abstract CharsetDecoder newDecoder();
jtulach@1334
   755
jtulach@1334
   756
    /**
jtulach@1334
   757
     * Constructs a new encoder for this charset. </p>
jtulach@1334
   758
     *
jtulach@1334
   759
     * @return  A new encoder for this charset
jtulach@1334
   760
     *
jtulach@1334
   761
     * @throws  UnsupportedOperationException
jtulach@1334
   762
     *          If this charset does not support encoding
jtulach@1334
   763
     */
jtulach@1334
   764
    public abstract CharsetEncoder newEncoder();
jtulach@1334
   765
jtulach@1334
   766
    /**
jtulach@1334
   767
     * Tells whether or not this charset supports encoding.
jtulach@1334
   768
     *
jtulach@1334
   769
     * <p> Nearly all charsets support encoding.  The primary exceptions are
jtulach@1334
   770
     * special-purpose <i>auto-detect</i> charsets whose decoders can determine
jtulach@1334
   771
     * which of several possible encoding schemes is in use by examining the
jtulach@1334
   772
     * input byte sequence.  Such charsets do not support encoding because
jtulach@1334
   773
     * there is no way to determine which encoding should be used on output.
jtulach@1334
   774
     * Implementations of such charsets should override this method to return
jtulach@1334
   775
     * <tt>false</tt>. </p>
jtulach@1334
   776
     *
jtulach@1334
   777
     * @return  <tt>true</tt> if, and only if, this charset supports encoding
jtulach@1334
   778
     */
jtulach@1334
   779
    public boolean canEncode() {
jtulach@1334
   780
        return true;
jtulach@1334
   781
    }
jtulach@1334
   782
jtulach@1334
   783
    /**
jtulach@1334
   784
     * Convenience method that decodes bytes in this charset into Unicode
jtulach@1334
   785
     * characters.
jtulach@1334
   786
     *
jtulach@1334
   787
     * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
jtulach@1334
   788
     * same result as the expression
jtulach@1334
   789
     *
jtulach@1334
   790
     * <pre>
jtulach@1334
   791
     *     cs.newDecoder()
jtulach@1334
   792
     *       .onMalformedInput(CodingErrorAction.REPLACE)
jtulach@1334
   793
     *       .onUnmappableCharacter(CodingErrorAction.REPLACE)
jtulach@1334
   794
     *       .decode(bb); </pre>
jtulach@1334
   795
     *
jtulach@1334
   796
     * except that it is potentially more efficient because it can cache
jtulach@1334
   797
     * decoders between successive invocations.
jtulach@1334
   798
     *
jtulach@1334
   799
     * <p> This method always replaces malformed-input and unmappable-character
jtulach@1334
   800
     * sequences with this charset's default replacement byte array.  In order
jtulach@1334
   801
     * to detect such sequences, use the {@link
jtulach@1334
   802
     * CharsetDecoder#decode(java.nio.ByteBuffer)} method directly.  </p>
jtulach@1334
   803
     *
jtulach@1334
   804
     * @param  bb  The byte buffer to be decoded
jtulach@1334
   805
     *
jtulach@1334
   806
     * @return  A char buffer containing the decoded characters
jtulach@1334
   807
     */
jtulach@1334
   808
    public final CharBuffer decode(ByteBuffer bb) {
jtulach@1334
   809
        try {
jtulach@1334
   810
            return ThreadLocalCoders.decoderFor(this)
jtulach@1334
   811
                .onMalformedInput(CodingErrorAction.REPLACE)
jtulach@1334
   812
                .onUnmappableCharacter(CodingErrorAction.REPLACE)
jtulach@1334
   813
                .decode(bb);
jtulach@1334
   814
        } catch (CharacterCodingException x) {
jtulach@1334
   815
            throw new Error(x);         // Can't happen
jtulach@1334
   816
        }
jtulach@1334
   817
    }
jtulach@1334
   818
jtulach@1334
   819
    /**
jtulach@1334
   820
     * Convenience method that encodes Unicode characters into bytes in this
jtulach@1334
   821
     * charset.
jtulach@1334
   822
     *
jtulach@1334
   823
     * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
jtulach@1334
   824
     * same result as the expression
jtulach@1334
   825
     *
jtulach@1334
   826
     * <pre>
jtulach@1334
   827
     *     cs.newEncoder()
jtulach@1334
   828
     *       .onMalformedInput(CodingErrorAction.REPLACE)
jtulach@1334
   829
     *       .onUnmappableCharacter(CodingErrorAction.REPLACE)
jtulach@1334
   830
     *       .encode(bb); </pre>
jtulach@1334
   831
     *
jtulach@1334
   832
     * except that it is potentially more efficient because it can cache
jtulach@1334
   833
     * encoders between successive invocations.
jtulach@1334
   834
     *
jtulach@1334
   835
     * <p> This method always replaces malformed-input and unmappable-character
jtulach@1334
   836
     * sequences with this charset's default replacement string.  In order to
jtulach@1334
   837
     * detect such sequences, use the {@link
jtulach@1334
   838
     * CharsetEncoder#encode(java.nio.CharBuffer)} method directly.  </p>
jtulach@1334
   839
     *
jtulach@1334
   840
     * @param  cb  The char buffer to be encoded
jtulach@1334
   841
     *
jtulach@1334
   842
     * @return  A byte buffer containing the encoded characters
jtulach@1334
   843
     */
jtulach@1334
   844
    public final ByteBuffer encode(CharBuffer cb) {
jtulach@1334
   845
        try {
jtulach@1334
   846
            return ThreadLocalCoders.encoderFor(this)
jtulach@1334
   847
                .onMalformedInput(CodingErrorAction.REPLACE)
jtulach@1334
   848
                .onUnmappableCharacter(CodingErrorAction.REPLACE)
jtulach@1334
   849
                .encode(cb);
jtulach@1334
   850
        } catch (CharacterCodingException x) {
jtulach@1334
   851
            throw new Error(x);         // Can't happen
jtulach@1334
   852
        }
jtulach@1334
   853
    }
jtulach@1334
   854
jtulach@1334
   855
    /**
jtulach@1334
   856
     * Convenience method that encodes a string into bytes in this charset.
jtulach@1334
   857
     *
jtulach@1334
   858
     * <p> An invocation of this method upon a charset <tt>cs</tt> returns the
jtulach@1334
   859
     * same result as the expression
jtulach@1334
   860
     *
jtulach@1334
   861
     * <pre>
jtulach@1334
   862
     *     cs.encode(CharBuffer.wrap(s)); </pre>
jtulach@1334
   863
     *
jtulach@1334
   864
     * @param  str  The string to be encoded
jtulach@1334
   865
     *
jtulach@1334
   866
     * @return  A byte buffer containing the encoded characters
jtulach@1334
   867
     */
jtulach@1334
   868
    public final ByteBuffer encode(String str) {
jtulach@1334
   869
        return encode(CharBuffer.wrap(str));
jtulach@1334
   870
    }
jtulach@1334
   871
jtulach@1334
   872
    /**
jtulach@1334
   873
     * Compares this charset to another.
jtulach@1334
   874
     *
jtulach@1334
   875
     * <p> Charsets are ordered by their canonical names, without regard to
jtulach@1334
   876
     * case. </p>
jtulach@1334
   877
     *
jtulach@1334
   878
     * @param  that
jtulach@1334
   879
     *         The charset to which this charset is to be compared
jtulach@1334
   880
     *
jtulach@1334
   881
     * @return A negative integer, zero, or a positive integer as this charset
jtulach@1334
   882
     *         is less than, equal to, or greater than the specified charset
jtulach@1334
   883
     */
jtulach@1334
   884
    public final int compareTo(Charset that) {
jtulach@1334
   885
        return (name().compareToIgnoreCase(that.name()));
jtulach@1334
   886
    }
jtulach@1334
   887
jtulach@1334
   888
    /**
jtulach@1334
   889
     * Computes a hashcode for this charset. </p>
jtulach@1334
   890
     *
jtulach@1334
   891
     * @return  An integer hashcode
jtulach@1334
   892
     */
jtulach@1334
   893
    public final int hashCode() {
jtulach@1334
   894
        return name().hashCode();
jtulach@1334
   895
    }
jtulach@1334
   896
jtulach@1334
   897
    /**
jtulach@1334
   898
     * Tells whether or not this object is equal to another.
jtulach@1334
   899
     *
jtulach@1334
   900
     * <p> Two charsets are equal if, and only if, they have the same canonical
jtulach@1334
   901
     * names.  A charset is never equal to any other type of object.  </p>
jtulach@1334
   902
     *
jtulach@1334
   903
     * @return  <tt>true</tt> if, and only if, this charset is equal to the
jtulach@1334
   904
     *          given object
jtulach@1334
   905
     */
jtulach@1334
   906
    public final boolean equals(Object ob) {
jtulach@1334
   907
        if (!(ob instanceof Charset))
jtulach@1334
   908
            return false;
jtulach@1334
   909
        if (this == ob)
jtulach@1334
   910
            return true;
jtulach@1334
   911
        return name.equals(((Charset)ob).name());
jtulach@1334
   912
    }
jtulach@1334
   913
jtulach@1334
   914
    /**
jtulach@1334
   915
     * Returns a string describing this charset. </p>
jtulach@1334
   916
     *
jtulach@1334
   917
     * @return  A string describing this charset
jtulach@1334
   918
     */
jtulach@1334
   919
    public final String toString() {
jtulach@1334
   920
        return name();
jtulach@1334
   921
    }
jtulach@1334
   922
jtulach@1334
   923
}