ClassPathResource.isReadable() returns false for content length 0

Issue: SPR-16832
This commit is contained in:
Juergen Hoeller 2018-05-29 22:31:47 +02:00
parent 8d94d20770
commit 616a40adb6
2 changed files with 12 additions and 12 deletions

View File

@ -80,7 +80,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
}
}
}
catch (Exception ex) {
catch (IOException ex) {
return false;
}
}
@ -110,18 +110,18 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
if (contentLength > 0) {
return true;
}
else if (contentLength < 0) {
else if (contentLength == 0) {
// Empty file or directory -> not considered readable...
return false;
}
// 0 length: either an empty file or a directory...
// On current JDKs, this will trigger an NPE from within the close() call
// for directories, only returning true for actual files with 0 length.
getInputStream().close();
return true;
else {
// Fall back to stream existence: can we open the stream?
getInputStream().close();
return true;
}
}
}
catch (Exception ex) {
// Usually an IOException but potentially a NullPointerException (see above)
catch (IOException ex) {
return false;
}
}
@ -135,7 +135,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
}
return ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol());
}
catch (Exception ex) {
catch (IOException ex) {
return false;
}
}
@ -186,7 +186,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
}
return ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme());
}
catch (Exception ex) {
catch (IOException ex) {
return false;
}
}

View File

@ -60,7 +60,7 @@ public interface Resource extends InputStreamSource {
boolean exists();
/**
* Indicate whether the contents of this resource can be read via
* Indicate whether non-empty contents of this resource can be read via
* {@link #getInputStream()}.
* <p>Will be {@code true} for typical resource descriptors that exist
* since it strictly implies {@link #exists()} semantics as of 5.1.