Author: markt Date: Thu Feb 28 23:30:14 2013 New Revision: 1451434 URL: http://svn.apache.org/r1451434 Log: Add option to converter so client can indicate no more input is available Add some more tests for BZ54602 TODO: Review the use of the now deprecated code
Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.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=1451434&r1=1451433&r2=1451434&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/buf/B2CConverter.java Thu Feb 28 23:30:14 2013 @@ -125,8 +125,22 @@ public class B2CConverter { * * @param bc byte input * @param cc char output + * + * @deprecated Use {@link #convert(ByteChunk, CharChunk, boolean) + */ + @Deprecated + public void convert(ByteChunk bc, CharChunk cc) throws IOException { + convert(bc, cc, false); + } + + /** + * Convert the given bytes to characters. + * + * @param bc byte input + * @param cc char output + * @param endOfInput Is this all of the available data */ - public void convert(ByteChunk bc, CharChunk cc) + public void convert(ByteChunk bc, CharChunk cc, boolean endOfInput) throws IOException { if ((bb == null) || (bb.array() != bc.getBuffer())) { // Create a new byte buffer if anything changed @@ -153,7 +167,7 @@ public class B2CConverter { do { leftovers.put(bc.substractB()); leftovers.flip(); - result = decoder.decode(leftovers, cb, false); + result = decoder.decode(leftovers, cb, endOfInput); leftovers.position(leftovers.limit()); leftovers.limit(leftovers.array().length); } while (result.isUnderflow() && (cb.position() == pos)); @@ -165,7 +179,7 @@ public class B2CConverter { } // Do the decoding and get the results into the byte chunk and the char // chunk - result = decoder.decode(bb, cb, false); + result = decoder.decode(bb, cb, endOfInput); if (result.isError() || result.isMalformed()) { result.throwException(); } else if (result.isOverflow()) { Modified: 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=1451434&r1=1451433&r2=1451434&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java (original) +++ tomcat/trunk/test/org/apache/tomcat/util/buf/TestB2CConverter.java Thu Feb 28 23:30:14 2013 @@ -17,6 +17,7 @@ package org.apache.tomcat.util.buf; import java.nio.charset.Charset; +import java.nio.charset.MalformedInputException; import org.junit.Assert; import org.junit.Test; @@ -26,6 +27,10 @@ public class TestB2CConverter { private static final byte[] UTF16_MESSAGE = new byte[] {-2, -1, 0, 65, 0, 66, 0, 67}; + private static final byte[] UTF8_INVALID = new byte[] {-8, -69, -73, -77}; + + private static final byte[] UTF8_PARTIAL = new byte[] {-50}; + @Test public void testSingleMessage() throws Exception { testMessages(1); @@ -50,7 +55,7 @@ public class TestB2CConverter { for (int i = 0; i < msgCount; i++) { bc.append(UTF16_MESSAGE, 0, UTF16_MESSAGE.length); - conv.convert(bc, cc); + conv.convert(bc, cc, true); Assert.assertEquals("ABC", cc.toString()); bc.recycle(); cc.recycle(); @@ -83,4 +88,52 @@ public class TestB2CConverter { Assert.assertTrue("Limit needs to be at least " + maxLeftover, maxLeftover <= B2CConverter.LEFTOVER_SIZE); } + + // TODO Work-around bug in UTF8 decoder + //@Test(expected=MalformedInputException.class) + public void testBug54602a() throws Exception { + // Check invalid input is rejected straight away + B2CConverter conv = new B2CConverter("UTF-8"); + ByteChunk bc = new ByteChunk(); + CharChunk cc = new CharChunk(); + + bc.append(UTF8_INVALID, 0, UTF8_INVALID.length); + cc.allocate(bc.getLength(), -1); + + conv.convert(bc, cc, false); + } + + @Test(expected=MalformedInputException.class) + public void testBug54602b() throws Exception { + // Check partial input is rejected + B2CConverter conv = new B2CConverter("UTF-8"); + ByteChunk bc = new ByteChunk(); + CharChunk cc = new CharChunk(); + + bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length); + cc.allocate(bc.getLength(), -1); + + conv.convert(bc, cc, true); + } + + @Test + public void testBug54602c() throws Exception { + // Check partial input is rejected once it is known to be all available + B2CConverter conv = new B2CConverter("UTF-8"); + ByteChunk bc = new ByteChunk(); + CharChunk cc = new CharChunk(); + + bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length); + cc.allocate(bc.getLength(), -1); + + conv.convert(bc, cc, false); + + Exception e = null; + try { + conv.convert(bc, cc, true); + } catch (MalformedInputException mie) { + e = mie; + } + Assert.assertNotNull(e); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org