diff options
author | Elliott Hughes <enh@google.com> | 2010-03-26 11:30:00 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-03-26 13:24:10 -0700 |
commit | bcf7c66e617ad0c33bb320184bb2401def517342 (patch) | |
tree | d7bf28cd5bab40431ad4a5dc2fbb98cc4e099367 /icu/src/main/java | |
parent | a5cdf4f808ecb46f6f348fce5873f1691587d9ea (diff) | |
download | libcore-bcf7c66e617ad0c33bb320184bb2401def517342.zip libcore-bcf7c66e617ad0c33bb320184bb2401def517342.tar.gz libcore-bcf7c66e617ad0c33bb320184bb2401def517342.tar.bz2 |
Start cleaning up the Charset implementation.
This was going to be https://issues.apache.org/jira/browse/HARMONY-6461,
but I couldn't resist cleaning up some of the surrounding code, and ended
up cleaning up some of our native code too. In the course of the afternoon
I spent on this, I lost my conviction that the upstream change makes
sense, so I reverted that, leaving this change just pure cleanup.
(Note that the cleanup work is incomplete. This is an improvement, but
there's plenty left to do. I just don't want to get too distracted until
all the Java 6 changes are done.)
Change-Id: I56841db5f6c038bbf7942e83a148dca546519269
Diffstat (limited to 'icu/src/main/java')
3 files changed, 36 insertions, 131 deletions
diff --git a/icu/src/main/java/com/ibm/icu4jni/charset/CharsetICU.java b/icu/src/main/java/com/ibm/icu4jni/charset/CharsetICU.java index df6f7dc..48deb00 100644 --- a/icu/src/main/java/com/ibm/icu4jni/charset/CharsetICU.java +++ b/icu/src/main/java/com/ibm/icu4jni/charset/CharsetICU.java @@ -22,7 +22,7 @@ import java.util.Map; public final class CharsetICU extends Charset{ - private String icuCanonicalName; + private final String icuCanonicalName; /** * Constructor to create a the CharsetICU object * @param canonicalName the canonical name as a string @@ -30,23 +30,18 @@ public final class CharsetICU extends Charset{ * @stable ICU 2.4 */ protected CharsetICU(String canonicalName, String icuCanonName, String[] aliases) { - super(canonicalName,aliases); + super(canonicalName, aliases); icuCanonicalName = icuCanonName; - } /** * Returns a new decoder instance of this charset object * @return a new decoder object * @stable ICU 2.4 */ - public CharsetDecoder newDecoder(){ - // the arrays are locals and not - // instance variables since the - // methods on this class need to - // be thread safe + public CharsetDecoder newDecoder() { long converterHandle = NativeConverter.openConverter(icuCanonicalName); - return new CharsetDecoderICU(this,converterHandle); - }; + return new CharsetDecoderICU(this, converterHandle); + } // hardCoded list of replacement bytes private static final Map subByteMap = new HashMap(); @@ -125,5 +120,3 @@ public final class CharsetICU extends Charset{ } } } - - diff --git a/icu/src/main/java/com/ibm/icu4jni/charset/CharsetProviderICU.java b/icu/src/main/java/com/ibm/icu4jni/charset/CharsetProviderICU.java index 6f63479..0479223 100644 --- a/icu/src/main/java/com/ibm/icu4jni/charset/CharsetProviderICU.java +++ b/icu/src/main/java/com/ibm/icu4jni/charset/CharsetProviderICU.java @@ -11,108 +11,40 @@ package com.ibm.icu4jni.charset; import java.nio.charset.Charset; import java.nio.charset.spi.CharsetProvider; -import java.util.*; +import java.util.ArrayList; import java.util.Iterator; -// BEGIN android-removed -// import com.ibm.icu4jni.converters.NativeConverter; -// END android-removed +import java.util.SortedMap; +import java.util.TreeMap; -public final class CharsetProviderICU extends CharsetProvider{ - - /** - * Constructs a CharsetProviderICU object - * @stable ICU 2.4 - */ - public CharsetProviderICU(){ +public final class CharsetProviderICU extends CharsetProvider { + public CharsetProviderICU() { } - - /** - * Constructs a charset for the given charset name - * @param charsetName charset name - * @return charset objet for the given charset name - * @stable ICU 2.4 - */ - public final Charset charsetForName(String charsetName) { - // get the canonical name - String icuCanonicalName = NativeConverter.getICUCanonicalName(charsetName); - // create the converter object and return it - if(icuCanonicalName==null || icuCanonicalName.length()==0){ - // this would make the Charset API to throw - // unsupported encoding exception - return null; - } - - // BEGIN android-added - try{ - long cn = NativeConverter.openConverter(icuCanonicalName); - NativeConverter.closeConverter(cn); - }catch (RuntimeException re) { - // unsupported encoding. let the charset api throw an - // UnsupportedEncodingException - return null; - } - // END android-added - - return getCharset(icuCanonicalName); - } - private final Charset getCharset(String icuCanonicalName){ - String[] aliases = (String[])NativeConverter.getAliases(icuCanonicalName); - String canonicalName = NativeConverter.getJavaCanonicalName(icuCanonicalName); - return (new CharsetICU(canonicalName,icuCanonicalName, aliases)); + @Override + public Charset charsetForName(String charsetName) { + return NativeConverter.charsetForName(charsetName); } - /** - * Adds an entry to the given map whose key is the charset's - * canonical name and whose value is the charset itself. - * @param map a map to receive charset objects and names - * @stable ICU 2.4 - */ - public final void putCharsets(Map map) { - // Get the available converter canonical names and aliases - String[] charsets = NativeConverter.getAvailable(); - for(int i=0; i<charsets.length;i++){ - // store the charsets and aliases in a Map - if (!map.containsKey(charsets[i])){ - map.put(charsets[i], charsetForName(charsets[i])); - } - } - } - /** - * Class that implements the iterator for charsets - * @stable ICU 2.4 - */ - protected final class CharsetIterator implements Iterator{ - private String[] names; - private int currentIndex; - protected CharsetIterator(String[] strs){ - names = strs; - currentIndex=0; - } - public boolean hasNext(){ - return (currentIndex< names.length); - } - public Object next(){ - if(currentIndex<names.length){ - return charsetForName(names[currentIndex++]); - }else{ - throw new NoSuchElementException(); + + @Override + public Iterator<Charset> charsets() { + ArrayList<Charset> result = new ArrayList<Charset>(); + for (String charsetName : NativeConverter.getAvailable()) { + result.add(charsetForName(charsetName)); } - } - public void remove() { - throw new UnsupportedOperationException(); - } + return result.iterator(); } - /** - * Returns an iterator for the available charsets - * @return Iterator the charset name iterator - * @stable ICU 2.4 + * Implements Charset.availableCharsets. */ - public final Iterator charsets(){ - String[] charsets = NativeConverter.getAvailable(); - Iterator iter = new CharsetIterator(charsets); - return iter; + public SortedMap<String, Charset> initAvailableCharsets() { + SortedMap<String, Charset> result = + new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER); + for (String charset : NativeConverter.getAvailable()) { + if (!result.containsKey(charset)) { + result.put(charset, charsetForName(charset)); + } + } + return result; } - } diff --git a/icu/src/main/java/com/ibm/icu4jni/charset/NativeConverter.java b/icu/src/main/java/com/ibm/icu4jni/charset/NativeConverter.java index 9af63ee..eefe3d5 100644 --- a/icu/src/main/java/com/ibm/icu4jni/charset/NativeConverter.java +++ b/icu/src/main/java/com/ibm/icu4jni/charset/NativeConverter.java @@ -9,11 +9,13 @@ package com.ibm.icu4jni.charset; +import java.nio.charset.Charset; + /** * Class for accessing the underlying JNI methods * @internal ICU 2.4 */ -final class NativeConverter{ +final class NativeConverter { //Native methods @@ -335,7 +337,9 @@ final class NativeConverter{ * @internal ICU 2.4 */ public static final native String[] getAvailable(); - + + public static final native Charset charsetForName(String charsetName); + /** * Gets the number of aliases for a converter name * @param enc encoding name @@ -353,30 +357,6 @@ final class NativeConverter{ public static final native String[] getAliases(String enc); /** - * Gets the canonical name of the converter - * @param enc converter name - * @return canonical name of the converter - * @internal ICU 2.4 - */ - public static final native String getCanonicalName(String enc); - - /** - * Gets the canonical name of the converter as defined by Java - * @param enc converter name - * @return canonical name of the converter - * @internal ICU 3.4 - */ - public static final native String getICUCanonicalName(String enc); - - /** - * Gets the canonical name of the converter as defined by Java - * @param icuCanonicalName converter name - * @return canonical name of the converter - * @internal ICU 3.4 - */ - public static final native String getJavaCanonicalName(String icuCanonicalName); - - /** * Sets the callback to Unicode for ICU conveter. The default behaviour of ICU callback * is to call the specified callback function for both illegal and unmapped sequences. * @param converterHandle Adress of the converter object created by native code |