Prechádzať zdrojové kódy

HADOOP-4706. Close the underlying output stream in IFileOutputStream::close. Contributed by Jothi Padmanabhan.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@725888 13f79535-47bb-0310-9956-ffa450edef68
Christopher Douglas 16 rokov pred
rodič
commit
1b37b6ef82

+ 3 - 0
CHANGES.txt

@@ -354,6 +354,9 @@ Trunk (unreleased changes)
     it's deprecated in favour of
     org.apache.hadoop.security.AccessControlException. (acmurthy) 
 
+    HADOOP-4706. Close the underlying output stream in
+    IFileOutputStream::close. (Jothi Padmanabhan via cdouglas)
+
 Release 0.19.1 - Unreleased
 
   IMPROVEMENTS

+ 15 - 9
src/mapred/org/apache/hadoop/mapred/IFile.java

@@ -131,22 +131,28 @@ class IFile {
       out.flush();
   
       if (compressOutput) {
-        // Flush & return the compressor
+        // Flush
         compressedOut.finish();
         compressedOut.resetState();
-        CodecPool.returnCompressor(compressor);
-        compressor = null;
       }
       
-      // Close the stream
-      checksumOut.close();
-      
-      compressedBytesWritten = rawOut.getPos() - start;
-    
       // Close the underlying stream iff we own it...
       if (ownOutputStream) {
-        rawOut.close();
+        out.close();
+      }
+      else {
+        // Write the checksum
+        checksumOut.finish();
+      }
+
+      compressedBytesWritten = rawOut.getPos() - start;
+
+      if (compressOutput) {
+        // Return back the compressor
+        CodecPool.returnCompressor(compressor);
+        compressor = null;
       }
+
       out = null;
       if(writtenRecordsCounter != null) {
         writtenRecordsCounter.increment(numRecordsWritten);

+ 16 - 1
src/mapred/org/apache/hadoop/mapred/IFileOutputStream.java

@@ -36,6 +36,7 @@ class IFileOutputStream extends FilterOutputStream {
   private final DataChecksum sum;
   private byte[] barray;
   private boolean closed = false;
+  private boolean finished = false;
 
   /**
    * Create a checksum output stream that writes
@@ -55,11 +56,25 @@ class IFileOutputStream extends FilterOutputStream {
       return;
     }
     closed = true;
+    finish();
+    out.close();
+  }
+
+  /**
+   * Finishes writing data to the output stream, by writing
+   * the checksum bytes to the end. The underlying stream is not closed.
+   * @throws IOException
+   */
+  public void finish() throws IOException {
+    if (finished) {
+      return;
+    }
+    finished = true;
     sum.writeValue(barray, 0, false);
     out.write (barray, 0, sum.getChecksumSize());
     out.flush();
   }
-  
+
   /**
    * Write bytes to the stream.
    */