Methods Summary |
---|
public final java.util.Set | aliases()Returns a set containing this charset's 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;
|
static boolean | atBugLevel(java.lang.String bl)
// package-private
if (bugLevel == null) {
if (!sun.misc.VM.isBooted())
return false;
java.security.PrivilegedAction pa =
new GetPropertyAction("sun.nio.cs.bugLevel");
bugLevel = (String)AccessController.doPrivileged(pa);
if (bugLevel == null)
bugLevel = "";
}
return (bugLevel != null) && bugLevel.equals(bl);
|
public static java.util.SortedMap | availableCharsets()Constructs a sorted map from canonical charset names to charset objects.
The map returned by this method will have one entry for each charset
for which support is available in the current Java virtual machine. If
two or more supported charsets have the same canonical name then the
resulting map will contain just one of them; which one it will contain
is not specified.
The invocation of this method, and the subsequent use of the
resulting map, may cause time-consuming disk or network I/O operations
to occur. This method is provided for applications that need to
enumerate all of the available charsets, for example to allow user
charset selection. This method is not used by the {@link #forName
forName} method, which instead employs an efficient incremental lookup
algorithm.
This method may return different results at different times if new
charset providers are dynamically made available to the current Java
virtual machine. In the absence of such changes, the charsets returned
by this method are exactly those that can be retrieved via the {@link
#forName forName} method.
return (SortedMap)AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
TreeMap m = new TreeMap(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
put(standardProvider.charsets(), m);
for (Iterator i = providers(); i.hasNext();) {
CharsetProvider cp = (CharsetProvider)i.next();
put(cp.charsets(), m);
}
return Collections.unmodifiableSortedMap(m);
}
});
|
private static void | cache(java.lang.String charsetName, java.nio.charset.Charset cs) // "Level 2" cache
cache2 = cache1;
cache1 = new Object[] { charsetName, cs };
|
public boolean | canEncode()Tells whether or not this charset supports encoding.
Nearly all charsets support encoding. The primary exceptions are
special-purpose auto-detect 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
false.
return true;
|
private static void | checkName(java.lang.String s)Checks that the given string is a legal charset name.
int n = s.length();
if (!atBugLevel("1.4")) {
if (n == 0)
throw new IllegalCharsetNameException(s);
}
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
if (c >= 'A" && c <= 'Z") continue;
if (c >= 'a" && c <= 'z") continue;
if (c >= '0" && c <= '9") continue;
if (c == '-") continue;
if (c == ':") continue;
if (c == '_") continue;
if (c == '.") continue;
throw new IllegalCharsetNameException(s);
}
|
public final int | compareTo(java.nio.charset.Charset that)Compares this charset to another.
Charsets are ordered by their canonical names, without regard to
case.
return (name().compareToIgnoreCase(that.name()));
|
public abstract boolean | contains(java.nio.charset.Charset cs)Tells whether or not this charset contains the given charset.
A charset C is said to contain a charset D if,
and only if, every character representable in D is also
representable in C. If this relationship holds then it is
guaranteed that every string that can be encoded in D can also be
encoded in C without performing any replacements.
That C contains D does not imply that each character
representable in C by a particular byte sequence is represented
in D by the same byte sequence, although sometimes this is the
case.
Every charset contains itself.
This method computes an approximation of the containment relation:
If it returns true then the given charset is known to be
contained by this charset; if it returns false, however, then
it is not necessarily the case that the given charset is not contained
in this charset.
|
public final java.nio.CharBuffer | decode(java.nio.ByteBuffer bb)Convenience method that decodes bytes in this charset into Unicode
characters.
An invocation of this method upon a charset cs returns the
same result as the expression
cs.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.decode(bb);
except that it is potentially more efficient because it can cache
decoders between successive invocations.
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.
try {
return ThreadLocalCoders.decoderFor(this)
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.decode(bb);
} catch (CharacterCodingException x) {
throw new Error(x); // Can't happen
}
|
public static java.nio.charset.Charset | defaultCharset()Returns the default charset of this Java virtual machine.
The default charset is determined during virtual-machine startup and
typically depends upon the locale and charset of the underlying
operating system.
synchronized (Charset.class) {
if (defaultCharset == null) {
java.security.PrivilegedAction pa =
new GetPropertyAction("file.encoding");
String csn = (String)AccessController.doPrivileged(pa);
Charset cs = lookup(csn);
if (cs != null)
return cs;
return forName("UTF-8");
}
return defaultCharset;
}
|
public java.lang.String | displayName()Returns this charset's human-readable name for the default locale.
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.
return name;
|
public java.lang.String | displayName(java.util.Locale locale)Returns this charset's human-readable name for the given locale.
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.
return name;
|
public final java.nio.ByteBuffer | encode(java.nio.CharBuffer cb)Convenience method that encodes Unicode characters into bytes in this
charset.
An invocation of this method upon a charset cs returns the
same result as the expression
cs.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.encode(bb);
except that it is potentially more efficient because it can cache
encoders between successive invocations.
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.
try {
return ThreadLocalCoders.encoderFor(this)
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.encode(cb);
} catch (CharacterCodingException x) {
throw new Error(x); // Can't happen
}
|
public final java.nio.ByteBuffer | encode(java.lang.String str)Convenience method that encodes a string into bytes in this charset.
An invocation of this method upon a charset cs returns the
same result as the expression
cs.encode(CharBuffer.wrap(s));
return encode(CharBuffer.wrap(str));
|
public final boolean | equals(java.lang.Object ob)Tells whether or not this object is equal to another.
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.
if (!(ob instanceof Charset))
return false;
if (this == ob)
return true;
return name.equals(((Charset)ob).name());
|
public static java.nio.charset.Charset | forName(java.lang.String charsetName)Returns a charset object for the named charset.
Charset cs = lookup(charsetName);
if (cs != null)
return cs;
throw new UnsupportedCharsetException(charsetName);
|
public final int | hashCode()Computes a hashcode for this charset.
return name().hashCode();
|
public final boolean | isRegistered()Tells whether or not this charset is registered in the IANA Charset
Registry.
return !name.startsWith("X-") && !name.startsWith("x-");
|
public static boolean | isSupported(java.lang.String charsetName)Tells whether the named charset is supported.
return (lookup(charsetName) != null);
|
private static java.nio.charset.Charset | lookup(java.lang.String charsetName)
if (charsetName == null)
throw new IllegalArgumentException("Null charset name");
Object[] a;
if ((a = cache1) != null && charsetName.equals(a[0]))
return (Charset)a[1];
// We expect most programs to use one Charset repeatedly.
// We convey a hint to this effect to the VM by putting the
// level 1 cache miss code in a separate method.
return lookup2(charsetName);
|
private static java.nio.charset.Charset | lookup2(java.lang.String charsetName)
Object[] a;
if ((a = cache2) != null && charsetName.equals(a[0])) {
cache2 = cache1;
cache1 = a;
return (Charset)a[1];
}
Charset cs;
if ((cs = standardProvider.charsetForName(charsetName)) != null ||
(cs = lookupExtendedCharset(charsetName)) != null ||
(cs = lookupViaProviders(charsetName)) != null)
{
cache(charsetName, cs);
return cs;
}
/* Only need to check the name if we didn't find a charset for it */
checkName(charsetName);
return null;
|
private static java.nio.charset.Charset | lookupExtendedCharset(java.lang.String charsetName)
CharsetProvider ecp = null;
synchronized (extendedProviderLock) {
if (!extendedProviderProbed) {
probeExtendedProvider();
extendedProviderProbed = true;
}
ecp = extendedProvider;
}
return (ecp != null) ? ecp.charsetForName(charsetName) : null;
|
private static java.nio.charset.Charset | lookupViaProviders(java.lang.String charsetName)
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return (Charset)AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
for (Iterator i = providers(); i.hasNext();) {
CharsetProvider cp = (CharsetProvider)i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
|
public final java.lang.String | name()Returns this charset's canonical name.
return name;
|
public abstract java.nio.charset.CharsetDecoder | newDecoder()Constructs a new decoder for this charset.
|
public abstract java.nio.charset.CharsetEncoder | newEncoder()Constructs a new encoder for this charset.
|
private static void | probeExtendedProvider()
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
Class epc
= Class.forName("sun.nio.cs.ext.ExtendedCharsets");
extendedProvider = (CharsetProvider)epc.newInstance();
} catch (ClassNotFoundException x) {
// Extended charsets not available
// (charsets.jar not present)
} catch (InstantiationException x) {
throw new Error(x);
} catch (IllegalAccessException x) {
throw new Error(x);
}
return null;
}
});
|
private static java.util.Iterator | providers()
return new Iterator() {
Class c = java.nio.charset.spi.CharsetProvider.class;
ClassLoader cl = ClassLoader.getSystemClassLoader();
Iterator i = Service.providers(c, cl);
Object next = null;
private boolean getNext() {
while (next == null) {
try {
if (!i.hasNext())
return false;
next = i.next();
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore security exceptions
continue;
}
throw sce;
}
}
return true;
}
public boolean hasNext() {
return getNext();
}
public Object next() {
if (!getNext())
throw new NoSuchElementException();
Object n = next;
next = null;
return n;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
|
private static void | put(java.util.Iterator i, java.util.Map m)
while (i.hasNext()) {
Charset cs = (Charset)i.next();
if (!m.containsKey(cs.name()))
m.put(cs.name(), cs);
}
|
public final java.lang.String | toString()Returns a string describing this charset.
return name();
|