jtulach@1334: /* jtulach@1334: * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: package java.nio.charset; jtulach@1334: jaroslav@1343: //import java.nio.ByteBuffer; jaroslav@1343: //import java.nio.CharBuffer; jtulach@1334: import java.util.Collections; jtulach@1334: import java.util.HashSet; jtulach@1334: import java.util.Iterator; jtulach@1334: import java.util.Locale; jtulach@1334: import java.util.Map; jtulach@1334: import java.util.Set; jtulach@1334: import java.util.SortedMap; jtulach@1334: import java.util.TreeMap; jtulach@1334: jtulach@1334: jtulach@1334: /** jtulach@1334: * A named mapping between sequences of sixteen-bit Unicode code units and sequences of jtulach@1334: * bytes. This class defines methods for creating decoders and encoders and jtulach@1334: * for retrieving the various names associated with a charset. Instances of jtulach@1334: * this class are immutable. jtulach@1334: * jtulach@1334: *

This class also defines static methods for testing whether a particular jtulach@1334: * charset is supported, for locating charset instances by name, and for jtulach@1334: * constructing a map that contains every charset for which support is jtulach@1334: * available in the current Java virtual machine. Support for new charsets can jtulach@1334: * be added via the service-provider interface defined in the {@link jtulach@1334: * java.nio.charset.spi.CharsetProvider} class. jtulach@1334: * jtulach@1334: *

All of the methods defined in this class are safe for use by multiple jtulach@1334: * concurrent threads. jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: *

Charset names

jtulach@1334: * jtulach@1334: *

Charsets are named by strings composed of the following characters: jtulach@1334: * jtulach@1334: *

jtulach@1334: * jtulach@1334: * A charset name must begin with either a letter or a digit. The empty string jtulach@1334: * is not a legal charset name. Charset names are not case-sensitive; that is, jtulach@1334: * case is always ignored when comparing charset names. Charset names jtulach@1334: * generally follow the conventions documented in
RFC 2278: IANA Charset jtulach@1334: * Registration Procedures. jtulach@1334: * jtulach@1334: *

Every charset has a canonical name and may also have one or more jtulach@1334: * aliases. The canonical name is returned by the {@link #name() name} method jtulach@1334: * of this class. Canonical names are, by convention, usually in upper case. jtulach@1334: * The aliases of a charset are returned by the {@link #aliases() aliases} jtulach@1334: * method. jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: *

Some charsets have an historical name that is defined for jtulach@1334: * compatibility with previous versions of the Java platform. A charset's jtulach@1334: * historical name is either its canonical name or one of its aliases. The jtulach@1334: * historical name is returned by the getEncoding() methods of the jtulach@1334: * {@link java.io.InputStreamReader#getEncoding InputStreamReader} and {@link jtulach@1334: * java.io.OutputStreamWriter#getEncoding OutputStreamWriter} classes. jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: *

If a charset listed in the IANA Charset jtulach@1334: * Registry is supported by an implementation of the Java platform then jtulach@1334: * its canonical name must be the name listed in the registry. Many charsets jtulach@1334: * are given more than one name in the registry, in which case the registry jtulach@1334: * identifies one of the names as MIME-preferred. If a charset has more jtulach@1334: * than one registry name then its canonical name must be the MIME-preferred jtulach@1334: * name and the other names in the registry must be valid aliases. If a jtulach@1334: * supported charset is not listed in the IANA registry then its canonical name jtulach@1334: * must begin with one of the strings "X-" or "x-". jtulach@1334: * jtulach@1334: *

The IANA charset registry does change over time, and so the canonical jtulach@1334: * name and the aliases of a particular charset may also change over time. To jtulach@1334: * ensure compatibility it is recommended that no alias ever be removed from a jtulach@1334: * charset, and that if the canonical name of a charset is changed then its jtulach@1334: * previous canonical name be made into an alias. jtulach@1334: * jtulach@1334: * jtulach@1334: *

Standard charsets

jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: *

Every implementation of the Java platform is required to support the jtulach@1334: * following standard charsets. Consult the release documentation for your jtulach@1334: * implementation to see if any other charsets are supported. The behavior jtulach@1334: * of such optional charsets may differ between implementations. jtulach@1334: * jtulach@1334: *

jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: * jtulach@1334: *

Charset

Description

US-ASCIISeven-bit ASCII, a.k.a. ISO646-US, jtulach@1334: * a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1  ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8Eight-bit UCS Transformation Format
UTF-16BESixteen-bit UCS Transformation Format, jtulach@1334: * big-endian byte order
UTF-16LESixteen-bit UCS Transformation Format, jtulach@1334: * little-endian byte order
UTF-16Sixteen-bit UCS Transformation Format, jtulach@1334: * byte order identified by an optional byte-order mark
jtulach@1334: * jtulach@1334: *

The UTF-8 charset is specified by RFC 2279; the jtulach@1334: * transformation format upon which it is based is specified in jtulach@1334: * Amendment 2 of ISO 10646-1 and is also described in the Unicode jtulach@1334: * Standard. jtulach@1334: * jtulach@1334: *

The UTF-16 charsets are specified by RFC 2781; the jtulach@1334: * transformation formats upon which they are based are specified in jtulach@1334: * Amendment 1 of ISO 10646-1 and are also described in the Unicode jtulach@1334: * Standard. jtulach@1334: * jtulach@1334: *

The UTF-16 charsets use sixteen-bit quantities and are jtulach@1334: * therefore sensitive to byte order. In these encodings the byte order of a jtulach@1334: * stream may be indicated by an initial byte-order mark represented by jtulach@1334: * the Unicode character '\uFEFF'. Byte-order marks are handled jtulach@1334: * as follows: jtulach@1334: * jtulach@1334: *

jtulach@1334: * jtulach@1334: * In any case, byte order marks occuring after the first element of an jtulach@1334: * input sequence are not omitted since the same code is used to represent jtulach@1334: * ZERO-WIDTH NON-BREAKING SPACE. jtulach@1334: * jtulach@1334: *

Every instance of the Java virtual machine has a default charset, which jtulach@1334: * may or may not be one of the standard charsets. The default charset is jtulach@1334: * determined during virtual-machine startup and typically depends upon the jtulach@1334: * locale and charset being used by the underlying operating system.

jtulach@1334: * jtulach@1334: *

The {@link StandardCharsets} class defines constants for each of the jtulach@1334: * standard charsets. jtulach@1334: * jtulach@1334: *

Terminology

jtulach@1334: * jtulach@1334: *

The name of this class is taken from the terms used in jtulach@1334: * RFC 2278. jtulach@1334: * In that document a charset is defined as the combination of jtulach@1334: * one or more coded character sets and a character-encoding scheme. jtulach@1334: * (This definition is confusing; some other software systems define jtulach@1334: * charset as a synonym for coded character set.) jtulach@1334: * jtulach@1334: *

A coded character set is a mapping between a set of abstract jtulach@1334: * characters and a set of integers. US-ASCII, ISO 8859-1, jtulach@1334: * JIS X 0201, and Unicode are examples of coded character sets. jtulach@1334: * jtulach@1334: *

Some standards have defined a character set to be simply a jtulach@1334: * set of abstract characters without an associated assigned numbering. jtulach@1334: * An alphabet is an example of such a character set. However, the subtle jtulach@1334: * distinction between character set and coded character set jtulach@1334: * is rarely used in practice; the former has become a short form for the jtulach@1334: * latter, including in the Java API specification. jtulach@1334: * jtulach@1334: *

A character-encoding scheme is a mapping between one or more jtulach@1334: * coded character sets and a set of octet (eight-bit byte) sequences. jtulach@1334: * UTF-8, UTF-16, ISO 2022, and EUC are examples of jtulach@1334: * character-encoding schemes. Encoding schemes are often associated with jtulach@1334: * a particular coded character set; UTF-8, for example, is used only to jtulach@1334: * encode Unicode. Some schemes, however, are associated with multiple jtulach@1334: * coded character sets; EUC, for example, can be used to encode jtulach@1334: * characters in a variety of Asian coded character sets. jtulach@1334: * jtulach@1334: *

When a coded character set is used exclusively with a single jtulach@1334: * character-encoding scheme then the corresponding charset is usually jtulach@1334: * named for the coded character set; otherwise a charset is usually named jtulach@1334: * for the encoding scheme and, possibly, the locale of the coded jtulach@1334: * character sets that it supports. Hence US-ASCII is both the jtulach@1334: * name of a coded character set and of the charset that encodes it, while jtulach@1334: * EUC-JP is the name of the charset that encodes the jtulach@1334: * JIS X 0201, JIS X 0208, and JIS X 0212 jtulach@1334: * coded character sets for the Japanese language. jtulach@1334: * jtulach@1334: *

The native character encoding of the Java programming language is jtulach@1334: * UTF-16. A charset in the Java platform therefore defines a mapping jtulach@1334: * between sequences of sixteen-bit UTF-16 code units (that is, sequences jtulach@1334: * of chars) and sequences of bytes.

jtulach@1334: * jtulach@1334: * jtulach@1334: * @author Mark Reinhold jtulach@1334: * @author JSR-51 Expert Group jtulach@1334: * @since 1.4 jtulach@1334: * jtulach@1334: * @see CharsetDecoder jtulach@1334: * @see CharsetEncoder jtulach@1334: * @see java.nio.charset.spi.CharsetProvider jtulach@1334: * @see java.lang.Character jtulach@1334: */ jtulach@1334: jtulach@1334: public abstract class Charset jtulach@1334: implements Comparable jtulach@1334: { jtulach@1334: jtulach@1334: /* -- Static methods -- */ jtulach@1334: jtulach@1334: private static volatile String bugLevel = null; jtulach@1334: jtulach@1334: /** jtulach@1334: * Checks that the given string is a legal charset name.

jtulach@1334: * jtulach@1334: * @param s jtulach@1334: * A purported charset name jtulach@1334: * jtulach@1334: * @throws IllegalCharsetNameException jtulach@1334: * If the given name is not a legal charset name jtulach@1334: */ jtulach@1334: private static void checkName(String s) { jtulach@1334: int n = s.length(); jtulach@1334: if (n == 0) jtulach@1334: throw new IllegalCharsetNameException(s); jtulach@1334: for (int i = 0; i < n; i++) { jtulach@1334: char c = s.charAt(i); jtulach@1334: if (c >= 'A' && c <= 'Z') continue; jtulach@1334: if (c >= 'a' && c <= 'z') continue; jtulach@1334: if (c >= '0' && c <= '9') continue; jtulach@1334: if (c == '-' && i != 0) continue; jtulach@1334: if (c == '+' && i != 0) continue; jtulach@1334: if (c == ':' && i != 0) continue; jtulach@1334: if (c == '_' && i != 0) continue; jtulach@1334: if (c == '.' && i != 0) continue; jtulach@1334: throw new IllegalCharsetNameException(s); jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: // Cache of the most-recently-returned charsets, jtulach@1334: // along with the names that were used to find them jtulach@1334: // jtulach@1334: private static volatile Object[] cache1 = null; // "Level 1" cache jtulach@1334: private static volatile Object[] cache2 = null; // "Level 2" cache jtulach@1334: jtulach@1334: private static void cache(String charsetName, Charset cs) { jtulach@1334: cache2 = cache1; jtulach@1334: cache1 = new Object[] { charsetName, cs }; jtulach@1334: } jtulach@1334: jtulach@1334: // Creates an iterator that walks over the available providers, ignoring jtulach@1334: // those whose lookup or instantiation causes a security exception to be jtulach@1334: // thrown. Should be invoked with full privileges. jtulach@1334: // jtulach@1334: private static Iterator providers() { jaroslav@1343: return Collections.emptyIterator(); jtulach@1334: } jtulach@1334: jtulach@1334: // Thread-local gate to prevent recursive provider lookups jtulach@1334: private static ThreadLocal gate = new ThreadLocal(); jtulach@1334: jtulach@1334: private static Charset lookupViaProviders(final String charsetName) { jaroslav@1343: return null; jtulach@1334: } jtulach@1334: jtulach@1334: /* The extended set of charsets */ jtulach@1334: private static Object extendedProviderLock = new Object(); jtulach@1334: private static boolean extendedProviderProbed = false; jtulach@1334: jtulach@1334: jtulach@1334: private static Charset lookupExtendedCharset(String charsetName) { jaroslav@1343: return null; jtulach@1334: } jtulach@1334: jtulach@1334: private static Charset lookup(String charsetName) { jtulach@1334: if (charsetName == null) jtulach@1334: throw new IllegalArgumentException("Null charset name"); jtulach@1334: jtulach@1334: Object[] a; jtulach@1334: if ((a = cache1) != null && charsetName.equals(a[0])) jtulach@1334: return (Charset)a[1]; jtulach@1334: // We expect most programs to use one Charset repeatedly. jtulach@1334: // We convey a hint to this effect to the VM by putting the jtulach@1334: // level 1 cache miss code in a separate method. jtulach@1334: return lookup2(charsetName); jtulach@1334: } jtulach@1334: jtulach@1334: private static Charset lookup2(String charsetName) { jtulach@1334: Object[] a; jtulach@1334: if ((a = cache2) != null && charsetName.equals(a[0])) { jtulach@1334: cache2 = cache1; jtulach@1334: cache1 = a; jtulach@1334: return (Charset)a[1]; jtulach@1334: } jtulach@1334: jtulach@1334: /* Only need to check the name if we didn't find a charset for it */ jtulach@1334: checkName(charsetName); jtulach@1334: return null; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether the named charset is supported.

jtulach@1334: * jtulach@1334: * @param charsetName jtulach@1334: * The name of the requested charset; may be either jtulach@1334: * a canonical name or an alias jtulach@1334: * jtulach@1334: * @return true if, and only if, support for the named charset jtulach@1334: * is available in the current Java virtual machine jtulach@1334: * jtulach@1334: * @throws IllegalCharsetNameException jtulach@1334: * If the given charset name is illegal jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException jtulach@1334: * If the given charsetName is null jtulach@1334: */ jtulach@1334: public static boolean isSupported(String charsetName) { jtulach@1334: return (lookup(charsetName) != null); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns a charset object for the named charset.

jtulach@1334: * jtulach@1334: * @param charsetName jtulach@1334: * The name of the requested charset; may be either jtulach@1334: * a canonical name or an alias jtulach@1334: * jtulach@1334: * @return A charset object for the named charset jtulach@1334: * jtulach@1334: * @throws IllegalCharsetNameException jtulach@1334: * If the given charset name is illegal jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException jtulach@1334: * If the given charsetName is null jtulach@1334: * jtulach@1334: * @throws UnsupportedCharsetException jtulach@1334: * If no support for the named charset is available jtulach@1334: * in this instance of the Java virtual machine jtulach@1334: */ jtulach@1334: public static Charset forName(String charsetName) { jtulach@1334: Charset cs = lookup(charsetName); jtulach@1334: if (cs != null) jtulach@1334: return cs; jtulach@1334: throw new UnsupportedCharsetException(charsetName); jtulach@1334: } jtulach@1334: jtulach@1334: // Fold charsets from the given iterator into the given map, ignoring jtulach@1334: // charsets whose names already have entries in the map. jtulach@1334: // jtulach@1334: private static void put(Iterator i, Map m) { jtulach@1334: while (i.hasNext()) { jtulach@1334: Charset cs = i.next(); jtulach@1334: if (!m.containsKey(cs.name())) jtulach@1334: m.put(cs.name(), cs); jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Constructs a sorted map from canonical charset names to charset objects. jtulach@1334: * jtulach@1334: *

The map returned by this method will have one entry for each charset jtulach@1334: * for which support is available in the current Java virtual machine. If jtulach@1334: * two or more supported charsets have the same canonical name then the jtulach@1334: * resulting map will contain just one of them; which one it will contain jtulach@1334: * is not specified.

jtulach@1334: * jtulach@1334: *

The invocation of this method, and the subsequent use of the jtulach@1334: * resulting map, may cause time-consuming disk or network I/O operations jtulach@1334: * to occur. This method is provided for applications that need to jtulach@1334: * enumerate all of the available charsets, for example to allow user jtulach@1334: * charset selection. This method is not used by the {@link #forName jtulach@1334: * forName} method, which instead employs an efficient incremental lookup jtulach@1334: * algorithm. jtulach@1334: * jtulach@1334: *

This method may return different results at different times if new jtulach@1334: * charset providers are dynamically made available to the current Java jtulach@1334: * virtual machine. In the absence of such changes, the charsets returned jtulach@1334: * by this method are exactly those that can be retrieved via the {@link jtulach@1334: * #forName forName} method.

jtulach@1334: * jtulach@1334: * @return An immutable, case-insensitive map from canonical charset names jtulach@1334: * to charset objects jtulach@1334: */ jtulach@1334: public static SortedMap availableCharsets() { jaroslav@1343: TreeMap tm = new TreeMap(); jaroslav@1343: tm.put("UTF-8", Charset.defaultCharset()); jaroslav@1343: return tm; jtulach@1334: } jtulach@1334: jtulach@1334: private static volatile Charset defaultCharset; jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the default charset of this Java virtual machine. jtulach@1334: * jtulach@1334: *

The default charset is determined during virtual-machine startup and jtulach@1334: * typically depends upon the locale and charset of the underlying jtulach@1334: * operating system. jtulach@1334: * jtulach@1334: * @return A charset object for the default charset jtulach@1334: * jtulach@1334: * @since 1.5 jtulach@1334: */ jtulach@1334: public static Charset defaultCharset() { jtulach@1334: if (defaultCharset == null) { jaroslav@1343: defaultCharset = forName("UTF-8"); jtulach@1334: } jtulach@1334: return defaultCharset; jtulach@1334: } jtulach@1334: jtulach@1334: jtulach@1334: /* -- Instance fields and methods -- */ jtulach@1334: jtulach@1334: private final String name; // tickles a bug in oldjavac jtulach@1334: private final String[] aliases; // tickles a bug in oldjavac jtulach@1334: private Set aliasSet = null; jtulach@1334: jtulach@1334: /** jtulach@1334: * Initializes a new charset with the given canonical name and alias jtulach@1334: * set.

jtulach@1334: * jtulach@1334: * @param canonicalName jtulach@1334: * The canonical name of this charset jtulach@1334: * jtulach@1334: * @param aliases jtulach@1334: * An array of this charset's aliases, or null if it has no aliases jtulach@1334: * jtulach@1334: * @throws IllegalCharsetNameException jtulach@1334: * If the canonical name or any of the aliases are illegal jtulach@1334: */ jtulach@1334: protected Charset(String canonicalName, String[] aliases) { jtulach@1334: checkName(canonicalName); jtulach@1334: String[] as = (aliases == null) ? new String[0] : aliases; jtulach@1334: for (int i = 0; i < as.length; i++) jtulach@1334: checkName(as[i]); jtulach@1334: this.name = canonicalName; jtulach@1334: this.aliases = as; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns this charset's canonical name.

jtulach@1334: * jtulach@1334: * @return The canonical name of this charset jtulach@1334: */ jtulach@1334: public final String name() { jtulach@1334: return name; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns a set containing this charset's aliases.

jtulach@1334: * jtulach@1334: * @return An immutable set of this charset's aliases jtulach@1334: */ jtulach@1334: public final Set aliases() { jtulach@1334: if (aliasSet != null) jtulach@1334: return aliasSet; jtulach@1334: int n = aliases.length; jtulach@1334: HashSet hs = new HashSet(n); jtulach@1334: for (int i = 0; i < n; i++) jtulach@1334: hs.add(aliases[i]); jtulach@1334: aliasSet = Collections.unmodifiableSet(hs); jtulach@1334: return aliasSet; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns this charset's human-readable name for the default locale. jtulach@1334: * jtulach@1334: *

The default implementation of this method simply returns this jtulach@1334: * charset's canonical name. Concrete subclasses of this class may jtulach@1334: * override this method in order to provide a localized display name.

jtulach@1334: * jtulach@1334: * @return The display name of this charset in the default locale jtulach@1334: */ jtulach@1334: public String displayName() { jtulach@1334: return name; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether or not this charset is registered in the IANA Charset jtulach@1334: * Registry.

jtulach@1334: * jtulach@1334: * @return true if, and only if, this charset is known by its jtulach@1334: * implementor to be registered with the IANA jtulach@1334: */ jtulach@1334: public final boolean isRegistered() { jtulach@1334: return !name.startsWith("X-") && !name.startsWith("x-"); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns this charset's human-readable name for the given locale. jtulach@1334: * jtulach@1334: *

The default implementation of this method simply returns this jtulach@1334: * charset's canonical name. Concrete subclasses of this class may jtulach@1334: * override this method in order to provide a localized display name.

jtulach@1334: * jtulach@1334: * @param locale jtulach@1334: * The locale for which the display name is to be retrieved jtulach@1334: * jtulach@1334: * @return The display name of this charset in the given locale jtulach@1334: */ jtulach@1334: public String displayName(Locale locale) { jtulach@1334: return name; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether or not this charset contains the given charset. jtulach@1334: * jtulach@1334: *

A charset C is said to contain a charset D if, jtulach@1334: * and only if, every character representable in D is also jtulach@1334: * representable in C. If this relationship holds then it is jtulach@1334: * guaranteed that every string that can be encoded in D can also be jtulach@1334: * encoded in C without performing any replacements. jtulach@1334: * jtulach@1334: *

That C contains D does not imply that each character jtulach@1334: * representable in C by a particular byte sequence is represented jtulach@1334: * in D by the same byte sequence, although sometimes this is the jtulach@1334: * case. jtulach@1334: * jtulach@1334: *

Every charset contains itself. jtulach@1334: * jtulach@1334: *

This method computes an approximation of the containment relation: jtulach@1334: * If it returns true then the given charset is known to be jtulach@1334: * contained by this charset; if it returns false, however, then jtulach@1334: * it is not necessarily the case that the given charset is not contained jtulach@1334: * in this charset. jtulach@1334: * jtulach@1334: * @return true if the given charset is contained in this charset jtulach@1334: */ jtulach@1334: public abstract boolean contains(Charset cs); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constructs a new decoder for this charset.

jtulach@1334: * jtulach@1334: * @return A new decoder for this charset jtulach@1334: */ jtulach@1334: public abstract CharsetDecoder newDecoder(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constructs a new encoder for this charset.

jtulach@1334: * jtulach@1334: * @return A new encoder for this charset jtulach@1334: * jtulach@1334: * @throws UnsupportedOperationException jtulach@1334: * If this charset does not support encoding jtulach@1334: */ jtulach@1334: public abstract CharsetEncoder newEncoder(); jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether or not this charset supports encoding. jtulach@1334: * jtulach@1334: *

Nearly all charsets support encoding. The primary exceptions are jtulach@1334: * special-purpose auto-detect charsets whose decoders can determine jtulach@1334: * which of several possible encoding schemes is in use by examining the jtulach@1334: * input byte sequence. Such charsets do not support encoding because jtulach@1334: * there is no way to determine which encoding should be used on output. jtulach@1334: * Implementations of such charsets should override this method to return jtulach@1334: * false.

jtulach@1334: * jtulach@1334: * @return true if, and only if, this charset supports encoding jtulach@1334: */ jtulach@1334: public boolean canEncode() { jtulach@1334: return true; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Convenience method that decodes bytes in this charset into Unicode jtulach@1334: * characters. jtulach@1334: * jtulach@1334: *

An invocation of this method upon a charset cs returns the jtulach@1334: * same result as the expression jtulach@1334: * jtulach@1334: *

jtulach@1334:      *     cs.newDecoder()
jtulach@1334:      *       .onMalformedInput(CodingErrorAction.REPLACE)
jtulach@1334:      *       .onUnmappableCharacter(CodingErrorAction.REPLACE)
jtulach@1334:      *       .decode(bb); 
jtulach@1334: * jtulach@1334: * except that it is potentially more efficient because it can cache jtulach@1334: * decoders between successive invocations. jtulach@1334: * jtulach@1334: *

This method always replaces malformed-input and unmappable-character jtulach@1334: * sequences with this charset's default replacement byte array. In order jtulach@1334: * to detect such sequences, use the {@link jtulach@1334: * CharsetDecoder#decode(java.nio.ByteBuffer)} method directly.

jtulach@1334: * jtulach@1334: * @param bb The byte buffer to be decoded jtulach@1334: * jtulach@1334: * @return A char buffer containing the decoded characters jtulach@1334: */ jaroslav@1343: // public final CharBuffer decode(ByteBuffer bb) { jaroslav@1343: // try { jaroslav@1343: // return ThreadLocalCoders.decoderFor(this) jaroslav@1343: // .onMalformedInput(CodingErrorAction.REPLACE) jaroslav@1343: // .onUnmappableCharacter(CodingErrorAction.REPLACE) jaroslav@1343: // .decode(bb); jaroslav@1343: // } catch (CharacterCodingException x) { jaroslav@1343: // throw new Error(x); // Can't happen jaroslav@1343: // } jaroslav@1343: // } jtulach@1334: jtulach@1334: /** jtulach@1334: * Convenience method that encodes Unicode characters into bytes in this jtulach@1334: * charset. jtulach@1334: * jtulach@1334: *

An invocation of this method upon a charset cs returns the jtulach@1334: * same result as the expression jtulach@1334: * jtulach@1334: *

jtulach@1334:      *     cs.newEncoder()
jtulach@1334:      *       .onMalformedInput(CodingErrorAction.REPLACE)
jtulach@1334:      *       .onUnmappableCharacter(CodingErrorAction.REPLACE)
jtulach@1334:      *       .encode(bb); 
jtulach@1334: * jtulach@1334: * except that it is potentially more efficient because it can cache jtulach@1334: * encoders between successive invocations. jtulach@1334: * jtulach@1334: *

This method always replaces malformed-input and unmappable-character jtulach@1334: * sequences with this charset's default replacement string. In order to jtulach@1334: * detect such sequences, use the {@link jtulach@1334: * CharsetEncoder#encode(java.nio.CharBuffer)} method directly.

jtulach@1334: * jtulach@1334: * @param cb The char buffer to be encoded jtulach@1334: * jtulach@1334: * @return A byte buffer containing the encoded characters jtulach@1334: */ jaroslav@1343: // public final ByteBuffer encode(CharBuffer cb) { jaroslav@1343: // try { jaroslav@1343: // return ThreadLocalCoders.encoderFor(this) jaroslav@1343: // .onMalformedInput(CodingErrorAction.REPLACE) jaroslav@1343: // .onUnmappableCharacter(CodingErrorAction.REPLACE) jaroslav@1343: // .encode(cb); jaroslav@1343: // } catch (CharacterCodingException x) { jaroslav@1343: // throw new Error(x); // Can't happen jaroslav@1343: // } jaroslav@1343: // } jtulach@1334: jtulach@1334: /** jtulach@1334: * Convenience method that encodes a string into bytes in this charset. jtulach@1334: * jtulach@1334: *

An invocation of this method upon a charset cs returns the jtulach@1334: * same result as the expression jtulach@1334: * jtulach@1334: *

jtulach@1334:      *     cs.encode(CharBuffer.wrap(s)); 
jtulach@1334: * jtulach@1334: * @param str The string to be encoded jtulach@1334: * jtulach@1334: * @return A byte buffer containing the encoded characters jtulach@1334: */ jaroslav@1343: // public final ByteBuffer encode(String str) { jaroslav@1343: // return encode(CharBuffer.wrap(str)); jaroslav@1343: // } jtulach@1334: jtulach@1334: /** jtulach@1334: * Compares this charset to another. jtulach@1334: * jtulach@1334: *

Charsets are ordered by their canonical names, without regard to jtulach@1334: * case.

jtulach@1334: * jtulach@1334: * @param that jtulach@1334: * The charset to which this charset is to be compared jtulach@1334: * jtulach@1334: * @return A negative integer, zero, or a positive integer as this charset jtulach@1334: * is less than, equal to, or greater than the specified charset jtulach@1334: */ jtulach@1334: public final int compareTo(Charset that) { jtulach@1334: return (name().compareToIgnoreCase(that.name())); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Computes a hashcode for this charset.

jtulach@1334: * jtulach@1334: * @return An integer hashcode jtulach@1334: */ jtulach@1334: public final int hashCode() { jtulach@1334: return name().hashCode(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Tells whether or not this object is equal to another. jtulach@1334: * jtulach@1334: *

Two charsets are equal if, and only if, they have the same canonical jtulach@1334: * names. A charset is never equal to any other type of object.

jtulach@1334: * jtulach@1334: * @return true if, and only if, this charset is equal to the jtulach@1334: * given object jtulach@1334: */ jtulach@1334: public final boolean equals(Object ob) { jtulach@1334: if (!(ob instanceof Charset)) jtulach@1334: return false; jtulach@1334: if (this == ob) jtulach@1334: return true; jtulach@1334: return name.equals(((Charset)ob).name()); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns a string describing this charset.

jtulach@1334: * jtulach@1334: * @return A string describing this charset jtulach@1334: */ jtulach@1334: public final String toString() { jtulach@1334: return name(); jtulach@1334: } jtulach@1334: jtulach@1334: }