Author: remy.maucherat(a)jboss.com
Date: 2011-06-28 10:03:06 -0400 (Tue, 28 Jun 2011)
New Revision: 1747
Added:
trunk/java/org/apache/tomcat/util/buf/EncodingToCharset.java
Modified:
trunk/java/org/apache/catalina/connector/InputBuffer.java
trunk/java/org/apache/catalina/connector/OutputBuffer.java
trunk/java/org/apache/catalina/connector/Request.java
trunk/java/org/apache/tomcat/util/buf/B2CConverter.java
trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
trunk/java/org/apache/tomcat/util/buf/C2BConverter.java
trunk/webapps/docs/changelog.xml
Log:
- Adapt the String to charset optimizations.
Modified: trunk/java/org/apache/catalina/connector/InputBuffer.java
===================================================================
--- trunk/java/org/apache/catalina/connector/InputBuffer.java 2011-06-24 09:46:17 UTC (rev
1746)
+++ trunk/java/org/apache/catalina/connector/InputBuffer.java 2011-06-28 14:03:06 UTC (rev
1747)
@@ -23,6 +23,7 @@
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
+import java.util.Locale;
import org.apache.catalina.security.SecurityUtil;
import org.apache.catalina.util.StringManager;
@@ -556,25 +557,25 @@
enc = coyoteRequest.getCharacterEncoding();
gotEnc = true;
- if (enc == null)
+ if (enc == null) {
enc = DEFAULT_ENCODING;
+ } else {
+ enc = enc.toUpperCase(Locale.US);
+ }
conv = encoders.get(enc);
if (conv == null) {
- if (SecurityUtil.isPackageProtectionEnabled()){
- try{
- conv = (B2CConverter)AccessController.doPrivileged(
- new PrivilegedExceptionAction(){
-
- public Object run() throws IOException{
+ if (SecurityUtil.isPackageProtectionEnabled()) {
+ try {
+ conv = (B2CConverter) AccessController
+ .doPrivileged(new
PrivilegedExceptionAction<B2CConverter>() {
+ public B2CConverter run() throws IOException {
return new B2CConverter(enc);
}
-
- }
- );
- }catch(PrivilegedActionException ex){
+ });
+ } catch (PrivilegedActionException ex) {
Exception e = ex.getException();
if (e instanceof IOException)
- throw (IOException)e;
+ throw (IOException) e;
}
} else {
conv = new B2CConverter(enc);
Modified: trunk/java/org/apache/catalina/connector/OutputBuffer.java
===================================================================
--- trunk/java/org/apache/catalina/connector/OutputBuffer.java 2011-06-24 09:46:17 UTC
(rev 1746)
+++ trunk/java/org/apache/catalina/connector/OutputBuffer.java 2011-06-28 14:03:06 UTC
(rev 1747)
@@ -24,6 +24,7 @@
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
+import java.util.Locale;
import org.apache.coyote.ActionCode;
import org.apache.coyote.Response;
@@ -555,26 +556,25 @@
enc = coyoteResponse.getCharacterEncoding();
gotEnc = true;
- if (enc == null)
+ if (enc == null) {
enc = DEFAULT_ENCODING;
- conv = (C2BConverter) encoders.get(enc);
+ } else {
+ enc = enc.toUpperCase(Locale.US);
+ }
+ conv = encoders.get(enc);
if (conv == null) {
-
if (Globals.IS_SECURITY_ENABLED){
- try{
- conv = (C2BConverter)AccessController.doPrivileged(
- new PrivilegedExceptionAction(){
-
- public Object run() throws IOException{
+ try {
+ conv = AccessController
+ .doPrivileged(new
PrivilegedExceptionAction<C2BConverter>() {
+ public C2BConverter run() throws IOException {
return new C2BConverter(enc);
}
-
- }
- );
- }catch(PrivilegedActionException ex){
+ });
+ } catch (PrivilegedActionException ex) {
Exception e = ex.getException();
if (e instanceof IOException)
- throw (IOException)e;
+ throw (IOException) e;
}
} else {
conv = new C2BConverter(enc);
Modified: trunk/java/org/apache/catalina/connector/Request.java
===================================================================
--- trunk/java/org/apache/catalina/connector/Request.java 2011-06-24 09:46:17 UTC (rev
1746)
+++ trunk/java/org/apache/catalina/connector/Request.java 2011-06-28 14:03:06 UTC (rev
1747)
@@ -111,6 +111,7 @@
import org.apache.coyote.ActionCode;
import org.apache.tomcat.util.buf.B2CConverter;
import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.buf.EncodingToCharset;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.buf.StringCache;
import org.apache.tomcat.util.http.Cookies;
@@ -171,6 +172,7 @@
// Ensure that classes are loaded for SM
new StringCache.ByteEntry();
new StringCache.CharEntry();
+ new EncodingToCharset();
}
public Request() {
@@ -1740,9 +1742,7 @@
return;
// Ensure that the specified encoding is valid
- byte buffer[] = new byte[1];
- buffer[0] = (byte) 'a';
- String dummy = new String(buffer, enc);
+ EncodingToCharset.toCharset(enc);
// Save the validated encoding
coyoteRequest.setCharacterEncoding(enc);
Modified: trunk/java/org/apache/tomcat/util/buf/B2CConverter.java
===================================================================
--- trunk/java/org/apache/tomcat/util/buf/B2CConverter.java 2011-06-24 09:46:17 UTC (rev
1746)
+++ trunk/java/org/apache/tomcat/util/buf/B2CConverter.java 2011-06-28 14:03:06 UTC (rev
1747)
@@ -22,7 +22,6 @@
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
-import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.UnsupportedCharsetException;
@@ -52,7 +51,7 @@
public B2CConverter(String charset)
throws IOException {
try {
- decoder = Charset.forName(charset).newDecoder();
+ decoder = EncodingToCharset.toCharset(charset).newDecoder();
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(charset);
}
Modified: trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
===================================================================
--- trunk/java/org/apache/tomcat/util/buf/ByteChunk.java 2011-06-24 09:46:17 UTC (rev
1746)
+++ trunk/java/org/apache/tomcat/util/buf/ByteChunk.java 2011-06-28 14:03:06 UTC (rev
1747)
@@ -511,7 +511,7 @@
String strValue=null;
try {
if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING;
- strValue = new String( buff, start, end-start, enc );
+ strValue = new String( buff, start, end-start,
EncodingToCharset.toCharset(enc) );
/*
Does not improve the speed too much on most systems,
it's safer to use the "clasical" new String().
Modified: trunk/java/org/apache/tomcat/util/buf/C2BConverter.java
===================================================================
--- trunk/java/org/apache/tomcat/util/buf/C2BConverter.java 2011-06-24 09:46:17 UTC (rev
1746)
+++ trunk/java/org/apache/tomcat/util/buf/C2BConverter.java 2011-06-28 14:03:06 UTC (rev
1747)
@@ -21,11 +21,10 @@
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
-import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
-import java.nio.charset.UnsupportedCharsetException;
import java.nio.charset.CodingErrorAction;
+import java.nio.charset.UnsupportedCharsetException;
/**
* NIO based character encoder.
@@ -47,7 +46,7 @@
public C2BConverter(String charset)
throws IOException {
try {
- encoder = Charset.forName(charset).newEncoder();
+ encoder = EncodingToCharset.toCharset(charset).newEncoder();
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException(charset);
Added: trunk/java/org/apache/tomcat/util/buf/EncodingToCharset.java
===================================================================
--- trunk/java/org/apache/tomcat/util/buf/EncodingToCharset.java
(rev 0)
+++ trunk/java/org/apache/tomcat/util/buf/EncodingToCharset.java 2011-06-28 14:03:06 UTC
(rev 1747)
@@ -0,0 +1,62 @@
+/*
+ * 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;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * Helper class for charset handling.
+ *
+ * @author Mark Thomas
+ * @author Remy Maucherat
+ */
+public class EncodingToCharset {
+
+ private static final Map<String, Charset> encodingToCharsetCache =
+ new HashMap<String, Charset>();
+
+ static {
+ for (Charset charset: Charset.availableCharsets().values()) {
+ encodingToCharsetCache.put(charset.name().toUpperCase(Locale.US), charset);
+ for (String alias : charset.aliases()) {
+ encodingToCharsetCache.put(alias.toUpperCase(Locale.US), charset);
+ }
+ }
+ }
+
+ public static Charset toCharset(String encoding)
+ throws UnsupportedEncodingException {
+ // Encoding names should all be ASCII
+ String enc = encoding.toUpperCase(Locale.US);
+
+ Charset charset = encodingToCharsetCache.get(enc);
+
+ if (charset == null) {
+ // Pre-population of the cache means this must be invalid
+ throw new UnsupportedEncodingException(encoding);
+ }
+ return charset;
+
+ }
+
+}
Modified: trunk/webapps/docs/changelog.xml
===================================================================
--- trunk/webapps/docs/changelog.xml 2011-06-24 09:46:17 UTC (rev 1746)
+++ trunk/webapps/docs/changelog.xml 2011-06-28 14:03:06 UTC (rev 1747)
@@ -16,6 +16,18 @@
<body>
+<section name="JBoss Web 7.0.0.CR5 (remm)">
+ <subsection name="Coyote">
+ <changelog>
+ <fix>
+ Improve byte[] to String conversion speed when running into a cache miss and
+ when requesting a reader/writer caused by encoding to charset conversion
+ bottleneck. (remm, markt)
+ </fix>
+ </changelog>
+ </subsection>
+</section>
+
<section name="JBoss Web 7.0.0.CR4 (remm)">
<subsection name="Catalina">
<changelog>