diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java index 8be0ceb8be..0e493a4f9c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java @@ -146,6 +146,7 @@ public class JarFile extends java.util.jar.JarFile implements Iterable manifestSupplier) throws IOException { super(rootFile.getFile()); + super.close(); this.parent = parent; this.rootFile = rootFile; this.pathFromRoot = pathFromRoot; @@ -353,7 +354,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable 0) { JarEntryName entryName = JarEntryName.get(spec.subSequence(index, separator)); JarEntry jarEntry = jarFile.getJarEntry(entryName.toCharSequence()); if (jarEntry == null) { - return JarURLConnection.notFound(connectionJarFile, entryName, - (connectionJarFile != jarFile) ? connectionJarFile::close : null); + return JarURLConnection.notFound(jarFile, entryName); } - connectionJarFile = connectionJarFile.getNestedJarFile(jarEntry); + jarFile = jarFile.getNestedJarFile(jarEntry); index = separator + SEPARATOR.length(); } JarEntryName jarEntryName = JarEntryName.get(spec, index); if (Boolean.TRUE.equals(useFastExceptions.get()) && !jarEntryName.isEmpty() - && !connectionJarFile.containsEntry(jarEntryName.toString())) { - if (connectionJarFile != jarFile) { - connectionJarFile.close(); - } + && !jarFile.containsEntry(jarEntryName.toString())) { return NOT_FOUND_CONNECTION; } - return new JarURLConnection(url, new JarFile(connectionJarFile), jarEntryName, - (connectionJarFile != jarFile) ? connectionJarFile::close : null); + return new JarURLConnection(url, new JarFile(jarFile), jarEntryName); } private static int indexOfRootSpec(StringSequence file, String pathFromRoot) { @@ -300,22 +279,18 @@ final class JarURLConnection extends java.net.JarURLConnection { private static JarURLConnection notFound() { try { - return notFound(null, null, null); + return notFound(null, null); } catch (IOException ex) { throw new IllegalStateException(ex); } } - private static JarURLConnection notFound(JarFile jarFile, JarEntryName jarEntryName, CloseAction closeAction) - throws IOException { + private static JarURLConnection notFound(JarFile jarFile, JarEntryName jarEntryName) throws IOException { if (Boolean.TRUE.equals(useFastExceptions.get())) { - if (closeAction != null) { - closeAction.perform(); - } return NOT_FOUND_CONNECTION; } - return new JarURLConnection(null, jarFile, jarEntryName, closeAction); + return new JarURLConnection(null, jarFile, jarEntryName); } /** @@ -418,15 +393,4 @@ final class JarURLConnection extends java.net.JarURLConnection { } - /** - * An action to be taken when the connection is being "closed" and its underlying - * resources are no longer needed. - */ - @FunctionalInterface - private interface CloseAction { - - void perform() throws IOException; - - } - } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java index a836c01e37..402ad6850e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarURLConnectionTests.java @@ -21,7 +21,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.URL; -import java.util.zip.ZipFile; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -30,7 +29,6 @@ import org.junit.jupiter.api.io.TempDir; import org.springframework.boot.loader.TestJarCreator; import org.springframework.boot.loader.jar.JarURLConnection.JarEntryName; -import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -230,7 +228,7 @@ class JarURLConnectionTests { JarURLConnection connection = JarURLConnection.get(url, this.jarFile); JarFile connectionJarFile = connection.getJarFile(); connectionJarFile.close(); - assertThat((Boolean) ReflectionTestUtils.getField(this.jarFile, ZipFile.class, "closeRequested")).isFalse(); + assertThat(this.jarFile.isClosed()).isFalse(); } private String getRelativePath() {