From addbcd8cd44de25f9fcb1920183155609908aa91 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal Date: Thu, 11 Jan 2018 16:50:21 -0800 Subject: [PATCH] HADOOP-15114. Add closeStreams(...) to IOUtils. Contributed by Ajay Kumar. --- .../java/org/apache/hadoop/io/IOUtils.java | 14 ++++++- .../org/apache/hadoop/io/TestIOUtils.java | 39 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java index 46ea1c8825..4684fb63ae 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java @@ -296,7 +296,19 @@ public static void closeStream(java.io.Closeable stream) { 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} * diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java index cac73bd2b2..f8b5a48f9e 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java @@ -21,9 +21,11 @@ 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.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 void testListDirectory() throws IOException { 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; + } + + } }