Explorar o código

HADOOP-5213. Fix Null pointer exception caused when bzip2compression
was used and user closed a output stream without writing any data.
(Zheng Shao via dhruba)



git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.20@770551 13f79535-47bb-0310-9956-ffa450edef68

Dhruba Borthakur %!s(int64=16) %!d(string=hai) anos
pai
achega
5011f075a6

+ 4 - 0
CHANGES.txt

@@ -29,6 +29,10 @@ Release 0.20.1 - Unreleased
     HADOOP-5688. Fix HftpFileSystem checksum path construction. (Tsz Wo
     (Nicholas) Sze via cdouglas)
 
+    HADOOP-5213. Fix Null pointer exception caused when bzip2compression 
+    was used and user closed a output stream without writing any data.
+    (Zheng Shao via dhruba)
+
 Release 0.20.0 - 2009-04-15
 
   INCOMPATIBLE CHANGES

+ 12 - 0
src/core/org/apache/hadoop/io/compress/BZip2Codec.java

@@ -169,6 +169,12 @@ public class BZip2Codec implements
     }
 
     public void finish() throws IOException {
+      if (needsReset) {
+        // In the case that nothing is written to this stream, we still need to
+        // write out the header before closing, otherwise the stream won't be
+        // recognized by BZip2CompressionInputStream.
+        internalReset();
+      }
       this.output.finish();
       needsReset = true;
     }
@@ -202,6 +208,12 @@ public class BZip2Codec implements
     }
 
     public void close() throws IOException {
+      if (needsReset) {
+        // In the case that nothing is written to this stream, we still need to
+        // write out the header before closing, otherwise the stream won't be
+        // recognized by BZip2CompressionInputStream.
+        internalReset();
+      }
       this.output.flush();
       this.output.close();
       needsReset = true;

+ 8 - 4
src/test/org/apache/hadoop/io/compress/TestCodec.java

@@ -53,15 +53,18 @@ public class TestCodec extends TestCase {
   private int seed = new Random().nextInt();
   
   public void testDefaultCodec() throws IOException {
+    codecTest(conf, seed, 0, "org.apache.hadoop.io.compress.DefaultCodec");
     codecTest(conf, seed, count, "org.apache.hadoop.io.compress.DefaultCodec");
   }
   
   public void testGzipCodec() throws IOException {
+    codecTest(conf, seed, 0, "org.apache.hadoop.io.compress.GzipCodec");
     codecTest(conf, seed, count, "org.apache.hadoop.io.compress.GzipCodec");
   }
   
-  public void testBZip2Codec() throws IOException {    
-    codecTest(conf, seed, count, "org.apache.hadoop.io.compress.BZip2Codec");    
+  public void testBZip2Codec() throws IOException {
+    codecTest(conf, seed, 0, "org.apache.hadoop.io.compress.BZip2Codec");
+    codecTest(conf, seed, count, "org.apache.hadoop.io.compress.BZip2Codec");
   }
 
   private static void codecTest(Configuration conf, int seed, int count, 
@@ -154,8 +157,9 @@ public class TestCodec extends TestCase {
   
   public void testSequenceFileBZip2Codec() throws IOException, ClassNotFoundException, 
       InstantiationException, IllegalAccessException {
-    sequenceFileCodecTest(conf, 100, "org.apache.hadoop.io.compress.BZip2Codec", 100);    
-    sequenceFileCodecTest(conf, 200000, "org.apache.hadoop.io.compress.BZip2Codec", 1000000);    
+    sequenceFileCodecTest(conf, 0, "org.apache.hadoop.io.compress.BZip2Codec", 100);
+    sequenceFileCodecTest(conf, 100, "org.apache.hadoop.io.compress.BZip2Codec", 100);
+    sequenceFileCodecTest(conf, 200000, "org.apache.hadoop.io.compress.BZip2Codec", 1000000);
   }
   
   private static void sequenceFileCodecTest(Configuration conf, int lines,