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 ee7264bdedc..459014ba727 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; @@ -260,6 +261,28 @@ public class IOUtils { } } + /** + * 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.