summaryrefslogtreecommitdiffstats
path: root/nio_char/src/main
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-04-02 17:19:21 -0700
committerElliott Hughes <enh@google.com>2010-04-02 17:58:45 -0700
commitccb8b92211a3e87acaf6486c8d4423c2053b8b5e (patch)
tree5898c2d9793dcf05f83192c17183f09e13b8920a /nio_char/src/main
parent3604384c5f53c83383ce85f838901e46b0105e5e (diff)
downloadlibcore-ccb8b92211a3e87acaf6486c8d4423c2053b8b5e.zip
libcore-ccb8b92211a3e87acaf6486c8d4423c2053b8b5e.tar.gz
libcore-ccb8b92211a3e87acaf6486c8d4423c2053b8b5e.tar.bz2
More Charset/ICU cleanup.
I've been feeling guilty about leaving broken double-checked locking (missing the "volatile") in harmony's Charset code. A quick investigation showed that the method that it's intended to optimize is basically never called, and the RI's documentation explicitly says "don't call this; it's slow". So this patch fixes that. I've also improved our documentation. I've also deleted a bunch of dead code. I've also tidied up some dodgy native string handling. Change-Id: Iad69ebb3459d9cc4c4ff37b255d458b83fe40132
Diffstat (limited to 'nio_char/src/main')
-rw-r--r--nio_char/src/main/java/java/nio/charset/Charset.java53
1 files changed, 22 insertions, 31 deletions
diff --git a/nio_char/src/main/java/java/nio/charset/Charset.java b/nio_char/src/main/java/java/nio/charset/Charset.java
index 4b8849f..7135d00 100644
--- a/nio_char/src/main/java/java/nio/charset/Charset.java
+++ b/nio_char/src/main/java/java/nio/charset/Charset.java
@@ -17,6 +17,7 @@
package java.nio.charset;
+import com.ibm.icu4jni.charset.NativeConverter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -39,10 +40,6 @@ import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
-// BEGIN android-changed
-import com.ibm.icu4jni.charset.CharsetProviderICU;
-// END android-changed
-
/**
* A charset defines a mapping between a Unicode character sequence and a byte
* sequence. It facilitates the encoding from a Unicode character sequence into
@@ -94,13 +91,7 @@ public abstract class Charset implements Comparable<Charset> {
private static ClassLoader systemClassLoader;
- // built in provider instance, assuming thread-safe
- // BEGIN android-changed
- private static final CharsetProviderICU _builtInProvider = new CharsetProviderICU();
- // END android-changed
-
- // cached built in charsets
- private static SortedMap<String, Charset> _builtInCharsets = null;
+ private static SortedMap<String, Charset> cachedBuiltInCharsets;
private final String canonicalName;
@@ -299,29 +290,29 @@ public abstract class Charset implements Comparable<Charset> {
}
}
+ private static synchronized SortedMap<String, Charset> getCachedBuiltInCharsets() {
+ if (cachedBuiltInCharsets == null) {
+ cachedBuiltInCharsets = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER);
+ for (String charsetName : NativeConverter.getAvailableCharsetNames()) {
+ Charset charset = NativeConverter.charsetForName(charsetName);
+ cachedBuiltInCharsets.put(charset.name(), charset);
+ }
+ }
+ return cachedBuiltInCharsets;
+ }
+
/**
- * Gets a map of all available charsets supported by the runtime.
- * <p>
- * The returned map contains mappings from canonical names to corresponding
- * instances of <code>Charset</code>. The canonical names can be considered
- * as case-insensitive.
- *
- * @return an unmodifiable map of all available charsets supported by the
- * runtime
+ * Returns an immutable case-insensitive map from canonical names to {@code Charset} instances.
+ * If multiple charsets have the same canonical name, it is unspecified which is returned in
+ * the map. This method may be slow. If you know which charset you're looking for, use
+ * {@link #forName}.
+ * @return an immutable case-insensitive map from canonical names to {@code Charset} instances
*/
@SuppressWarnings("unchecked")
public static SortedMap<String, Charset> availableCharsets() {
- // Initialize the built-in charsets map cache if necessary
- if (_builtInCharsets == null) {
- synchronized (Charset.class) {
- if (_builtInCharsets == null) {
- _builtInCharsets = _builtInProvider.initAvailableCharsets();
- }
- }
- }
-
- // Start with the built-in charsets...
- SortedMap<String, Charset> charsets = new TreeMap<String, Charset>(_builtInCharsets);
+ // Start with a copy of the built-in charsets...
+ TreeMap<String, Charset> charsets = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER);
+ charsets.putAll(getCachedBuiltInCharsets());
// Add all charsets provided by charset providers...
ClassLoader contextClassLoader = getContextClassLoader();
@@ -467,7 +458,7 @@ public abstract class Charset implements Comparable<Charset> {
throw new IllegalArgumentException();
}
checkCharsetName(charsetName);
- cs = _builtInProvider.charsetForName(charsetName);
+ cs = NativeConverter.charsetForName(charsetName);
if (cs != null) {
cacheCharset(cs);
}