< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JarArchive.java

Print this page
rev 15409 : 8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
Reviewed-by:
Contributed-by: steve.drach@oracle.com

@@ -28,12 +28,13 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UncheckedIOException;
 import java.nio.file.Path;
 import java.util.Objects;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 import java.util.stream.Stream;
-import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import jdk.tools.jlink.internal.Archive.Entry.EntryType;
 
 /**
  * An Archive backed by a jar file.

@@ -41,17 +42,17 @@
 public abstract class JarArchive implements Archive {
 
     /**
      * An entry located in a jar file.
      */
-    private class JarEntry extends Entry {
+    private class JarFileEntry extends Entry {
 
         private final long size;
-        private final ZipEntry entry;
-        private final ZipFile file;
+        private final JarEntry entry;
+        private final JarFile file;
 
-        JarEntry(String path, String name, EntryType type, ZipFile file, ZipEntry entry) {
+        JarFileEntry(String path, String name, EntryType type, JarFile file, JarEntry entry) {
             super(JarArchive.this, path, name, type);
             this.entry = Objects.requireNonNull(entry);
             this.file = Objects.requireNonNull(file);
             size = entry.getSize();
         }

@@ -72,12 +73,13 @@
 
     private static final String MODULE_INFO = "module-info.class";
 
     private final Path file;
     private final String moduleName;
-    // currently processed ZipFile
-    private ZipFile zipFile;
+    // currently processed JarFile
+    private JarFile jarFile;
+    private int prefixLength = 0;
 
     protected JarArchive(String mn, Path file) {
         Objects.requireNonNull(mn);
         Objects.requireNonNull(file);
         this.moduleName = mn;

@@ -95,25 +97,33 @@
     }
 
     @Override
     public Stream<Entry> entries() {
         try {
-            if (zipFile == null) {
+            if (jarFile == null) {
                 open();
             }
         } catch (IOException ioe) {
             throw new UncheckedIOException(ioe);
         }
-        return zipFile.stream().map(this::toEntry).filter(n -> n != null);
+        Stream<JarEntry> jarEntries = jarFile.stream();
+        if (jarFile.isMultiRelease()) {
+            int version = jarFile.getVersion().major();
+            String name = "META-INF/versions/" + version + "/";
+            prefixLength = name.length();
+            jarEntries = jarEntries
+                    .filter(je -> je.getName().startsWith(name));
+        }
+        return jarEntries.map(this::toEntry).filter(n -> n != null);
     }
 
     abstract EntryType toEntryType(String entryName);
 
     abstract String getFileName(String entryName);
 
-    private Entry toEntry(ZipEntry ze) {
-        String name = ze.getName();
+    private Entry toEntry(JarEntry ze) {
+        String name = ze.getName().substring(prefixLength);
         String fn = getFileName(name);
 
         if (ze.isDirectory() || fn.startsWith("_")) {
             return null;
         }

@@ -121,23 +131,23 @@
         EntryType rt = toEntryType(name);
 
         if (fn.equals(MODULE_INFO)) {
             fn = moduleName + "/" + MODULE_INFO;
         }
-        return new JarEntry(ze.getName(), fn, rt, zipFile, ze);
+        return new JarFileEntry(name, fn, rt, jarFile, ze);
     }
 
     @Override
     public void close() throws IOException {
-        if (zipFile != null) {
-            zipFile.close();
+        if (jarFile != null) {
+            jarFile.close();
         }
     }
 
     @Override
     public void open() throws IOException {
-        if (zipFile != null) {
-            zipFile.close();
+        if (jarFile != null) {
+            jarFile.close();
         }
-        zipFile = new ZipFile(file.toFile());
+        jarFile = new JarFile(file.toFile(), true, ZipFile.OPEN_READ, JarFile.runtimeVersion());
     }
 }
< prev index next >