Early getJarFile() call for consistent jar file existence check

See gh-34796
This commit is contained in:
Juergen Hoeller 2025-04-23 10:16:12 +02:00
parent 0252e39409
commit 253f321e8b
1 changed files with 6 additions and 9 deletions

View File

@ -29,6 +29,7 @@ import java.nio.channels.ReadableByteChannel;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.util.ResourceUtils;
@ -44,6 +45,7 @@ import org.springframework.util.ResourceUtils;
*/
public abstract class AbstractFileResolvingResource extends AbstractResource {
@SuppressWarnings("try")
@Override
public boolean exists() {
try {
@ -86,15 +88,10 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
if (con instanceof JarURLConnection jarCon) {
// For JarURLConnection, do not check content-length but rather the
// existence of the entry (or the jar root in case of no entryName).
try {
if (jarCon.getEntryName() == null) {
// Jar root: check for the existence of any actual jar entries.
return jarCon.getJarFile().entries().hasMoreElements();
}
return (jarCon.getJarEntry() != null);
}
finally {
jarCon.getJarFile().close();
// getJarFile() called for enforced presence check of the jar file,
// throwing a NoSuchFileException otherwise (turned to false below).
try (JarFile jarFile = jarCon.getJarFile()) {
return (jarCon.getEntryName() == null || jarCon.getJarEntry() != null);
}
}
else if (con.getContentLengthLong() > 0) {