From 5984f989561b06f42c2b9cd311caad8d7dd68400 Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Wed, 27 Apr 2005 07:30:36 +0000 Subject: [PATCH] [multiple changes] 2005-04-27 Michael Koch * java/nio/charset/Charset.java (providers2): Renamed from 'providers' to work around CNI limitation. 2005-04-27 Sven de Marothy * java/nio/charset/Charset.java: (defaultCharset()): New method. Status updated to 1.5 2005-04-27 Sven de Marothy * java/nio/charset/Charset.java: Cached encoders shouldn't be static. 2005-04-27 Sven de Marothy * java/nio/charset/Charset.java: Reset cached de/encoders. 2005-04-27 Robert Schuster * java/nio/charset/Charset.java (forName): Throws IllegalArgumentException when argument is null and added documentation. 2005-04-27 Ito Kazumitsu * java/nio/charset/Charset.java (providers): New method to make an array of CharsetProviders defined in META-INF/services/java.nio.charset.spi.CharsetProvider. (charsetForName, availableCharsets): Use the new method providers(). From-SVN: r98816 --- libjava/ChangeLog | 33 ++++++++ libjava/java/nio/charset/Charset.java | 106 ++++++++++++++++++++------ 2 files changed, 116 insertions(+), 23 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 8c3ab9bcb56..871eae93ba4 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,36 @@ +2005-04-27 Michael Koch + + * java/nio/charset/Charset.java (providers2): Renamed from 'providers' + to work around CNI limitation. + +2005-04-27 Sven de Marothy + + * java/nio/charset/Charset.java: + (defaultCharset()): New method. + Status updated to 1.5 + +2005-04-27 Sven de Marothy + + * java/nio/charset/Charset.java: Cached encoders shouldn't be static. + +2005-04-27 Sven de Marothy + + * java/nio/charset/Charset.java: Reset cached de/encoders. + +2005-04-27 Robert Schuster + + * java/nio/charset/Charset.java (forName): Throws + IllegalArgumentException when argument is null + and added documentation. + +2005-04-27 Ito Kazumitsu + + * java/nio/charset/Charset.java (providers): + New method to make an array of CharsetProviders defined in + META-INF/services/java.nio.charset.spi.CharsetProvider. + (charsetForName, availableCharsets): Use the + new method providers(). + 2005-04-26 Michael Koch * java/net/InetAddress.java: Made all hexadecimal numbers lowercase. diff --git a/libjava/java/nio/charset/Charset.java b/libjava/java/nio/charset/Charset.java index c7672c1c3df..703d0232fbc 100644 --- a/libjava/java/nio/charset/Charset.java +++ b/libjava/java/nio/charset/Charset.java @@ -40,12 +40,17 @@ package java.nio.charset; import gnu.java.nio.charset.Provider; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.spi.CharsetProvider; import java.util.Collections; +import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Locale; import java.util.Set; import java.util.SortedMap; @@ -57,18 +62,14 @@ import java.util.TreeMap; */ public abstract class Charset implements Comparable { - private static CharsetEncoder cachedEncoder; - private static CharsetDecoder cachedDecoder; + private CharsetEncoder cachedEncoder; + private CharsetDecoder cachedDecoder; - static - { - synchronized (Charset.class) - { - cachedEncoder = null; - cachedDecoder = null; - } - } - + /** + * Charset providers. + */ + private static CharsetProvider[] providers; + private final String canonicalName; private final String[] aliases; @@ -82,6 +83,8 @@ public abstract class Charset implements Comparable checkName (aliases[i]); } + cachedEncoder = null; + cachedDecoder = null; this.canonicalName = canonicalName; this.aliases = aliases; } @@ -138,8 +141,6 @@ public abstract class Charset implements Comparable Charset cs = charsetForName (charsetName); if (cs == null) throw new UnsupportedCharsetException (charsetName); - cachedDecoder = null; - cachedEncoder = null; return cs; } @@ -154,30 +155,88 @@ public abstract class Charset implements Comparable private static Charset charsetForName(String charsetName) { checkName (charsetName); - return provider ().charsetForName (charsetName); + Charset cs = null; + CharsetProvider[] providers = providers2(); + for (int i = 0; i < providers.length; i++) + { + cs = providers[i].charsetForName(charsetName); + if (cs != null) + break; + } + return cs; } public static SortedMap availableCharsets() { TreeMap charsets = new TreeMap(String.CASE_INSENSITIVE_ORDER); - for (Iterator i = provider ().charsets (); i.hasNext (); ) + CharsetProvider[] providers = providers2(); + for (int j = 0; j < providers.length; j++) { - Charset cs = (Charset) i.next (); - charsets.put (cs.name (), cs); + for (Iterator i = providers[j].charsets(); i.hasNext(); ) + { + Charset cs = (Charset) i.next(); + charsets.put(cs.name(), cs); + } } return Collections.unmodifiableSortedMap(charsets); } - // XXX: we need to support multiple providers, reading them from - // java.nio.charset.spi.CharsetProvider in the resource directory - // META-INF/services private static CharsetProvider provider() { + try { + String s = System.getProperty("charset.provider"); + if(s != null){ + CharsetProvider p = + (CharsetProvider) ((Class.forName(s)).newInstance()); + return p; + } + } catch(Exception e){} return Provider.provider(); } + /** + * We need to support multiple providers, reading them from + * java.nio.charset.spi.CharsetProvider in the resource directory + * META-INF/services. + */ + private static CharsetProvider[] providers2() + { + if (providers == null) + { + try + { + Enumeration en = ClassLoader.getSystemResources + ("META-INF/services/java.nio.charset.spi.CharsetProvider"); + LinkedHashSet set = new LinkedHashSet(); + set.add(provider()); + while (en.hasMoreElements()) + { + BufferedReader rdr = new BufferedReader(new InputStreamReader + (((URL) (en.nextElement())).openStream())); + while (true) + { + String s = rdr.readLine(); + if (s == null) + break; + CharsetProvider p = + (CharsetProvider) ((Class.forName(s)).newInstance()); + set.add(p); + } + } + + providers = new CharsetProvider[set.size()]; + set.toArray(providers); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + return providers; + } + public final String name () { return canonicalName; @@ -238,8 +297,8 @@ public abstract class Charset implements Comparable cachedEncoder = newEncoder () .onMalformedInput (CodingErrorAction.REPLACE) .onUnmappableCharacter (CodingErrorAction.REPLACE); - } - + } else + cachedEncoder.reset(); return cachedEncoder.encode (cb); } } @@ -269,7 +328,8 @@ public abstract class Charset implements Comparable cachedDecoder = newDecoder () .onMalformedInput (CodingErrorAction.REPLACE) .onUnmappableCharacter (CodingErrorAction.REPLACE); - } + } else + cachedDecoder.reset(); return cachedDecoder.decode (bb); }