From f3d4b3e5ac250a8149dfe5b6d89b9ea88aedb1b5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 3 Nov 2020 17:59:38 +0000 Subject: [PATCH] Allow entries to be streamed from JarURLConnections' jar file Fixes gh-23821 --- .../boot/loader/jar/JarFileWrapper.java | 6 ++++++ .../boot/loader/jar/JarFileWrapperTests.java | 17 ++++++++++++++++- .../boot/loader/jar/JarURLConnectionTests.java | 11 +++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileWrapper.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileWrapper.java index 2fd82e0e02b..a273a90ae56 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileWrapper.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileWrapper.java @@ -24,6 +24,7 @@ import java.security.Permission; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.Manifest; +import java.util.stream.Stream; import java.util.zip.ZipEntry; /** @@ -67,6 +68,11 @@ class JarFileWrapper extends AbstractJarFile { return this.parent.entries(); } + @Override + public Stream stream() { + return this.parent.stream(); + } + @Override public JarEntry getJarEntry(String name) { return this.parent.getJarEntry(name); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileWrapperTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileWrapperTests.java index 4bd32820f76..ea0c853276d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileWrapperTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileWrapperTests.java @@ -28,6 +28,7 @@ import java.util.Enumeration; import java.util.Set; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import org.junit.jupiter.api.AfterEach; @@ -150,6 +151,12 @@ class JarFileWrapperTests { .isThrownBy(() -> JarFileWrapper.class.getDeclaredMethod("close")); } + @Test + void streamDelegatestoParent() { + this.wrapper.stream(); + this.parent.verify(Call.STREAM); + } + /** * {@link JarFile} that we can spy (even on Java 11+) */ @@ -179,6 +186,12 @@ class JarFileWrapperTests { return super.entries(); } + @Override + public Stream stream() { + mark(Call.STREAM); + return super.stream(); + } + @Override public JarEntry getJarEntry(String name) { mark(Call.GET_JAR_ENTRY); @@ -257,7 +270,9 @@ class JarFileWrapperTests { GET_COMMENT, - SIZE + SIZE, + + STREAM } 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 7ea75b2fb67..f7481c5c4fc 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,6 +21,9 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.URL; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.stream.Collectors; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -201,6 +204,14 @@ class JarURLConnectionTests { assertThat(connection.getLastModified()).isEqualTo(connection.getJarEntry().getTime()); } + @Test + void entriesCanBeStreamedFromJarFileOfConnection() throws Exception { + URL url = new URL("jar:" + this.rootJarFile.toURI().toURL() + "!/"); + JarURLConnection connection = JarURLConnection.get(url, this.jarFile); + List entryNames = connection.getJarFile().stream().map(JarEntry::getName).collect(Collectors.toList()); + assertThat(entryNames).hasSize(12); + } + @Test void jarEntryBasicName() { assertThat(new JarEntryName(new StringSequence("a/b/C.class")).toString()).isEqualTo("a/b/C.class");