From b53995b5d4df43bb4e12bee179bffccfac080487 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 10 Oct 2018 00:31:49 +0200 Subject: [PATCH] Resource.lastModified() propagates 0 value if target resource exists Issue: SPR-17320 --- .../springframework/core/io/AbstractResource.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java index 7d38cee93f..eb60a49a2c 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -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. @@ -55,8 +55,7 @@ public abstract class AbstractResource implements Resource { 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) { @@ -126,7 +125,7 @@ public abstract class AbstractResource implements Resource { Assert.state(is != null, "Resource InputStream must not be null"); try { long size = 0; - byte[] buf = new byte[255]; + byte[] buf = new byte[256]; int read; while ((read = is.read(buf)) != -1) { size += read; @@ -149,10 +148,11 @@ public abstract class AbstractResource implements Resource { */ @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; }