Methods Summary |
---|
private static void | addCharsets(java.nio.charset.spi.CharsetProvider cp, java.util.TreeMap charsets)
Iterator<Charset> it = cp.charsets();
while (it.hasNext()) {
Charset cs = it.next();
// Only new charsets will be added
if (!charsets.containsKey(cs.name())) {
charsets.put(cs.name(), cs);
}
}
|
public final java.util.Set | aliases()Gets the set of this charset's aliases.
return Collections.unmodifiableSet(this.aliasesSet);
|
public static java.util.SortedMap | availableCharsets()Gets a map of all available charsets supported by the runtime.
The returned map contains mappings from canonical names to corresponding
instances of Charset . The canonical names can be
considered as case-insensitive.
// Initialize the built-in charsets map cache if necessary
if (null == _builtInCharsets) {
synchronized (Charset.class) {
if (null == _builtInCharsets) {
_builtInCharsets = new TreeMap<String, Charset>(
IgnoreCaseComparator.getInstance());
_builtInProvider.putCharsets(_builtInCharsets);
}
}
}
// Add built-in charsets
TreeMap<String, Charset> charsets = (TreeMap<String, Charset>) _builtInCharsets
.clone();
// Collect all charsets provided by charset providers
ClassLoader contextClassLoader = getContextClassLoader();
Enumeration<URL> e = null;
try {
if (null != contextClassLoader) {
e = contextClassLoader
.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
} else {
getSystemClassLoader();
e = systemClassLoader
.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
}
// Examine each configuration file
while (e.hasMoreElements()) {
loadConfiguredCharsets(e.nextElement(), contextClassLoader,
charsets);
}
} catch (IOException ex) {
// Unexpected ClassLoader exception, ignore
}
return Collections.unmodifiableSortedMap(charsets);
|
private static void | cacheCharset(java.nio.charset.Charset cs)
cachedCharsetTable.put(cs.name(), cs);
Set<String> aliasesSet = cs.aliases();
if (null != aliasesSet) {
Iterator<String> iter = aliasesSet.iterator();
while (iter.hasNext()) {
String alias = iter.next();
cachedCharsetTable.put(alias, cs);
}
}
|
public boolean | canEncode()Returns true if this charset supports encoding, false otherwise.
return true;
|
private static void | checkCharsetName(java.lang.String name)
// An empty string is illegal charset name
if (name.length() == 0) {
throw new IllegalCharsetNameException(name);
}
// The first character must be a letter or a digit
// This is related to HARMONY-68 (won't fix)
// char first = name.charAt(0);
// if (!isLetter(first) && !isDigit(first)) {
// throw new IllegalCharsetNameException(name);
// }
// Check the remaining characters
int length = name.length();
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (!isLetter(c) && !isDigit(c) && !isSpecial(c)) {
throw new IllegalCharsetNameException(name);
}
}
|
public final int | compareTo(java.nio.charset.Charset charset)Compares this charset with the given charset. This comparation is
based on the case insensitive canonical names of the charsets.
return this.canonicalName.compareToIgnoreCase(charset.canonicalName);
|
public abstract boolean | contains(java.nio.charset.Charset charset)Determines whether this charset is a super set of the given charset.
|
public final java.nio.CharBuffer | decode(java.nio.ByteBuffer buffer)Decodes the content of the specified byte buffer and writes it to a
character buffer that is to be returned.
The default action in case of decoding errors is
CodingErrorAction.REPLACE .
CharsetDecoder d = getCachedCharsetDecoder(canonicalName);
try {
synchronized (d) {
return d.decode(buffer);
}
} catch (CharacterCodingException ex) {
throw new Error(ex.getMessage(), ex);
}
|
public static java.nio.charset.Charset | defaultCharset()Gets the system default charset from the virtual machine.
Charset defaultCharset = null;
String encoding = AccessController
.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return System.getProperty("file.encoding"); //$NON-NLS-1$
}
});
try {
defaultCharset = Charset.forName(encoding);
} catch (UnsupportedCharsetException e) {
defaultCharset = Charset.forName("UTF-8"); //$NON-NLS-1$
}
return defaultCharset;
|
public java.lang.String | displayName()Gets the name of this charset for the default locale.
This is the default implementation of this method which always returns
the canonical name of this charset. Subclasses overriding this Method
may return a display name that was localized.
return this.canonicalName;
|
public java.lang.String | displayName(java.util.Locale l)Gets the name of this charset for the specified locale.
This is the default implementation of this method which always returns
the canonical name of this charset. Subclasses overriding this Method
may return a display name that was localized.
return this.canonicalName;
|
public final synchronized java.nio.ByteBuffer | encode(java.nio.CharBuffer buffer)Encodes the content of the give character buffer and outputs to a byte
buffer that is to be returned.
The default action in case of encoding errors is
CodingErrorAction.REPLACE .
CharsetEncoder e = getCachedCharsetEncoder(canonicalName);
try {
synchronized (e) {
return e.encode(buffer);
}
} catch (CharacterCodingException ex) {
throw new Error(ex.getMessage(), ex);
}
|
public final java.nio.ByteBuffer | encode(java.lang.String s)Encodes a string and outputs to a byte buffer that is to be returned.
The default action in case of encoding errors is
CodingErrorAction.REPLACE .
return encode(CharBuffer.wrap(s));
|
public final boolean | equals(java.lang.Object obj)Determines whether this charset equals to the given object. They are
considered to be equal if they have the same canonical name.
if (obj instanceof Charset) {
Charset that = (Charset) obj;
return this.canonicalName.equals(that.canonicalName);
}
return false;
|
public static java.nio.charset.Charset | forName(java.lang.String charsetName)Gets a Charset instance for the specified charset name.
Charset c = forNameInternal(charsetName);
if (null == c) {
throw new UnsupportedCharsetException(charsetName);
}
return c;
|
private static java.nio.charset.Charset | forNameInternal(java.lang.String charsetName)
if (null == charsetName) {
throw new IllegalArgumentException();
}
checkCharsetName(charsetName);
synchronized (Charset.class) {
// Try to get Charset from cachedCharsetTable
Charset cs = getCachedCharset(charsetName);
if (null != cs) {
return cs;
}
// Try built-in charsets
cs = _builtInProvider.charsetForName(charsetName);
if (null != cs) {
cacheCharset(cs);
return cs;
}
// Collect all charsets provided by charset providers
ClassLoader contextClassLoader = getContextClassLoader();
Enumeration<URL> e = null;
try {
if (null != contextClassLoader) {
e = contextClassLoader
.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
} else {
getSystemClassLoader();
e = systemClassLoader
.getResources(PROVIDER_CONFIGURATION_FILE_NAME);
}
// Examine each configuration file
while (e.hasMoreElements()) {
cs = searchConfiguredCharsets(charsetName,
contextClassLoader, e.nextElement());
if (null != cs) {
cacheCharset(cs);
return cs;
}
}
} catch (IOException ex) {
// Unexpected ClassLoader exception, ignore
}
}
return null;
|
private static java.nio.charset.Charset | getCachedCharset(java.lang.String name)
return cachedCharsetTable.get(name);
|
private java.nio.charset.CharsetDecoder | getCachedCharsetDecoder(java.lang.String name)
synchronized (cachedCharsetDecoderTable) {
CharsetDecoder d = cachedCharsetDecoderTable
.get(name);
if (null == d) {
d = this.newDecoder();
d.onMalformedInput(CodingErrorAction.REPLACE);
d.onUnmappableCharacter(CodingErrorAction.REPLACE);
cachedCharsetDecoderTable.put(name, d);
}
return d;
}
|
private java.nio.charset.CharsetEncoder | getCachedCharsetEncoder(java.lang.String name)
synchronized (cachedCharsetEncoderTable) {
CharsetEncoder e = cachedCharsetEncoderTable
.get(name);
if (null == e) {
e = this.newEncoder();
e.onMalformedInput(CodingErrorAction.REPLACE);
e.onUnmappableCharacter(CodingErrorAction.REPLACE);
cachedCharsetEncoderTable.put(name, e);
}
return e;
}
|
private static java.lang.ClassLoader | getContextClassLoader()
final Thread t = Thread.currentThread();
return AccessController
.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return t.getContextClassLoader();
}
});
|
private static void | getSystemClassLoader()
if (null == systemClassLoader) {
systemClassLoader = AccessController
.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return ClassLoader.getSystemClassLoader();
}
});
}
|
public final int | hashCode()Gets the hash code of this charset.
return this.canonicalName.hashCode();
|
private static boolean | isDigit(char c)
return ('0" <= c && c <= '9");
|
private static boolean | isLetter(char c)
return ('a" <= c && c <= 'z") || ('A" <= c && c <= 'Z");
|
public final boolean | isRegistered()Indicates whether this charset is known to be registered in the IANA
Charset Registry.
return !canonicalName.startsWith("x-") //$NON-NLS-1$
&& !canonicalName.startsWith("X-"); //$NON-NLS-1$
|
private static boolean | isSpecial(char c)
return ('-" == c || '." == c || ':" == c || '_" == c);
|
public static boolean | isSupported(java.lang.String charsetName)Determines whether the specified charset is supported by this runtime.
Charset cs = forNameInternal(charsetName);
return (null != cs);
|
private static void | loadConfiguredCharsets(java.net.URL configFile, java.lang.ClassLoader contextClassLoader, java.util.TreeMap charsets)
BufferedReader reader = null;
try {
InputStream is = configFile.openStream();
// Read each line for charset provider class names
// BEGIN android-modified
reader = new BufferedReader(new InputStreamReader(is,
PROVIDER_CONFIGURATION_FILE_ENCODING), 8192);
// END android-modified
String providerClassName = reader.readLine();
while (null != providerClassName) {
providerClassName = trimClassName(providerClassName);
// Skip comments and blank lines
if (providerClassName.length() > 0) { // Non empty string
// Load the charset provider
Object cp = null;
try {
Class<?> c = Class.forName(providerClassName, true,
contextClassLoader);
cp = c.newInstance();
} catch (Exception ex) {
// try to use system classloader when context
// classloader failed to load config file.
try {
getSystemClassLoader();
Class<?> c = Class.forName(providerClassName, true,
systemClassLoader);
cp = c.newInstance();
} catch (Exception e) {
throw new Error(e.getMessage(), e);
}
}
// Put the charsets supported by this provider into the map
addCharsets((CharsetProvider) cp, charsets);
}
// Read the next line of the config file
providerClassName = reader.readLine();
}
} catch (IOException ex) {
// Can't read this configuration file, ignore
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException ex) {
// Ignore closing exception
}
}
|
public final java.lang.String | name()Gets the canonical name of this charset.
return this.canonicalName;
|
public abstract java.nio.charset.CharsetDecoder | newDecoder()Gets a new instance of a decoder for this charset.
|
public abstract java.nio.charset.CharsetEncoder | newEncoder()Gets a new instance of an encoder for this charset.
|
private static java.nio.charset.Charset | searchConfiguredCharsets(java.lang.String charsetName, java.lang.ClassLoader contextClassLoader, java.net.URL configFile)
BufferedReader reader = null;
try {
InputStream is = configFile.openStream();
// Read each line for charset provider class names
// BEGIN android-modified
reader = new BufferedReader(new InputStreamReader(is,
PROVIDER_CONFIGURATION_FILE_ENCODING), 8192);
// END android-modified
String providerClassName = reader.readLine();
while (null != providerClassName) {
providerClassName = trimClassName(providerClassName);
if (providerClassName.length() > 0) { // Non empty string
// Load the charset provider
Object cp = null;
try {
Class<?> c = Class.forName(providerClassName, true,
contextClassLoader);
cp = c.newInstance();
} catch (Exception ex) {
// try to use system classloader when context
// classloader failed to load config file.
try {
getSystemClassLoader();
Class<?> c = Class.forName(providerClassName, true,
systemClassLoader);
cp = c.newInstance();
} catch (SecurityException e) {
// BEGIN android-changed
// ignore
// END android-changed
} catch (Exception e) {
throw new Error(e.getMessage(), e);
}
}
// BEGIN android-changed
if (cp != null) {
// Try to get the desired charset from this provider
Charset cs = ((CharsetProvider) cp)
.charsetForName(charsetName);
if (null != cs) {
return cs;
}
}
// END android-changed
}
// Read the next line of the config file
providerClassName = reader.readLine();
}
return null;
} catch (IOException ex) {
// Can't read this configuration file
return null;
} finally {
try {
if (null != reader) {
reader.close();
}
} catch (IOException ex) {
// Ignore closing exception
}
}
|
public final java.lang.String | toString()Gets a string representation of this charset. Usually this contains the
canonical name of the charset.
return "Charset[" + this.canonicalName + "]"; //$NON-NLS-1$//$NON-NLS-2$
|
private static java.lang.String | trimClassName(java.lang.String name)
String trimedName = name;
int index = name.indexOf(PROVIDER_CONFIGURATION_FILE_COMMENT);
// Trim comments
if (index != -1) {
trimedName = name.substring(0, index);
}
return trimedName.trim();
|