From: markt Date: Tue, 5 Jul 2011 16:45:38 +0000 (+0000) Subject: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51475 X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=e75a17ddd51dabc7b289e10e89f84dd7ad12df30;p=tomcat7.0 Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51475 Handle messages larger than the buffer size. Expand test cases to cover this. Based on a patch by Christian Stöber. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1143134 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java b/java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java index d34d5394f..dca44a1f5 100644 --- a/java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java +++ b/java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java @@ -77,18 +77,20 @@ public class GzipInterceptor extends ChannelInterceptorBase { } /** - * TODO Fix to create an automatically growing buffer. - * @param data byte[] - * @return byte[] + * @param data Data to decompress + * @return Decompressed data * @throws IOException */ public static byte[] decompress(byte[] data) throws IOException { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayInputStream bin = new ByteArrayInputStream(data); GZIPInputStream gin = new GZIPInputStream(bin); byte[] tmp = new byte[DEFAULT_BUFFER_SIZE]; - int length = gin.read(tmp); - byte[] result = new byte[length]; - System.arraycopy(tmp,0,result,0,length); - return result; + int length = 0; + while (length > -1) { + bout.write(tmp, 0, length); + length = gin.read(tmp); + } + return bout.toByteArray(); } } diff --git a/test/org/apache/catalina/tribes/group/interceptors/TestGzipInterceptor.java b/test/org/apache/catalina/tribes/group/interceptors/TestGzipInterceptor.java index 0f0e13244..e8e561614 100644 --- a/test/org/apache/catalina/tribes/group/interceptors/TestGzipInterceptor.java +++ b/test/org/apache/catalina/tribes/group/interceptors/TestGzipInterceptor.java @@ -22,9 +22,37 @@ import junit.framework.TestCase; public class TestGzipInterceptor extends TestCase { - public void testBasic() throws Exception { - byte[] data = new byte[1024]; - Arrays.fill(data,(byte)1); + public void testSmallerThanBufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE / 2); + } + + public void testJustSmallerThanBufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE -1); + } + + public void testExactBufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE); + } + + public void testJustLargerThanBufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE + 1); + } + + public void testFactor2BufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE * 2); + } + + public void testFactor4BufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE * 4); + } + + public void testMuchLargerThanBufferSize() throws Exception { + doCompressDecompress(GzipInterceptor.DEFAULT_BUFFER_SIZE * 10 + 1000); + } + + private void doCompressDecompress(int size) throws Exception { + byte[] data = new byte[size]; + Arrays.fill(data, (byte)1); byte[] compress = GzipInterceptor.compress(data); byte[] result = GzipInterceptor.decompress(compress); assertTrue(Arrays.equals(data, result));