classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Short-circuit default provider when possible


From: Mark Wielaard
Subject: [cp-patches] Short-circuit default provider when possible
Date: Wed, 31 Aug 2005 18:53:43 +0200

Hi,

On Wed, 2005-08-31 at 17:54 +0200, Mark Wielaard wrote:
> I'll see if I can come up with a good/real workaround.
> The issue is that since we are using the UTF-8 encoder now in zip
> reading Charset tries to lookup additional providers during bootstrap
> which tries to reopen the zip file we are just reading...

While creating the system class loader we were opening zip files which
needed a new Charset (for UTF-8) and while trying to get that we tried
to read in all the additional CharsetProviders we could find on the
system class path through the system class loader, which wasn't created
yet...

The following patch solves the issue by short-circuiting the default
CharsetProvider whenever possible. Additional providers are only read
when necessary. This means that we won't get into the circular behavior
described above as long as the bootstrap classes use one of the "normal"
character sets. And that the extra providers are never loaded when they
are not actually used.

2005-08-31  Mark Wielaard  <address@hidden>

    * java/nio/charset/Charset.java (charsetForName): Try default provider
    before trying to load extra providers.
    (availableCharsets): Explicitly add default provider Charsets.
    (providers2): Don't include default provider.

Apologies for noticing the problem yesterday while testing the zip
patches and then completely forgetting to actually solve to problem.

Committed,

Mark
Index: java/nio/charset/Charset.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/nio/charset/Charset.java,v
retrieving revision 1.23
diff -u -r1.23 Charset.java
--- java/nio/charset/Charset.java       2 Jul 2005 20:32:40 -0000       1.23
+++ java/nio/charset/Charset.java       31 Aug 2005 16:50:33 -0000
@@ -68,9 +68,9 @@
 {
   private CharsetEncoder cachedEncoder;
   private CharsetDecoder cachedDecoder;
- 
+  
   /**
-   * Charset providers.
+   * Extra Charset providers.
    */
   private static CharsetProvider[] providers;
   
@@ -204,13 +204,19 @@
   private static Charset charsetForName(String charsetName)
   {
     checkName (charsetName);
-    Charset cs = null;
-    CharsetProvider[] providers = providers2();
-    for (int i = 0; i < providers.length; i++)
+    // Try the default provider first
+    // (so we don't need to load external providers unless really necessary)
+    // if it is an exotic charset try loading the external providers.
+    Charset cs = provider().charsetForName(charsetName);
+    if (cs == null)
       {
-        cs = providers[i].charsetForName(charsetName);
-        if (cs != null)
-         break;
+       CharsetProvider[] providers = providers2();
+       for (int i = 0; i < providers.length; i++)
+         {
+           cs = providers[i].charsetForName(charsetName);
+           if (cs != null)
+             break;
+         }
       }
     return cs;
   }
@@ -218,6 +224,11 @@
   public static SortedMap availableCharsets()
   {
     TreeMap charsets = new TreeMap(String.CASE_INSENSITIVE_ORDER);
+    for (Iterator i = provider().charsets(); i.hasNext(); )
+      {
+       Charset cs = (Charset) i.next();
+       charsets.put(cs.name(), cs);
+      }
 
     CharsetProvider[] providers = providers2();
     for (int j = 0; j < providers.length; j++)
@@ -246,7 +257,7 @@
   /**
    * We need to support multiple providers, reading them from
    * java.nio.charset.spi.CharsetProvider in the resource directory
-   * META-INF/services.
+   * META-INF/services. This returns the "extra" charset providers.
    */
   private static CharsetProvider[] providers2()
   {
@@ -257,7 +268,6 @@
             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

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]