Browse Source

Polish AbstractResource

See gh-23813
pull/27752/head
Johnny Lim 6 years ago committed by Stephane Nicoll
parent
commit
5c4cde7853
  1. 23
      spring-core/src/main/java/org/springframework/core/io/AbstractResource.java

23
spring-core/src/main/java/org/springframework/core/io/AbstractResource.java

@ -25,6 +25,7 @@ import java.net.URISyntaxException; @@ -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 { @@ -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,14 +70,18 @@ public abstract class AbstractResource implements Resource { @@ -72,14 +70,18 @@ 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;
}
}
private void debug(Supplier<String> message, Throwable ex) {
Log logger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled()) {
logger.debug(message.get(), ex);
}
}
/**
* This implementation always returns {@code true} for a resource
* that {@link #exists() exists} (revised as of 5.1).
@ -174,10 +176,7 @@ public abstract class AbstractResource implements Resource { @@ -174,10 +176,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);
}
}
}

Loading…
Cancel
Save