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