Author: markt Date: Mon Jan 7 23:00:47 2013 New Revision: 1430079 URL: http://svn.apache.org/viewvc?rev=1430079&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=54248 Need to reset the decoder when the N2C converter is recycled to ensure BOMs are correctly handled for those encodings that require them.
Added: tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java (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=1430079&r1=1430078&r2=1430079&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java Mon Jan 7 23:00:47 2013 @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -102,6 +103,7 @@ public class B2CConverter { private IntermediateInputStream iis; private ReadConvertor conv; + private CharsetDecoder decoder; private final String encoding; /** Create a converter, with bytes going to a byte buffer @@ -119,6 +121,7 @@ public class B2CConverter { */ public void recycle() { conv.recycle(); + decoder.reset(); } static final int BUFFER_SIZE=8192; @@ -167,12 +170,11 @@ public class B2CConverter { } - public void reset() - throws IOException - { - // destroy the reader/iis - iis=new IntermediateInputStream(); - conv = new ReadConvertor(iis, getCharset(encoding)); + public void reset() throws IOException { + // Re-create the reader and iis + iis = new IntermediateInputStream(); + decoder = getCharset(encoding).newDecoder(); + conv = new ReadConvertor(iis, decoder); } } @@ -184,12 +186,12 @@ public class B2CConverter { /** * */ -final class ReadConvertor extends InputStreamReader { +final class ReadConvertor extends InputStreamReader { /** Create a converter. */ - public ReadConvertor(IntermediateInputStream in, Charset charset) { - super(in, charset); + public ReadConvertor(IntermediateInputStream in, CharsetDecoder decoder) { + super(in, decoder); } /** Overridden - will do nothing but reset internal state. Added: tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java?rev=1430079&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java (added) +++ tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java Mon Jan 7 23:00:47 2013 @@ -0,0 +1,61 @@ +/* + * 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 org.junit.Assert; +import org.junit.Test; + +public class TestB2CConverter { + + private static final byte[] UTF16_MESSAGE = + new byte[] {-2, -1, 0, 65, 0, 66, 0, 67}; + + @Test + public void testSingleMessage() throws Exception { + testMessages(1); + } + + @Test + public void testTwoMessage() throws Exception { + testMessages(2); + } + + @Test + public void testManyMessage() throws Exception { + testMessages(10); + } + + private void testMessages(int msgCount) throws Exception { + B2CConverter conv = new B2CConverter("UTF-16"); + + ByteChunk bc = new ByteChunk(); + CharChunk cc = new CharChunk(); + + + for (int i = 0; i < msgCount; i++) { + bc.append(UTF16_MESSAGE, 0, UTF16_MESSAGE.length); + // Note: The limit is the number of characters to read + conv.convert(bc, cc, 3); + Assert.assertEquals("ABC", cc.toString()); + bc.recycle(); + cc.recycle(); + conv.recycle(); + } + + System.out.println(cc); + } +} Propchange: tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org