You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2011/06/27 15:39:14 UTC

svn commit: r1140156 - in /tomcat/trunk/java/org/apache/tomcat/util/buf: B2CConverter.java Constants.java LocalStrings.properties

Author: markt
Date: Mon Jun 27 13:39:13 2011
New Revision: 1140156

URL: http://svn.apache.org/viewvc?rev=1140156&view=rev
Log:
Pre-populate Charset cache.
Since cache is pre-populated, no need to look up non-matching values which effectively caches misses too.

Added:
    tomcat/trunk/java/org/apache/tomcat/util/buf/Constants.java   (with props)
    tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties   (with props)
Modified:
    tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java?rev=1140156&r1=1140155&r2=1140156&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java Mon Jun 27 13:39:13 2011
@@ -23,11 +23,12 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
-import java.nio.charset.IllegalCharsetNameException;
-import java.nio.charset.UnsupportedCharsetException;
 import java.util.Locale;
+import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.tomcat.util.res.StringManager;
+
 /** Efficient conversion of bytes  to character .
  *  
  *  This uses the standard JDK mechanism - a reader - but provides mechanisms
@@ -45,9 +46,20 @@ public class B2CConverter {
     private static final org.apache.juli.logging.Log log=
         org.apache.juli.logging.LogFactory.getLog( B2CConverter.class );
     
+    private static final StringManager sm =
+        StringManager.getManager(Constants.Package);
+
     private static final ConcurrentHashMap<String, Charset> encodingToCharsetCache =
         new ConcurrentHashMap<String, Charset>();
 
+    static {
+        for (Entry<String,Charset> entry :
+                Charset.availableCharsets().entrySet()) {
+            encodingToCharsetCache.put(entry.getKey().toLowerCase(),
+                    entry.getValue());
+        }
+    }
+
     public static Charset getCharset(String enc)
             throws UnsupportedEncodingException{
 
@@ -55,21 +67,11 @@ public class B2CConverter {
         String lowerCaseEnc = enc.toLowerCase(Locale.US);
 
         Charset charset = encodingToCharsetCache.get(lowerCaseEnc);
+        
         if (charset == null) {
-            try {
-                charset = Charset.forName(enc);
-            } catch (IllegalCharsetNameException icne) {
-                UnsupportedEncodingException uee =
-                    new UnsupportedEncodingException();
-                uee.initCause(icne);
-                throw uee;
-            } catch (UnsupportedCharsetException uce) {
-                UnsupportedEncodingException uee =
-                    new UnsupportedEncodingException();
-                uee.initCause(uce);
-                throw uee;
-            }
-            encodingToCharsetCache.put(enc, charset);
+            // Pre-population of the cache means this must be invalid
+            throw new UnsupportedEncodingException(
+                    sm.getString("b2cConvertor.unknownEncoding", enc));
         }
         return charset;
     }

Added: tomcat/trunk/java/org/apache/tomcat/util/buf/Constants.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/Constants.java?rev=1140156&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/Constants.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/Constants.java Mon Jun 27 13:39:13 2011
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tomcat.util.buf;
+
+/**
+ * String constants for the file package.
+ */
+public final class Constants {
+
+    public static final String Package = "org.apache.tomcat.util.buf";
+
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/util/buf/Constants.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties?rev=1140156&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties (added)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties Mon Jun 27 13:39:13 2011
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+b2cConvertor.unknownEncoding=The character encoding [{0}] is not supported

Propchange: tomcat/trunk/java/org/apache/tomcat/util/buf/LocalStrings.properties
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org