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
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{
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;
}
--- /dev/null
+/*
+ * 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";
+
+}
--- /dev/null
+# 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