Skip to content

Commit ff0afcf

Browse files
committed
Resource.lastModified() propagates 0 value if target resource exists
Includes consistent use of getContentLengthLong over getContentLength. Issue: SPR-17320
1 parent 5343076 commit ff0afcf

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.File;
2020
import java.io.FileNotFoundException;
2121
import java.io.IOException;
22-
import java.io.InputStream;
2322
import java.net.HttpURLConnection;
2423
import java.net.URI;
2524
import java.net.URL;
@@ -66,18 +65,17 @@ else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
6665
return false;
6766
}
6867
}
69-
if (con.getContentLength() >= 0) {
68+
if (con.getContentLengthLong() >= 0) {
7069
return true;
7170
}
7271
if (httpCon != null) {
73-
// no HTTP OK status, and no content-length header: give up
72+
// No HTTP OK status, and no content-length header: give up
7473
httpCon.disconnect();
7574
return false;
7675
}
7776
else {
7877
// Fall back to stream existence: can we open the stream?
79-
InputStream is = getInputStream();
80-
is.close();
78+
getInputStream().close();
8179
return true;
8280
}
8381
}
@@ -211,7 +209,7 @@ public long contentLength() throws IOException {
211209
// Try a URL connection content-length header
212210
URLConnection con = url.openConnection();
213211
customizeConnection(con);
214-
return con.getContentLength();
212+
return con.getContentLengthLong();
215213
}
216214
}
217215

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,8 +57,7 @@ public boolean exists() {
5757
catch (IOException ex) {
5858
// Fall back to stream existence: can we open the stream?
5959
try {
60-
InputStream is = getInputStream();
61-
is.close();
60+
getInputStream().close();
6261
return true;
6362
}
6463
catch (Throwable isEx) {
@@ -146,7 +145,7 @@ public long contentLength() throws IOException {
146145
InputStream is = getInputStream();
147146
try {
148147
long size = 0;
149-
byte[] buf = new byte[255];
148+
byte[] buf = new byte[256];
150149
int read;
151150
while ((read = is.read(buf)) != -1) {
152151
size += read;
@@ -169,10 +168,11 @@ public long contentLength() throws IOException {
169168
*/
170169
@Override
171170
public long lastModified() throws IOException {
172-
long lastModified = getFileForLastModifiedCheck().lastModified();
173-
if (lastModified == 0L) {
171+
File fileToCheck = getFileForLastModifiedCheck();
172+
long lastModified = fileToCheck.lastModified();
173+
if (lastModified == 0L && !fileToCheck.exists()) {
174174
throw new FileNotFoundException(getDescription() +
175-
" cannot be resolved in the file system for resolving its last-modified timestamp");
175+
" cannot be resolved in the file system for checking its last-modified timestamp");
176176
}
177177
return lastModified;
178178
}

0 commit comments

Comments
 (0)