Commit f3d4b3e5 authored by Andy Wilkinson's avatar Andy Wilkinson

Allow entries to be streamed from JarURLConnections' jar file

Fixes gh-23821
parent 2d9d40bd
...@@ -24,6 +24,7 @@ import java.security.Permission; ...@@ -24,6 +24,7 @@ import java.security.Permission;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import java.util.stream.Stream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
/** /**
...@@ -67,6 +68,11 @@ class JarFileWrapper extends AbstractJarFile { ...@@ -67,6 +68,11 @@ class JarFileWrapper extends AbstractJarFile {
return this.parent.entries(); return this.parent.entries();
} }
@Override
public Stream<JarEntry> stream() {
return this.parent.stream();
}
@Override @Override
public JarEntry getJarEntry(String name) { public JarEntry getJarEntry(String name) {
return this.parent.getJarEntry(name); return this.parent.getJarEntry(name);
......
...@@ -28,6 +28,7 @@ import java.util.Enumeration; ...@@ -28,6 +28,7 @@ import java.util.Enumeration;
import java.util.Set; import java.util.Set;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import java.util.stream.Stream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
...@@ -150,6 +151,12 @@ class JarFileWrapperTests { ...@@ -150,6 +151,12 @@ class JarFileWrapperTests {
.isThrownBy(() -> JarFileWrapper.class.getDeclaredMethod("close")); .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+) * {@link JarFile} that we can spy (even on Java 11+)
*/ */
...@@ -179,6 +186,12 @@ class JarFileWrapperTests { ...@@ -179,6 +186,12 @@ class JarFileWrapperTests {
return super.entries(); return super.entries();
} }
@Override
public Stream<java.util.jar.JarEntry> stream() {
mark(Call.STREAM);
return super.stream();
}
@Override @Override
public JarEntry getJarEntry(String name) { public JarEntry getJarEntry(String name) {
mark(Call.GET_JAR_ENTRY); mark(Call.GET_JAR_ENTRY);
...@@ -257,7 +270,9 @@ class JarFileWrapperTests { ...@@ -257,7 +270,9 @@ class JarFileWrapperTests {
GET_COMMENT, GET_COMMENT,
SIZE SIZE,
STREAM
} }
......
...@@ -21,6 +21,9 @@ import java.io.File; ...@@ -21,6 +21,9 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; 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.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
...@@ -201,6 +204,14 @@ class JarURLConnectionTests { ...@@ -201,6 +204,14 @@ class JarURLConnectionTests {
assertThat(connection.getLastModified()).isEqualTo(connection.getJarEntry().getTime()); 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<String> entryNames = connection.getJarFile().stream().map(JarEntry::getName).collect(Collectors.toList());
assertThat(entryNames).hasSize(12);
}
@Test @Test
void jarEntryBasicName() { void jarEntryBasicName() {
assertThat(new JarEntryName(new StringSequence("a/b/C.class")).toString()).isEqualTo("a/b/C.class"); assertThat(new JarEntryName(new StringSequence("a/b/C.class")).toString()).isEqualTo("a/b/C.class");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment