}
/**
- * 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();
}
}
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));