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

This commit is contained in:
Arpit Agarwal 2018-01-11 16:50:21 -08:00
parent bc285da107
commit addbcd8cd4
2 changed files with 52 additions and 1 deletions

View File

@ -297,6 +297,18 @@ public static void closeStream(java.io.Closeable 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} * Closes the socket ignoring {@link IOException}
* *

View File

@ -21,9 +21,11 @@
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -41,12 +43,15 @@
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Test cases for IOUtils.java * Test cases for IOUtils.java
*/ */
public class TestIOUtils { public class TestIOUtils {
private static final String TEST_FILE_NAME = "test_file"; private static final String TEST_FILE_NAME = "test_file";
private static final Logger LOG = LoggerFactory.getLogger(TestIOUtils.class);
@Test @Test
public void testCopyBytesShouldCloseStreamsWhenCloseIsTrue() throws Exception { public void testCopyBytesShouldCloseStreamsWhenCloseIsTrue() throws Exception {
@ -289,4 +294,38 @@ public void testListDirectory() throws IOException {
FileUtils.deleteDirectory(dir); 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;
}
}
} }