jdk/src/java.base/share/classes/jdk/internal/jmod/JmodFile.java
author mchung
Mon, 12 Dec 2016 18:56:50 -0800
changeset 42670 d833113eb7d7
parent 42468 7a9555a7e080
child 43712 5dfd0950317c
permissions -rw-r--r--
8169925: Organize licenses by module in source, JMOD file, and run-time image Reviewed-by: alanb, erikj, ihse, naoto, prr Contributed-by: mandy.chung@oracle.com, jeannette.hung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     1
/*
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     4
 *
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    10
 *
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    15
 * accompanied this code).
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    16
 *
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    20
 *
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    23
 * questions.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    24
 */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    25
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    26
package jdk.internal.jmod;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    27
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    28
import java.io.BufferedInputStream;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    29
import java.io.IOException;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    30
import java.io.InputStream;
42468
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
    31
import java.io.OutputStream;
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    32
import java.nio.file.Files;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    33
import java.nio.file.Path;
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    34
import java.util.Arrays;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    35
import java.util.Map;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    36
import java.util.function.Function;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    37
import java.util.stream.Collectors;
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    38
import java.util.stream.Stream;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    39
import java.util.zip.ZipEntry;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    40
import java.util.zip.ZipFile;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    41
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    42
/**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    43
 * Helper class to read JMOD file
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    44
 */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    45
