📄 charset.java
字号:
protected Charset(String canonicalName, String[] aliases) { checkName(canonicalName); String[] as = (aliases == null) ? new String[0] : aliases; for (int i = 0; i < as.length; i++) checkName(as[i]); this.name = canonicalName; this.aliases = as; } /** * Returns this charset's canonical name. </p> * * @return The canonical name of this charset */ public final String name() { return name; } /** * Returns a set containing this charset's aliases. </p> * * @return An immutable set of this charset's aliases */ public final Set aliases() { if (aliasSet != null) return aliasSet; int n = aliases.length; HashSet hs = new HashSet(n); for (int i = 0; i < n; i++) hs.add(aliases[i]); aliasSet = Collections.unmodifiableSet(hs); return aliasSet; } /** * Returns this charset's human-readable name for the default locale. * * <p> The default implementation of this method simply returns this * charset's canonical name. Concrete subclasses of this class may * override this method in order to provide a localized display name. </p> * * @return The display name of this charset in the default locale */ public String displayName() { return name; } /** * Tells whether or not this charset is registered in the <a * href="http://www.iana.org/assignments/character-sets">IANA Charset * Registry</a>. </p> * * @return <tt>true</tt> if, and only if, this charset is known by its * implementor to be registered with the IANA */ public final boolean isRegistered() { return !name.startsWith("X-") && !name.startsWith("x-"); } /** * Returns this charset's human-readable name for the given locale. * * <p> The default implementation of this method simply returns this * charset's canonical name. Concrete subclasses of this class may * override this method in order to provide a localized display name. </p> * * @param locale * The locale for which the display name is to be retrieved * * @return The display name of this charset in the given locale */ public String displayName(Locale locale) { return name; } /** * Tells whether or not this charset contains the given charset. * * <p> A charset <i>C</i> is said to <i>contain</i> a charset <i>D</i> if, * and only if, every character representable in <i>D</i> is also * representable in <i>C</i>. If this relationship holds then it is * guaranteed that every string that can be encoded in <i>D</i> can also be * encoded in <i>C</i> without performing any replacements. * * <p> That <i>C</i> contains <i>D</i> does not imply that each character * representable in <i>C</i> by a particular byte sequence is represented * in <i>D</i> by the same byte sequence, although sometimes this is the * case. * * <p> Every charset contains itself. * * <p> This method computes an approximation of the containment relation: * If it returns <tt>true</tt> then the given charset is known to be * contained by this charset; if it returns <tt>false</tt>, however, then * it is not necessarily the case that the given charset is not contained * in this charset. * * @return <tt>true</tt> if, and only if, the given charset * is contained in this charset */ public abstract boolean contains(Charset cs); /** * Constructs a new decoder for this charset. </p> * * @return A new decoder for this charset */ public abstract CharsetDecoder newDecoder(); /** * Constructs a new encoder for this charset. </p> * * @return A new encoder for this charset * * @throws UnsupportedOperationException * If this charset does not support encoding */ public abstract CharsetEncoder newEncoder(); /** * Tells whether or not this charset supports encoding. * * <p> Nearly all charsets support encoding. The primary exceptions are * special-purpose <i>auto-detect</i> charsets whose decoders can determine * which of several possible encoding schemes is in use by examining the * input byte sequence. Such charsets do not support encoding because * there is no way to determine which encoding should be used on output. * Implementations of such charsets should override this method to return * <tt>false</tt>. </p> * * @return <tt>true</tt> if, and only if, this charset supports encoding */ public boolean canEncode() { return true; } /** * Convenience method that decodes bytes in this charset into Unicode * characters. * * <p> An invocation of this method upon a charset <tt>cs</tt> returns the * same result as the expression * * <pre> * cs.newDecoder() * .onMalformedInput(CodingErrorAction.REPLACE) * .onUnmappableCharacter(CodingErrorAction.REPLACE) * .decode(bb); </pre> * * except that it is potentially more efficient because it can cache * decoders between successive invocations. * * <p> This method always replaces malformed-input and unmappable-character * sequences with this charset's default replacement byte array. In order * to detect such sequences, use the {@link * CharsetDecoder#decode(java.nio.ByteBuffer)} method directly. </p> * * @param bb The byte buffer to be decoded * * @return A char buffer containing the decoded characters */ public final CharBuffer decode(ByteBuffer bb) { try { return ThreadLocalCoders.decoderFor(this) .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(bb); } catch (CharacterCodingException x) { throw new Error(x); // Can't happen } } /** * Convenience method that encodes Unicode characters into bytes in this * charset. * * <p> An invocation of this method upon a charset <tt>cs</tt> returns the * same result as the expression * * <pre> * cs.newEncoder() * .onMalformedInput(CodingErrorAction.REPLACE) * .onUnmappableCharacter(CodingErrorAction.REPLACE) * .encode(bb); </pre> * * except that it is potentially more efficient because it can cache * encoders between successive invocations. * * <p> This method always replaces malformed-input and unmappable-character * sequences with this charset's default replacement string. In order to * detect such sequences, use the {@link * CharsetEncoder#encode(java.nio.CharBuffer)} method directly. </p> * * @param cb The char buffer to be encoded * * @return A byte buffer containing the encoded characters */ public final ByteBuffer encode(CharBuffer cb) { try { return ThreadLocalCoders.encoderFor(this) .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .encode(cb); } catch (CharacterCodingException x) { throw new Error(x); // Can't happen } } /** * Convenience method that encodes a string into bytes in this charset. * * <p> An invocation of this method upon a charset <tt>cs</tt> returns the * same result as the expression * * <pre> * cs.encode(CharBuffer.wrap(s)); </pre> * * @param str The string to be encoded * * @return A byte buffer containing the encoded characters */ public final ByteBuffer encode(String str) { return encode(CharBuffer.wrap(str)); } /** * Compares this charset to another object. * * <p> Charsets are ordered by their canonical names, without regard to * case. </p> * * @param ob * The object to which this object is to be compared * * @return A negative integer, zero, or a positive integer as this charset * is less than, equal to, or greater than the specified object */ public final int compareTo(Object ob) { return (name().compareToIgnoreCase(((Charset)ob).name())); } /** * Computes a hashcode for this charset. </p> * * @return An integer hashcode */ public final int hashCode() { return name().hashCode(); } /** * Tells whether or not this object is equal to another. * * <p> Two charsets are equal if, and only if, they have the same canonical * names. A charset is never equal to any other type of object. </p> * * @return <tt>true</tt> if, and only if, this charset is equal to the * given object */ public final boolean equals(Object ob) { if (!(ob instanceof Charset)) return false; if (this == ob) return true; return name.equals(((Charset)ob).name()); } /** * Returns a string describing this charset. </p> * * @return A string describing this charset */ public final String toString() { return name(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -