From e75a17ddd51dabc7b289e10e89f84dd7ad12df30 Mon Sep 17 00:00:00 2001 From: markt Date: Tue, 5 Jul 2011 16:45:38 +0000 Subject: [PATCH] =?utf8?q?Fix=20https://issues.apache.org/bugzilla/show=5F?= =?utf8?q?bug.cgi=3Fid=3D51475=20Handle=20messages=20larger=20than=20the?= =?utf8?q?=20buffer=20size.=20Expand=20test=20cases=20to=20cover=20this.?= =?utf8?q?=20Based=20on=20a=20patch=20by=20Christian=20St=C3=B6ber.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1143134 13f79535-47bb-0310-9956-ffa450edef68 --- .../tribes/group/interceptors/GzipInterceptor.java | 16 +++++----- .../group/interceptors/TestGzipInterceptor.java | 34 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 10 deletions(-) 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)); -- 2.11.0