public class JmodFile implements AutoCloseable {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    46
    // jmod magic number and version number
42468
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
    47
    private static final int JMOD_MAJOR_VERSION = 0x01;
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
    48
    private static final int JMOD_MINOR_VERSION = 0x00;
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
    49
    private static final byte[] JMOD_MAGIC_NUMBER = {
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    50
        0x4A, 0x4D, /* JM */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    51
        JMOD_MAJOR_VERSION, JMOD_MINOR_VERSION, /* version 1.0 */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    52
    };
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    53
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    54
    public static void checkMagic(Path file) throws IOException {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    55
        try (InputStream in = Files.newInputStream(file);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    56
             BufferedInputStream bis = new BufferedInputStream(in)) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    57
            // validate the header
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    58
            byte[] magic = new byte[4];
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    59
            bis.read(magic);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    60
            if (magic[0] != JMOD_MAGIC_NUMBER[0] ||
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    61
                magic[1] != JMOD_MAGIC_NUMBER[1]) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    62
                throw new IOException("Invalid jmod file: " + file.toString());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    63
            }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    64
            if (magic[2] > JMOD_MAJOR_VERSION ||
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    65
                (magic[2] == JMOD_MAJOR_VERSION && magic[3] > JMOD_MINOR_VERSION)) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    66
                throw new IOException("Unsupported jmod version: " +
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    67
                    magic[2] + "." + magic[3] + " in " + file.toString());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    68
            }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    69
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    70
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    71
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    72
    /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    73
     * JMOD sections
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    74
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    75
    public static enum Section {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    76
        CLASSES("classes"),
41561
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41352
diff changeset
    77
        CONFIG("conf"),
0c6942d13f2e 8167558: Add new JMOD section for header files and man pages
mchung
parents: 41352
diff changeset
    78
        HEADER_FILES("include"),
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    79
        LEGAL_NOTICES("legal"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    80
        MAN_PAGES("man"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    81
        NATIVE_LIBS("native"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
    82
        NATIVE_CMDS("bin");
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    83
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    84
        private final String jmodDir;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    85
        private Section(String jmodDir) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    86
            this.jmodDir = jmodDir;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    87
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    88
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    89
        /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    90
         * Returns the directory name in the JMOD file corresponding to
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    91
         * this section
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    92
         */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    93
        public String jmodDir() { return jmodDir; }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    94
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    95
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    96
    /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    97
     * JMOD file entry.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    98
     *
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
    99
     * Each entry corresponds to a ZipEntry whose name is:
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   100
     *   Section::jmodDir + '/' + name
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   101
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   102
    public static class Entry {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   103
        private final ZipEntry zipEntry;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   104
        private final Section section;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   105
        private final String name;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   106
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   107
        private Entry(ZipEntry e) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   108
            String name = e.getName();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   109
            int i = name.indexOf('/');
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   110
            if (i <= 1) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   111
                throw new RuntimeException("invalid jmod entry: " + name);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   112
            }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   113
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   114
            this.zipEntry = e;
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   115
            this.section = section(name.substring(0, i));
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   116
            this.name = name.substring(i+1);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   117
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   118
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   119
        /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   120
         * Returns the section of this entry.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   121
         */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   122
        public Section section() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   123
            return section;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   124
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   125
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   126
        /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   127
         * Returns the name of this entry.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   128
         */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   129
        public String name() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   130
            return name;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   131
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   132
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   133
        /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   134
         * Returns the size of this entry.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   135
         */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   136
        public long size() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   137
            return zipEntry.getSize();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   138
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   139
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   140
        public ZipEntry zipEntry() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   141
            return zipEntry;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   142
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   143
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   144
        @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   145
        public String toString() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   146
            return section.jmodDir() + "/" + name;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   147
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   148
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   149
        /*
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   150
         * A map from the jmodDir name to Section
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   151
         */
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   152
        static final Map<String, Section> NAME_TO_SECTION =
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   153
            Arrays.stream(Section.values())
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   154
                  .collect(Collectors.toMap(Section::jmodDir, Function.identity()));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   155
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   156
        static Section section(String name) {
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   157
            if (!NAME_TO_SECTION.containsKey(name)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   158
                throw new IllegalArgumentException("invalid section: " + name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   159
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   160
            }
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   161
            return NAME_TO_SECTION.get(name);
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   162
        }
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents: 42468
diff changeset
   163
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   164
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   165
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   166
    private final Path file;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   167
    private final ZipFile zipfile;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   168
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   169
    /**
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   170
     * Constructs a {@code JmodFile} from a given path.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   171
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   172
    public JmodFile(Path file) throws IOException {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   173
        checkMagic(file);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   174
        this.file = file;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   175
        this.zipfile = new ZipFile(file.toFile());
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   176
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   177
42468
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
   178
    public static void writeMagicNumber(OutputStream os) throws IOException {
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
   179
        os.write(JMOD_MAGIC_NUMBER);
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
   180
    }
7a9555a7e080 8166568: Add jmod extract subcommand
chegar
parents: 41817
diff changeset
   181
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   182
    /**
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   183
     * Returns the {@code Entry} for a resource in a JMOD file section
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   184
     * or {@code null} if not found.
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   185
     */
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   186
    public Entry getEntry(Section section, String name) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   187
        String entry = section.jmodDir() + "/" + name;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   188
        ZipEntry ze = zipfile.getEntry(entry);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   189
        return (ze != null) ? new Entry(ze) : null;
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   190
    }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   191
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   192
    /**
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   193
     * Opens an {@code InputStream} for reading the named entry of the given
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   194
     * section in this jmod file.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   195
     *
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   196
     * @throws IOException if the named entry is not found, or I/O error
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   197
     *         occurs when reading it
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   198
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   199
    public InputStream getInputStream(Section section, String name)
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   200
        throws IOException
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   201
    {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   202
        String entry = section.jmodDir() + "/" + name;
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   203
        ZipEntry e = zipfile.getEntry(entry);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   204
        if (e == null) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   205
            throw new IOException(name + " not found: " + file);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   206
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   207
        return zipfile.getInputStream(e);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   208
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   209
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   210
    /**
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   211
     * Opens an {@code InputStream} for reading an entry in the JMOD file.
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   212
     *
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   213
     * @throws IOException if an I/O error occurs
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   214
     */
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   215
    public InputStream getInputStream(Entry entry) throws IOException {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   216
        return zipfile.getInputStream(entry.zipEntry());
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   217
    }
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   218
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41561
diff changeset
   219
    /**
41352
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   220
     * Returns a stream of non-directory entries in this jmod file.
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   221
     */
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   222
    public Stream<Entry> stream() {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   223
        return zipfile.stream()
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   224
                      .filter(e -> !e.isDirectory())
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   225
                      .map(Entry::new);
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   226
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   227
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   228
    @Override
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   229
    public void close() throws IOException {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   230
        if (zipfile != null) {
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   231
            zipfile.close();
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   232
        }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   233
    }
f9844bad9052 8166860: Add magic number to jmod file
mchung
parents:
diff changeset
   234
}