< prev index next >

jdk/src/java.base/share/classes/jdk/internal/jmod/JmodFile.java

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.jmod;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;





  33 import java.util.stream.Stream;
  34 import java.util.zip.ZipEntry;
  35 import java.util.zip.ZipFile;
  36 
  37 /**
  38  * Helper class to read JMOD file
  39  */
  40 public class JmodFile implements AutoCloseable {
  41     // jmod magic number and version number
  42     public static final int JMOD_MAJOR_VERSION = 0x01;
  43     public static final int JMOD_MINOR_VERSION = 0x00;
  44     public static final byte[] JMOD_MAGIC_NUMBER = {
  45         0x4A, 0x4D, /* JM */
  46         JMOD_MAJOR_VERSION, JMOD_MINOR_VERSION, /* version 1.0 */
  47     };
  48 
  49     public static void checkMagic(Path file) throws IOException {
  50         try (InputStream in = Files.newInputStream(file);
  51              BufferedInputStream bis = new BufferedInputStream(in)) {
  52             // validate the header
  53             byte[] magic = new byte[4];
  54             bis.read(magic);
  55             if (magic[0] != JMOD_MAGIC_NUMBER[0] ||
  56                 magic[1] != JMOD_MAGIC_NUMBER[1]) {
  57                 throw new IOException("Invalid jmod file: " + file.toString());
  58             }
  59             if (magic[2] > JMOD_MAJOR_VERSION ||
  60                 (magic[2] == JMOD_MAJOR_VERSION && magic[3] > JMOD_MINOR_VERSION)) {
  61                 throw new IOException("Unsupported jmod version: " +
  62                     magic[2] + "." + magic[3] + " in " + file.toString());
  63             }
  64         }
  65     }
  66 
  67     /**
  68      * JMOD sections
  69      */
  70     public static enum Section {
  71         NATIVE_LIBS("native"),
  72         NATIVE_CMDS("bin"),
  73         CLASSES("classes"),
  74         CONFIG("conf"),
  75         HEADER_FILES("include"),
  76         MAN_PAGES("man");



  77 
  78         private final String jmodDir;
  79         private Section(String jmodDir) {
  80             this.jmodDir = jmodDir;
  81         }
  82 
  83         /**
  84          * Returns the directory name in the JMOD file corresponding to
  85          * this section
  86          */
  87         public String jmodDir() { return jmodDir; }
  88 
  89     }
  90 
  91     /**
  92      * JMOD file entry.
  93      *
  94      * Each entry corresponds to a ZipEntry whose name is:
  95      *   Section::jmodDir + '/' + name
  96      */
  97     public static class Entry {
  98         private final ZipEntry zipEntry;
  99         private final Section section;
 100         private final String name;
 101 
 102         private Entry(ZipEntry e) {
 103             String name = e.getName();
 104             int i = name.indexOf('/');
 105             if (i <= 1) {
 106                 throw new RuntimeException("invalid jmod entry: " + name);
 107             }
 108 
 109             this.zipEntry = e;
 110             this.section = section(name);
 111             this.name = name.substring(i+1);
 112         }
 113 
 114         /**
 115          * Returns the section of this entry.
 116          */
 117         public Section section() {
 118             return section;
 119         }
 120 
 121         /**
 122          * Returns the name of this entry.
 123          */
 124         public String name() {
 125             return name;
 126         }
 127 
 128         /**
 129          * Returns the size of this entry.
 130          */
 131         public long size() {
 132             return zipEntry.getSize();
 133         }
 134 
 135         public ZipEntry zipEntry() {
 136             return zipEntry;
 137         }
 138 
 139         @Override
 140         public String toString() {
 141             return section.jmodDir() + "/" + name;
 142         }
 143 







 144         static Section section(String name) {
 145             int i = name.indexOf('/');
 146             String s = name.substring(0, i);
 147             switch (s) {
 148                 case "native":
 149                     return Section.NATIVE_LIBS;
 150                 case "bin":
 151                     return Section.NATIVE_CMDS;
 152                 case "classes":
 153                     return Section.CLASSES;
 154                 case "conf":
 155                     return Section.CONFIG;
 156                 case "include":
 157                     return Section.HEADER_FILES;
 158                 case "man":
 159                     return Section.MAN_PAGES;
 160                 default:
 161                     throw new IllegalArgumentException("invalid section: " + s);
 162             }

 163         }

 164     }
 165 
 166     private final Path file;
 167     private final ZipFile zipfile;
 168 
 169     /**
 170      * Constructs a {@code JmodFile} from a given path.
 171      */
 172     public JmodFile(Path file) throws IOException {
 173         checkMagic(file);
 174         this.file = file;
 175         this.zipfile = new ZipFile(file.toFile());
 176     }
 177 
 178     /**
 179      * Returns the {@code Entry} for a resource in a JMOD file section
 180      * or {@code null} if not found.
 181      */
 182     public Entry getEntry(Section section, String name) {
 183         String entry = section.jmodDir() + "/" + name;




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.jmod;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.util.Arrays;
  34 import java.util.HashMap;
  35 import java.util.Map;
  36 import java.util.function.Function;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 import java.util.zip.ZipEntry;
  40 import java.util.zip.ZipFile;
  41 
  42 /**
  43  * Helper class to read JMOD file
  44  */
  45 public class JmodFile implements AutoCloseable {
  46     // jmod magic number and version number
  47     public static final int JMOD_MAJOR_VERSION = 0x01;
  48     public static final int JMOD_MINOR_VERSION = 0x00;
  49     public static final byte[] JMOD_MAGIC_NUMBER = {
  50         0x4A, 0x4D, /* JM */
  51         JMOD_MAJOR_VERSION, JMOD_MINOR_VERSION, /* version 1.0 */
  52     };
  53 
  54     public static void checkMagic(Path file) throws IOException {
  55         try (InputStream in = Files.newInputStream(file);
  56              BufferedInputStream bis = new BufferedInputStream(in)) {
  57             // validate the header
  58             byte[] magic = new byte[4];
  59             bis.read(magic);
  60             if (magic[0] != JMOD_MAGIC_NUMBER[0] ||
  61                 magic[1] != JMOD_MAGIC_NUMBER[1]) {
  62                 throw new IOException("Invalid jmod file: " + file.toString());
  63             }
  64             if (magic[2] > JMOD_MAJOR_VERSION ||
  65                 (magic[2] == JMOD_MAJOR_VERSION && magic[3] > JMOD_MINOR_VERSION)) {
  66                 throw new IOException("Unsupported jmod version: " +
  67                     magic[2] + "." + magic[3] + " in " + file.toString());
  68             }
  69         }
  70     }
  71 
  72     /**
  73      * JMOD sections
  74      */
  75     public static enum Section {


  76         CLASSES("classes"),
  77         CONFIG("conf"),
  78         HEADER_FILES("include"),
  79         LEGAL_NOTICES("legal"),
  80         MAN_PAGES("man"),
  81         NATIVE_LIBS("native"),
  82         NATIVE_CMDS("bin");
  83 
  84         private final String jmodDir;
  85         private Section(String jmodDir) {
  86             this.jmodDir = jmodDir;
  87         }
  88 
  89         /**
  90          * Returns the directory name in the JMOD file corresponding to
  91          * this section
  92          */
  93         public String jmodDir() { return jmodDir; }

  94     }
  95 
  96     /**
  97      * JMOD file entry.
  98      *
  99      * Each entry corresponds to a ZipEntry whose name is:
 100      *   Section::jmodDir + '/' + name
 101      */
 102     public static class Entry {
 103         private final ZipEntry zipEntry;
 104         private final Section section;
 105         private final String name;
 106 
 107         private Entry(ZipEntry e) {
 108             String name = e.getName();
 109             int i = name.indexOf('/');
 110             if (i <= 1) {
 111                 throw new RuntimeException("invalid jmod entry: " + name);
 112             }
 113 
 114             this.zipEntry = e;
 115             this.section = section(name.substring(0, i));
 116             this.name = name.substring(i+1);
 117         }
 118 
 119         /**
 120          * Returns the section of this entry.
 121          */
 122         public Section section() {
 123             return section;
 124         }
 125 
 126         /**
 127          * Returns the name of this entry.
 128          */
 129         public String name() {
 130             return name;
 131         }
 132 
 133         /**
 134          * Returns the size of this entry.
 135          */
 136         public long size() {
 137             return zipEntry.getSize();
 138         }
 139 
 140         public ZipEntry zipEntry() {
 141             return zipEntry;
 142         }
 143 
 144         @Override
 145         public String toString() {
 146             return section.jmodDir() + "/" + name;
 147         }
 148 
 149         /*
 150          * A map from the jmodDir name to Section
 151          */
 152         static final Map<String, Section> NAME_TO_SECTION =
 153             Arrays.stream(Section.values())
 154                   .collect(Collectors.toMap(Section::jmodDir, Function.identity()));
 155 
 156         static Section section(String name) {
 157             if (!NAME_TO_SECTION.containsKey(name)) {
 158                 throw new IllegalArgumentException("invalid section: " + name);
 159 














 160             }
 161             return NAME_TO_SECTION.get(name);
 162         }
 163 
 164     }
 165 
 166     private final Path file;
 167     private final ZipFile zipfile;
 168 
 169     /**
 170      * Constructs a {@code JmodFile} from a given path.
 171      */
 172     public JmodFile(Path file) throws IOException {
 173         checkMagic(file);
 174         this.file = file;
 175         this.zipfile = new ZipFile(file.toFile());
 176     }
 177 
 178     /**
 179      * Returns the {@code Entry} for a resource in a JMOD file section
 180      * or {@code null} if not found.
 181      */
 182     public Entry getEntry(Section section, String name) {
 183         String entry = section.jmodDir() + "/" + name;


< prev index next >