From: markt Date: Mon, 27 Jun 2011 13:39:13 +0000 (+0000) Subject: Pre-populate Charset cache. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=c9309a4fc6a07e3a1b859ee832a68fbbefdd4dbd;p=tomcat7.0 Pre-populate Charset cache. Since cache is pre-populated, no need to look up non-matching values which effectively caches misses too. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1140156 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/tomcat/util/buf/B2CConverter.java b/java/org/apache/tomcat/util/buf/B2CConverter.java index 542f02cbc..433899405 100644 --- a/java/org/apache/tomcat/util/buf/B2CConverter.java +++ b/java/org/apache/tomcat/util/buf/B2CConverter.java @@ -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 encodingToCharsetCache = new ConcurrentHashMap(); + static { + for (Entry 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; } diff --git a/java/org/apache/tomcat/util/buf/Constants.java b/java/org/apache/tomcat/util/buf/Constants.java new file mode 100644 index 000000000..7f9fc1f61 --- /dev/null +++ b/java/org/apache/tomcat/util/buf/Constants.java @@ -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"; + +} diff --git a/java/org/apache/tomcat/util/buf/LocalStrings.properties b/java/org/apache/tomcat/util/buf/LocalStrings.properties new file mode 100644 index 000000000..d06277c52 --- /dev/null +++ b/java/org/apache/tomcat/util/buf/LocalStrings.properties @@ -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