commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1661151 - in /commons/proper/compress/trunk/src: changes/ main/java/org/apache/commons/compress/compressors/bzip2/ test/java/org/apache/commons/compress/compressors/bzip2/
Date Fri, 20 Feb 2015 16:18:01 GMT
Author: bodewig
Date: Fri Feb 20 16:18:00 2015
New Revision: 1661151

URL: http://svn.apache.org/r1661151
Log:
return 0 from read when asked to read 0 bytes - COMPRESS-309

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1661151&r1=1661150&r2=1661151&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Fri Feb 20 16:18:00 2015
@@ -54,6 +54,11 @@ breaks backwards compatibility for code
 This also changes the superclass of ZCompressorInputStream.    
 ">
 
+      <action issue="COMPRESS-309" type="fix"
+              date="2015-02-20">
+        BZip2CompressorInputStream#read would return -1 when asked to
+        read 0 bytes.
+      </action>
       <action issue="COMPRESS-306" type="fix">
         ArchiveStreamFactory fails to pass on the encoding when creating some streams.
         * ArjArchiveInputStream

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java?rev=1661151&r1=1661150&r2=1661151&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java
Fri Feb 20 16:18:00 2015
@@ -165,6 +165,9 @@ public class BZip2CompressorInputStream
         if (this.in == null) {
             throw new IOException("stream closed");
         }
+        if (len == 0) {
+            return 0;
+        }
 
         final int hi = offs + len;
         int destOffs = offs;

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java?rev=1661151&r1=1661150&r2=1661151&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java
Fri Feb 20 16:18:00 2015
@@ -20,8 +20,11 @@ package org.apache.commons.compress.comp
 
 import static org.apache.commons.compress.AbstractTestCase.getFile;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
+import org.junit.Assert;
 import org.junit.Test;
 
 public class BZip2CompressorInputStreamTest {
@@ -36,4 +39,34 @@ public class BZip2CompressorInputStreamT
         }
     }
 
+    /**
+     * @see "https://issues.apache.org/jira/browse/COMPRESS-309"
+     */
+    @Test
+    public void readOfLength0ShouldReturn0() throws Exception {
+        // Create a big random piece of data
+        byte[] rawData = new byte[1048576];
+        for (int i=0; i < rawData.length; ++i) {
+            rawData[i] = (byte) Math.floor(Math.random()*256);
+        }
+
+        // Compress it
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos);
+        bzipOut.write(rawData);
+        bzipOut.flush();
+        bzipOut.close();
+        baos.flush();
+        baos.close();
+
+        // Try to read it back in
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais);
+        byte[] buffer = new byte[1024];
+        Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));
+        Assert.assertEquals(0, bzipIn.read(buffer, 1024, 0));
+        Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));
+        bzipIn.close();
+    }
+
 }



Mime
View raw message