From 897d5a4d24ec862999ba77e7ad2bd7de724f981a Mon Sep 17 00:00:00 2001 From: Akira Ajisaka Date: Thu, 22 Jun 2017 17:42:59 +0900 Subject: [PATCH] HADOOP-14542. Add IOUtils.cleanupWithLogger that accepts slf4j logger API. Contributed by Chen Liang. (cherry picked from commit b64951905e64f6fed581c28634be6ed15c190633) --- .../java/org/apache/hadoop/io/IOUtils.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 a56ccfe7d48..8d6bd984152 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.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; @@ -260,6 +261,28 @@ public static void cleanup(Log log, java.io.Closeable... closeables) { } } + /** + * Close the Closeable objects and ignore 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.