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