瀏覽代碼

HADOOP-15114. Add closeStreams(...) to IOUtils. Contributed by Ajay Kumar.

Arpit Agarwal 7 年之前
父節點
當前提交
addbcd8cd4

+ 13 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java

@@ -296,7 +296,19 @@ public class IOUtils {
       cleanupWithLogger(null, stream);
     }
   }
-  
+
+  /**
+   * Closes the streams ignoring {@link Throwable}.
+   * Must only be called in cleaning up from exception handlers.
+   *
+   * @param streams the Streams to close
+   */
+  public static void closeStreams(java.io.Closeable... streams) {
+    if (streams != null) {
+      cleanupWithLogger(null, streams);
+    }
+  }
+
   /**
    * Closes the socket ignoring {@link IOException}
    *

+ 39 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java

@@ -21,9 +21,11 @@ package org.apache.hadoop.io;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
+import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.EOFException;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
@@ -41,12 +43,15 @@ import org.apache.hadoop.test.GenericTestUtils;
 import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Test cases for IOUtils.java
  */
 public class TestIOUtils {
   private static final String TEST_FILE_NAME = "test_file";
+  private static final Logger LOG = LoggerFactory.getLogger(TestIOUtils.class);
   
   @Test
   public void testCopyBytesShouldCloseStreamsWhenCloseIsTrue() throws Exception {
@@ -289,4 +294,38 @@ public class TestIOUtils {
       FileUtils.deleteDirectory(dir);
     }
   }
+
+  @Test
+  public void testCloseStreams() {
+    File tmpFile = new File("deleteMe.txt");
+    FileOutputStream fos = null;
+    BufferedOutputStream bos = null;
+    FileOutputStream nullStream = null;
+
+    try {
+      fos = new FileOutputStream(tmpFile) {
+        @Override
+        public void close() throws IOException {
+          throw new IOException();
+        }
+      };
+      bos = new BufferedOutputStream(
+          new FileOutputStream(tmpFile)) {
+        @Override
+        public void close() throws IOException {
+          throw new NullPointerException();
+        }
+      };
+    } catch (IOException ioe) {
+      LOG.warn("Exception in TestIOUtils.testCloseStreams: ", ioe);
+    }
+    try {
+      IOUtils.closeStreams(fos, bos, nullStream);
+      IOUtils.closeStreams();
+    } catch (Exception ex) {
+      LOG.error("Expect IOUtils.closeStreams to close streams quietly.", ex);
+      throw ex;
+    }
+
+  }
 }