HADOOP-14542. Add IOUtils.cleanupWithLogger that accepts slf4j logger API. Contributed by Chen Liang.
Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/b6495190 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/b6495190 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/b6495190 Branch: refs/heads/HADOOP-13345 Commit: b64951905e64f6fed581c28634be6ed15c190633 Parents: 9ae9467 Author: Akira Ajisaka <[email protected]> Authored: Thu Jun 22 17:42:59 2017 +0900 Committer: Akira Ajisaka <[email protected]> Committed: Thu Jun 22 17:42:59 2017 +0900 ---------------------------------------------------------------------- .../main/java/org/apache/hadoop/io/IOUtils.java | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/b6495190/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java ---------------------------------------------------------------------- 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 ee7264b..459014b 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 @@ -38,6 +38,7 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.util.Shell; +import org.slf4j.Logger; import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_DEFAULT; import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_KEY; @@ -261,6 +262,28 @@ public class IOUtils { } /** + * Close the Closeable objects and <b>ignore</b> any {@link Throwable} or + * null pointers. Must only be used for cleanup in exception handlers. + * + * @param logger the log to record problems to at debug level. Can be null. + * @param closeables the objects to close + */ + public static void cleanupWithLogger(Logger logger, + java.io.Closeable... closeables) { + for (java.io.Closeable c : closeables) { + if (c != null) { + try { + c.close(); + } catch (Throwable e) { + if (logger != null) { + logger.debug("Exception in closing {}", c, e); + } + } + } + } + } + + /** * Closes the stream ignoring {@link Throwable}. * Must only be called in cleaning up from exception handlers. * --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
