diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java index de327d7e1c1..cf3266672f9 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.util.function.Supplier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -60,10 +61,7 @@ public abstract class AbstractResource implements Resource { return getFile().exists(); } catch (IOException ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug("Could not retrieve File for existence check of " + getDescription(), ex); - } + debug(() -> "Could not retrieve File for existence check of " + getDescription(), ex); } } // Fall back to stream existence: can we open the stream? @@ -72,10 +70,7 @@ public abstract class AbstractResource implements Resource { return true; } catch (Throwable ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug("Could not retrieve InputStream for existence check of " + getDescription(), ex); - } + debug(() -> "Could not retrieve InputStream for existence check of " + getDescription(), ex); return false; } } @@ -174,10 +169,7 @@ public abstract class AbstractResource implements Resource { is.close(); } catch (IOException ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug("Could not close content-length InputStream for " + getDescription(), ex); - } + debug(() -> "Could not close content-length InputStream for " + getDescription(), ex); } } } @@ -258,4 +250,11 @@ public abstract class AbstractResource implements Resource { return getDescription(); } + private void debug(Supplier message, Throwable ex) { + Log logger = LogFactory.getLog(getClass()); + if (logger.isDebugEnabled()) { + logger.debug(message.get(), ex); + } + } + }