Author: kwall
Date: Tue Sep 20 12:47:49 2016
New Revision: 1761567

URL: http://svn.apache.org/viewvc?rev=1761567&view=rev
Log:
QPID-7408: Minor tidy-ups to implementation of GunzipOutputStream

Modified:
    
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/GunzipOutputStream.java
    
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/GunzipOutputStreamTest.java

Modified: 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/GunzipOutputStream.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/GunzipOutputStream.java?rev=1761567&r1=1761566&r2=1761567&view=diff
==============================================================================
--- 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/GunzipOutputStream.java
 (original)
+++ 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/GunzipOutputStream.java
 Tue Sep 20 12:47:49 2016
@@ -37,8 +37,8 @@ public class GunzipOutputStream extends
     private final GZIPHeader _header = new GZIPHeader();
     private final GZIPTrailer _trailer = new GZIPTrailer();
     private final byte[] _singleByteArray = new byte[1];
-    private StreamState _streamState = StreamState.HEADER_PARSING;
     private final CRC32 _crc;
+    private StreamState _streamState = StreamState.HEADER_PARSING;
 
     public GunzipOutputStream(final OutputStream targetOutputStream)
     {
@@ -49,49 +49,51 @@ public class GunzipOutputStream extends
     @Override
     public void write(final byte data[], final int offset, final int length) 
throws IOException
     {
-        ByteArrayInputStream bais = new ByteArrayInputStream(data, offset, 
length);
-        int b;
-        while ((b = bais.read()) != -1)
+        try(ByteArrayInputStream bais = new ByteArrayInputStream(data, offset, 
length))
         {
-            if (_streamState == StreamState.DONE)
-            {
-                // another member is coming
-                _streamState = StreamState.HEADER_PARSING;
-                _crc.reset();
-                _header.reset();
-                _trailer.reset();
-                inf.reset();
-            }
-
-            if (_streamState == StreamState.HEADER_PARSING)
+            int b;
+            while ((b = bais.read()) != -1)
             {
-                _header.headerByte(b);
-                if (_header.getState() == HeaderState.DONE)
+                if (_streamState == StreamState.DONE)
                 {
-                    _streamState = StreamState.INFLATING;
-                    continue;
+                    // another member is coming
+                    _streamState = StreamState.HEADER_PARSING;
+                    _crc.reset();
+                    _header.reset();
+                    _trailer.reset();
+                    inf.reset();
                 }
-            }
 
-            if (_streamState == StreamState.INFLATING)
-            {
-                _singleByteArray[0] = (byte) b;
-                super.write(_singleByteArray, 0, 1);
+                if (_streamState == StreamState.HEADER_PARSING)
+                {
+                    _header.headerByte(b);
+                    if (_header.getState() == HeaderState.DONE)
+                    {
+                        _streamState = StreamState.INFLATING;
+                        continue;
+                    }
+                }
 
-                if (inf.finished())
+                if (_streamState == StreamState.INFLATING)
                 {
-                    _streamState = StreamState.TRAILER_PARSING;
-                    continue;
+                    _singleByteArray[0] = (byte) b;
+                    super.write(_singleByteArray, 0, 1);
+
+                    if (inf.finished())
+                    {
+                        _streamState = StreamState.TRAILER_PARSING;
+                        continue;
+                    }
                 }
-            }
 
-            if (_streamState == StreamState.TRAILER_PARSING)
-            {
-                if (_trailer.trailerByte(b))
+                if (_streamState == StreamState.TRAILER_PARSING)
                 {
-                    _trailer.verify(_crc);
-                    _streamState = StreamState.DONE;
-                    continue;
+                    if (_trailer.trailerByte(b))
+                    {
+                        _trailer.verify(_crc);
+                        _streamState = StreamState.DONE;
+                        continue;
+                    }
                 }
             }
         }
@@ -267,7 +269,7 @@ public class GunzipOutputStream extends
             else
             {
                 throw new IOException(
-                        "Received bytes exceeds GZIP trailer length. Multipe 
memeber support is not implemented");
+                        String.format("Received too many GZIP trailer bytes. 
Expecting %d bytes.", TRAILER_SIZE));
             }
         }
 
@@ -275,12 +277,12 @@ public class GunzipOutputStream extends
         {
             try (DataInputStream in = new DataInputStream(new 
ByteArrayInputStream(_trailerBytes)))
             {
-                long crc32 = readLittleEndianInt(in);
+                long crc32 = readLittleEndianLong(in);
                 if (crc32 != crc.getValue())
                 {
                     throw new IOException("crc32 mismatch. Gzip-compressed 
data is corrupted");
                 }
-                long isize = readLittleEndianInt(in);
+                long isize = readLittleEndianLong(in);
                 if (isize != (inf.getBytesWritten() & SIZE_MASK))
                 {
                     throw new IOException("Uncompressed size mismatch. 
Gzip-compressed data is corrupted");
@@ -288,7 +290,7 @@ public class GunzipOutputStream extends
             }
         }
 
-        private long readLittleEndianInt(final DataInputStream inData) throws 
IOException
+        private long readLittleEndianLong(final DataInputStream inData) throws 
IOException
         {
             return inData.readUnsignedByte()
                    | (inData.readUnsignedByte() << 8)

Modified: 
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/GunzipOutputStreamTest.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/GunzipOutputStreamTest.java?rev=1761567&r1=1761566&r2=1761567&view=diff
==============================================================================
--- 
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/GunzipOutputStreamTest.java
 (original)
+++ 
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/GunzipOutputStreamTest.java
 Tue Sep 20 12:47:49 2016
@@ -20,13 +20,15 @@
 
 package org.apache.qpid.server.management.plugin;
 
+import static org.junit.Assert.assertArrayEquals;
+
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.zip.GZIPOutputStream;
+import java.nio.ByteBuffer;
 
 import javax.xml.bind.DatatypeConverter;
 
 import org.apache.qpid.test.utils.QpidTestCase;
+import org.apache.qpid.util.GZIPUtils;
 
 
 public class GunzipOutputStreamTest extends QpidTestCase
@@ -43,72 +45,49 @@ public class GunzipOutputStreamTest exte
     private static final String TEST_TEXT = "This is test";
     private static final String TEST_TEXT2 = "Another test text";
 
-    public void testDecompressing() throws IOException
+    public void testDecompressing() throws Exception
     {
-        String testText = generateTestText();
-        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-        GunzipOutputStream adapter = new GunzipOutputStream(outputStream);
+        final byte[] originalUncompressedInput = generateTestBytes();
+        final byte[] compressedBytes = 
GZIPUtils.compressBufferToArray(ByteBuffer.wrap(originalUncompressedInput));
 
-        compressAndDecompressWithAdapter(testText, adapter);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        GunzipOutputStream guos = new GunzipOutputStream(outputStream);
+        guos.write(compressedBytes);
+        guos.close();
 
-        assertEquals("Unexpected content", testText, new 
String(outputStream.toByteArray()));
+        assertArrayEquals("Unexpected content", originalUncompressedInput, 
outputStream.toByteArray());
     }
 
-    public void testDecompressingWithFileName() throws IOException
+    public void testDecompressingWithEmbeddedFileName() throws Exception
     {
         byte[] data = 
DatatypeConverter.parseBase64Binary(GZIP_CONTENT_WITH_EMBEDDED_FILE_NAME);
 
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-        GunzipOutputStream adapter = new GunzipOutputStream(outputStream);
-        adapter.write(data);
+        GunzipOutputStream guos = new GunzipOutputStream(outputStream);
+        guos.write(data);
+        guos.close();
 
         assertEquals("Unexpected content", TEST_TEXT, new 
String(outputStream.toByteArray()));
     }
 
-    public void testDecompressingMultipleMembers() throws IOException
+    public void testDecompressingMultipleMembers() throws Exception
     {
         byte[] data = 
DatatypeConverter.parseBase64Binary(GZIP_CONTENT_WITH_MULTIPLE_MEMBERS);
 
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-        GunzipOutputStream adapter = new GunzipOutputStream(outputStream);
+        GunzipOutputStream guos = new GunzipOutputStream(outputStream);
         for (int i = 0; i < data.length; i++)
         {
-            adapter.write(data[i]);
+            guos.write(data[i]);
         }
+        guos.close();
 
         StringBuilder expected = new StringBuilder(TEST_TEXT);
         expected.append(TEST_TEXT2);
         assertEquals("Unexpected content", expected.toString(), new 
String(outputStream.toByteArray()));
     }
 
-    private void compressAndDecompressWithAdapter(final String testText, final 
GunzipOutputStream adapter)
-            throws IOException
-    {
-        byte[] data = compress(testText);
-        byte[] buffer = new byte[256];
-        int remaining = data.length;
-        int written = 0;
-        while (remaining > 0)
-        {
-            int length = Math.min(remaining, buffer.length);
-            System.arraycopy(data, written, buffer, 0, length);
-            adapter.write(buffer, 0, length);
-            written += length;
-            remaining -= length;
-        }
-    }
-
-    private byte[] compress(final String testText) throws IOException
-    {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try (GZIPOutputStream zipStream = new GZIPOutputStream(baos))
-        {
-            zipStream.write(testText.getBytes());
-        }
-        return baos.toByteArray();
-    }
-
-    private String generateTestText()
+    private byte[] generateTestBytes()
     {
         StringBuilder sb = new StringBuilder();
         int i = 0;
@@ -124,6 +103,6 @@ public class GunzipOutputStreamTest exte
             }
             sb.append(" ").append(i++);
         }
-        return sb.toString();
+        return sb.toString().getBytes();
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to