Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51475
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 5 Jul 2011 16:45:38 +0000 (16:45 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 5 Jul 2011 16:45:38 +0000 (16:45 +0000)
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

java/org/apache/catalina/tribes/group/interceptors/GzipInterceptor.java
test/org/apache/catalina/tribes/group/interceptors/TestGzipInterceptor.java

index d34d539..dca44a1 100644 (file)
@@ -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();
     }
 }
index 0f0e132..e8e5616 100644 (file)
@@ -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));