Skip to content

Commit

Permalink
Resource.lastModified() propagates 0 value if target resource exists
Browse files Browse the repository at this point in the history
Includes consistent use of getContentLengthLong over getContentLength.

Issue: SPR-17320
  • Loading branch information
jhoeller committed Oct 9, 2018
1 parent 5343076 commit ff0afcf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -66,18 +65,17 @@ else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
return false;
}
}
if (con.getContentLength() >= 0) {
if (con.getContentLengthLong() >= 0) {
return true;
}
if (httpCon != null) {
// no HTTP OK status, and no content-length header: give up
// No HTTP OK status, and no content-length header: give up
httpCon.disconnect();
return false;
}
else {
// Fall back to stream existence: can we open the stream?
InputStream is = getInputStream();
is.close();
getInputStream().close();
return true;
}
}
Expand Down Expand Up @@ -211,7 +209,7 @@ public long contentLength() throws IOException {
// Try a URL connection content-length header
URLConnection con = url.openConnection();
customizeConnection(con);
return con.getContentLength();
return con.getContentLengthLong();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -57,8 +57,7 @@ public boolean exists() {
catch (IOException ex) {
// Fall back to stream existence: can we open the stream?
try {
InputStream is = getInputStream();
is.close();
getInputStream().close();
return true;
}
catch (Throwable isEx) {
Expand Down Expand Up @@ -146,7 +145,7 @@ public long contentLength() throws IOException {
InputStream is = getInputStream();
try {
long size = 0;
byte[] buf = new byte[255];
byte[] buf = new byte[256];
int read;
while ((read = is.read(buf)) != -1) {
size += read;
Expand All @@ -169,10 +168,11 @@ public long contentLength() throws IOException {
*/
@Override
public long lastModified() throws IOException {
long lastModified = getFileForLastModifiedCheck().lastModified();
if (lastModified == 0L) {
File fileToCheck = getFileForLastModifiedCheck();
long lastModified = fileToCheck.lastModified();
if (lastModified == 0L && !fileToCheck.exists()) {
throw new FileNotFoundException(getDescription() +
" cannot be resolved in the file system for resolving its last-modified timestamp");
" cannot be resolved in the file system for checking its last-modified timestamp");
}
return lastModified;
}
Expand Down

0 comments on commit ff0afcf

Please sign in to comment.