Skip to content

Commit 616a40a

Browse files
committed
ClassPathResource.isReadable() returns false for content length 0
Issue: SPR-16832
1 parent 8d94d20 commit 616a40a

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
8080
}
8181
}
8282
}
83-
catch (Exception ex) {
83+
catch (IOException ex) {
8484
return false;
8585
}
8686
}
@@ -110,18 +110,18 @@ public boolean isReadable() {
110110
if (contentLength > 0) {
111111
return true;
112112
}
113-
else if (contentLength < 0) {
113+
else if (contentLength == 0) {
114+
// Empty file or directory -> not considered readable...
114115
return false;
115116
}
116-
// 0 length: either an empty file or a directory...
117-
// On current JDKs, this will trigger an NPE from within the close() call
118-
// for directories, only returning true for actual files with 0 length.
119-
getInputStream().close();
120-
return true;
117+
else {
118+
// Fall back to stream existence: can we open the stream?
119+
getInputStream().close();
120+
return true;
121+
}
121122
}
122123
}
123-
catch (Exception ex) {
124-
// Usually an IOException but potentially a NullPointerException (see above)
124+
catch (IOException ex) {
125125
return false;
126126
}
127127
}
@@ -135,7 +135,7 @@ public boolean isFile() {
135135
}
136136
return ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol());
137137
}
138-
catch (Exception ex) {
138+
catch (IOException ex) {
139139
return false;
140140
}
141141
}
@@ -186,7 +186,7 @@ protected boolean isFile(URI uri) {
186186
}
187187
return ResourceUtils.URL_PROTOCOL_FILE.equals(uri.getScheme());
188188
}
189-
catch (Exception ex) {
189+
catch (IOException ex) {
190190
return false;
191191
}
192192
}

spring-core/src/main/java/org/springframework/core/io/Resource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public interface Resource extends InputStreamSource {
6060
boolean exists();
6161

6262
/**
63-
* Indicate whether the contents of this resource can be read via
63+
* Indicate whether non-empty contents of this resource can be read via
6464
* {@link #getInputStream()}.
6565
* <p>Will be {@code true} for typical resource descriptors that exist
6666
* since it strictly implies {@link #exists()} semantics as of 5.1.

0 commit comments

Comments
 (0)