jdk/src/java.base/share/classes/java/util/jar/JarFile.java
author psandoz
Tue, 08 Nov 2016 12:36:13 -0800
changeset 41957 d228d198335b
parent 40940 1413b2ff89e4
child 41958 ea78a8efc680
permissions -rw-r--r--
8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification Reviewed-by: mchung, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37785
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.SoftReference;
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
    30
import java.net.URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
    32
import java.util.stream.Stream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
    33
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.CodeSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.Certificate;
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
    37
import java.security.CodeSource;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    38
import jdk.internal.misc.SharedSecrets;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
    39
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.util.ManifestEntryVerifier;
22578
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
    41
import sun.security.util.SignatureFileVerifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
    44
 * The {@code JarFile} class is used to read the contents of a jar file
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
    45
 * from any file that can be opened with {@code java.io.RandomAccessFile}.
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
    46
 * It extends the class {@code java.util.zip.ZipFile} with support
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    47
 * for reading an optional {@code Manifest} entry, and support for
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    48
 * processing multi-release jar files.  The {@code Manifest} can be used
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    49
 * to specify meta-information about the jar file and its entries.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    50
 *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    51
 * <p>A multi-release jar file is a jar file that contains
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    52
 * a manifest with a main attribute named "Multi-Release",
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    53
 * a set of "base" entries, some of which are public classes with public
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    54
 * or protected methods that comprise the public interface of the jar file,
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    55
 * and a set of "versioned" entries contained in subdirectories of the
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    56
 * "META-INF/versions" directory.  The versioned entries are partitioned by the
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    57
 * major version of the Java platform.  A versioned entry, with a version
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    58
 * {@code n}, {@code 8 < n}, in the "META-INF/versions/{n}" directory overrides
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    59
 * the base entry as well as any entry with a version number {@code i} where
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    60
 * {@code 8 < i < n}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    61
 *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    62
 * <p>By default, a {@code JarFile} for a multi-release jar file is configured
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    63
 * to process the multi-release jar file as if it were a plain (unversioned) jar
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    64
 * file, and as such an entry name is associated with at most one base entry.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    65
 * The {@code JarFile} may be configured to process a multi-release jar file by
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    66
 * creating the {@code JarFile} with the
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
    67
 * {@link JarFile#JarFile(File, boolean, int, Runtime.Version)} constructor.  The
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
    68
 * {@code Runtime.Version} object sets a maximum version used when searching for
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    69
 * versioned entries.  When so configured, an entry name
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    70
 * can correspond with at most one base entry and zero or more versioned
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    71
 * entries. A search is required to associate the entry name with the latest
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    72
 * versioned entry whose version is less than or equal to the maximum version
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    73
 * (see {@link #getEntry(String)}).
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    74
 *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    75
 * <p>Class loaders that utilize {@code JarFile} to load classes from the
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    76
 * contents of {@code JarFile} entries should construct the {@code JarFile}
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
    77
 * by invoking the {@link JarFile#JarFile(File, boolean, int, Runtime.Version)}
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
    78
 * constructor with the value {@code Runtime.version()} assigned to the last
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    79
 * argument.  This assures that classes compatible with the major
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    80
 * version of the running JVM are loaded from multi-release jar files.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    81
 *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    82
 * <p>If the verify flag is on when opening a signed jar file, the content of
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    83
 * the file is verified against its signature embedded inside the file. Please
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    84
 * note that the verification process does not include validating the signer's
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    85
 * certificate. A caller should inspect the return value of
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    86
 * {@link JarEntry#getCodeSigners()} to further determine if the signature
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    87
 * can be trusted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
    89
 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    93
 * @implNote
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    94
 * <div class="block">
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    95
 * If the API can not be used to configure a {@code JarFile} (e.g. to override
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    96
 * the configuration of a compiled application or library), two {@code System}
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    97
 * properties are available.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    98
 * <ul>
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
    99
 * <li>
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   100
 * {@code jdk.util.jar.version} can be assigned a value that is the
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   101
 * {@code String} representation of a non-negative integer
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   102
 * {@code <= Runtime.version().major()}.  The value is used to set the effective
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   103
 * runtime version to something other than the default value obtained by
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   104
 * evaluating {@code Runtime.version().major()}. The effective runtime version
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   105
 * is the version that the {@link JarFile#JarFile(File, boolean, int, Runtime.Version)}
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   106
 * constructor uses when the value of the last argument is
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   107
 * {@code JarFile.runtimeVersion()}.
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   108
 * </li>
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   109
 * <li>
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   110
 * {@code jdk.util.jar.enableMultiRelease} can be assigned one of the three
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   111
 * {@code String} values <em>true</em>, <em>false</em>, or <em>force</em>.  The
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   112
 * value <em>true</em>, the default value, enables multi-release jar file
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   113
 * processing.  The value <em>false</em> disables multi-release jar processing,
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   114
 * ignoring the "Multi-Release" manifest attribute, and the versioned
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   115
 * directories in a multi-release jar file if they exist.  Furthermore,
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   116
 * the method {@link JarFile#isMultiRelease()} returns <em>false</em>. The value
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   117
 * <em>force</em> causes the {@code JarFile} to be initialized to runtime
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   118
 * versioning after construction.  It effectively does the same as this code:
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   119
 * {@code (new JarFile(File, boolean, int, JarFile.runtimeVersion())}.
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   120
 * </li>
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   121
 * </ul>
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   122
 * </div>
22316
5668a8abf212 8024659: Clarify JarFile API
weijun
parents: 19409
diff changeset
   123
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see     Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see     java.util.zip.ZipFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @see     java.util.jar.JarEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
class JarFile extends ZipFile {
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   132
    private final static Runtime.Version BASE_VERSION;
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   133
    private final static int BASE_VERSION_MAJOR;
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   134
    private final static Runtime.Version RUNTIME_VERSION;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   135
    private final static boolean MULTI_RELEASE_ENABLED;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   136
    private final static boolean MULTI_RELEASE_FORCED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private SoftReference<Manifest> manRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private JarEntry manEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private JarVerifier jv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private boolean jvInitialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private boolean verify;
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   142
    private final Runtime.Version version;  // current version
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   143
    private final int versionMajor;         // version.major()
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   144
    private boolean isMultiRelease;         // is jar multi-release?
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   145
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   146
    // indicates if Class-Path attribute present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private boolean hasClassPathAttribute;
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   148
    // true if manifest checked for special attributes
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   149
    private volatile boolean hasCheckedSpecialAttributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   152
        // Set up JavaUtilJarAccess in SharedSecrets
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        SharedSecrets.setJavaUtilJarAccess(new JavaUtilJarAccessImpl());
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   154
        // multi-release jar file versions >= 9
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   155
        BASE_VERSION = Runtime.Version.parse(Integer.toString(8));
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   156
        BASE_VERSION_MAJOR = BASE_VERSION.major();
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   157
        String jarVersion = GetPropertyAction.privilegedGetProperty("jdk.util.jar.version");
38432
892603099bb0 8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents: 37785
diff changeset
   158
        int runtimeVersion = Runtime.version().major();
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   159
        if (jarVersion != null) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   160
            int jarVer = Integer.parseInt(jarVersion);
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   161
            runtimeVersion = (jarVer > runtimeVersion)
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   162
                    ? runtimeVersion
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   163
                    : Math.max(jarVer, BASE_VERSION_MAJOR);
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   164
        }
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   165
        RUNTIME_VERSION = Runtime.Version.parse(Integer.toString(runtimeVersion));
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37308
diff changeset
   166
        String enableMultiRelease = GetPropertyAction
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   167
                .privilegedGetProperty("jdk.util.jar.enableMultiRelease", "true");
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   168
        switch (enableMultiRelease) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   169
            case "true":
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   170
            default:
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   171
                MULTI_RELEASE_ENABLED = true;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   172
                MULTI_RELEASE_FORCED = false;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   173
                break;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   174
            case "false":
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   175
                MULTI_RELEASE_ENABLED = false;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   176
                MULTI_RELEASE_FORCED = false;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   177
                break;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   178
            case "force":
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   179
                MULTI_RELEASE_ENABLED = true;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   180
                MULTI_RELEASE_FORCED = true;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   181
                break;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   182
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   185
    private static final String META_INF = "META-INF/";
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   186
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   187
    private static final String META_INF_VERSIONS = META_INF + "versions/";
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   188
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   189
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * The JAR manifest file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   192
    public static final String MANIFEST_NAME = META_INF + "MANIFEST.MF";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
41957
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   195
     * Returns the version that represents the unversioned configuration of a
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   196
     * multi-release jar file.
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   197
     *
41957
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   198
     * @return the version that represents the unversioned configuration
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   199
     *
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   200
     * @since 9
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   201
     */
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   202
    public static Runtime.Version baseVersion() {
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   203
        return BASE_VERSION;
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   204
    }
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   205
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   206
    /**
41957
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   207
     * Returns the version that represents the effective runtime versioned
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   208
     * configuration of a multi-release jar file.
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   209
     * <p>
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   210
     * By default the major version number of the returned {@code Version} will
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   211
     * be equal to the major version number of {@code Runtime.version()}.
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   212
     * However, if the {@code jdk.util.jar.version} property is set, the
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   213
     * returned {@code Version} is derived from that property and major version
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   214
     * numbers may not be equal.
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   215
     *
41957
d228d198335b 8165782: java.util.jar.JarFile.runtimeVersion() spec needs clarification
psandoz
parents: 40940
diff changeset
   216
     * @return the version that represents the runtime versioned configuration
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   217
     *
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   218
     * @since 9
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   219
     */
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   220
    public static Runtime.Version runtimeVersion() {
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   221
        return RUNTIME_VERSION;
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   222
    }
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   223
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   224
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   225
     * Creates a new {@code JarFile} to read from the specified
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   226
     * file {@code name}. The {@code JarFile} will be verified if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * it is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param name the name of the jar file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @throws SecurityException if access to the file is denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *         by the SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public JarFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this(new File(name), true, ZipFile.OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   238
     * Creates a new {@code JarFile} to read from the specified
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   239
     * file {@code name}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param name the name of the jar file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param verify whether or not to verify the jar file if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * it is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @throws SecurityException if access to the file is denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         by the SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public JarFile(String name, boolean verify) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        this(new File(name), verify, ZipFile.OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   252
     * Creates a new {@code JarFile} to read from the specified
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   253
     * {@code File} object. The {@code JarFile} will be verified if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * it is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param file the jar file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @throws SecurityException if access to the file is denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         by the SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public JarFile(File file) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        this(file, true, ZipFile.OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   265
     * Creates a new {@code JarFile} to read from the specified
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   266
     * {@code File} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param file the jar file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param verify whether or not to verify the jar file if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * it is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws SecurityException if access to the file is denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *         by the SecurityManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public JarFile(File file, boolean verify) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        this(file, verify, ZipFile.OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   279
     * Creates a new {@code JarFile} to read from the specified
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   280
     * {@code File} object in the specified mode.  The mode argument
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   281
     * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param file the jar file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param verify whether or not to verify the jar file if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * it is signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws IllegalArgumentException
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   289
     *         if the {@code mode} argument is invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @throws SecurityException if access to the file is denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *         by the SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public JarFile(File file, boolean verify, int mode) throws IOException {
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   295
        this(file, verify, mode, BASE_VERSION);
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   296
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   297
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   298
    /**
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   299
     * Creates a new {@code JarFile} to read from the specified
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   300
     * {@code File} object in the specified mode.  The mode argument
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   301
     * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   302
     * The version argument, after being converted to a canonical form, is
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   303
     * used to configure the {@code JarFile} for processing
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   304
     * multi-release jar files.
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   305
     * <p>
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   306
     * The canonical form derived from the version parameter is
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   307
     * {@code Runtime.Version.parse(Integer.toString(n))} where {@code n} is
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   308
     * {@code Math.max(version.major(), JarFile.baseVersion().major())}.
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   309
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   310
     * @param file the jar file to be opened for reading
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   311
     * @param verify whether or not to verify the jar file if
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   312
     * it is signed.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   313
     * @param mode the mode in which the file is to be opened
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   314
     * @param version specifies the release version for a multi-release jar file
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   315
     * @throws IOException if an I/O error has occurred
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   316
     * @throws IllegalArgumentException
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   317
     *         if the {@code mode} argument is invalid
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   318
     * @throws SecurityException if access to the file is denied
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   319
     *         by the SecurityManager
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   320
     * @throws NullPointerException if {@code version} is {@code null}
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   321
     * @since 9
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   322
     */
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   323
    public JarFile(File file, boolean verify, int mode, Runtime.Version version) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        super(file, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        this.verify = verify;
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   326
        Objects.requireNonNull(version);
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   327
        if (MULTI_RELEASE_FORCED || version.major() == RUNTIME_VERSION.major()) {
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   328
            // This deals with the common case where the value from JarFile.runtimeVersion() is passed
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   329
            this.version = RUNTIME_VERSION;
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   330
        } else if (version.major() <= BASE_VERSION_MAJOR) {
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   331
            // This also deals with the common case where the value from JarFile.baseVersion() is passed
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   332
            this.version = BASE_VERSION;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   333
        } else {
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   334
            // Canonicalize
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   335
            this.version = Runtime.Version.parse(Integer.toString(version.major()));
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   336
        }
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   337
        this.versionMajor = this.version.major();
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   338
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   339
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   340
    /**
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   341
     * Returns the maximum version used when searching for versioned entries.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   342
     *
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   343
     * @return the maximum version
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   344
     * @since 9
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   345
     */
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   346
    public final Runtime.Version getVersion() {
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   347
        return isMultiRelease() ? this.version : BASE_VERSION;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   348
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   349
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   350
    /**
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   351
     * Indicates whether or not this jar file is a multi-release jar file.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   352
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   353
     * @return true if this JarFile is a multi-release jar file
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   354
     * @since 9
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   355
     */
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   356
    public final boolean isMultiRelease() {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   357
        if (isMultiRelease) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   358
            return true;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   359
        }
40539
12bb7768ae6e 8164585: JarFile::isMultiRelease does not return true in all cases where it should return true
sdrach
parents: 39758
diff changeset
   360
        if (MULTI_RELEASE_ENABLED) {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   361
            try {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   362
                checkForSpecialAttributes();
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   363
            } catch (IOException io) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   364
                isMultiRelease = false;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   365
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   366
        }
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   367
        return isMultiRelease;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   368
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   369
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   371
     * Returns the jar file manifest, or {@code null} if none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   373
     * @return the jar file manifest, or {@code null} if none
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *         may be thrown if the jar file has been closed
19047
08210fe86260 8021408: Fix misc doclint issues in java.util and java.io
darcy
parents: 18822
diff changeset
   377
     * @throws IOException  if an I/O error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public Manifest getManifest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return getManifestFromReference();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private Manifest getManifestFromReference() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        Manifest man = manRef != null ? manRef.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (man == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            JarEntry manEntry = getManEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // If found then load the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (manEntry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    byte[] b = getBytes(manEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    man = new Manifest(new ByteArrayInputStream(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    if (!jvInitialized) {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   396
                        jv = new JarVerifier(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    man = new Manifest(super.getInputStream(manEntry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
   401
                manRef = new SoftReference<>(man);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return man;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
34686
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   407
    private String[] getMetaInfEntryNames() {
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   408
        return jdk.internal.misc.SharedSecrets.getJavaUtilZipFileAccess()
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   409
                                              .getMetaInfEntryNames((ZipFile)this);
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   410
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   413
     * Returns the {@code JarEntry} for the given base entry name or
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   414
     * {@code null} if not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   416
     * <p>If this {@code JarFile} is a multi-release jar file and is configured
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   417
     * to be processed as such, then a search is performed to find and return
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   418
     * a {@code JarEntry} that is the latest versioned entry associated with the
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   419
     * given entry name.  The returned {@code JarEntry} is the versioned entry
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   420
     * corresponding to the given base entry name prefixed with the string
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   421
     * {@code "META-INF/versions/{n}/"}, for the largest value of {@code n} for
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   422
     * which an entry exists.  If such a versioned entry does not exist, then
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   423
     * the {@code JarEntry} for the base entry is returned, otherwise
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   424
     * {@code null} is returned if no entries are found.  The initial value for
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   425
     * the version {@code n} is the maximum version as returned by the method
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   426
     * {@link JarFile#getVersion()}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   427
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param name the jar file entry name
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   429
     * @return the {@code JarEntry} for the given entry name, or
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   430
     *         the versioned entry name, or {@code null} if not found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *         may be thrown if the jar file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see java.util.jar.JarEntry
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   436
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   437
     * @implSpec
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   438
     * <div class="block">
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   439
     * This implementation invokes {@link JarFile#getEntry(String)}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   440
     * </div>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public JarEntry getJarEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return (JarEntry)getEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   447
     * Returns the {@code ZipEntry} for the given base entry name or
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   448
     * {@code null} if not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   450
     * <p>If this {@code JarFile} is a multi-release jar file and is configured
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   451
     * to be processed as such, then a search is performed to find and return
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   452
     * a {@code ZipEntry} that is the latest versioned entry associated with the
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   453
     * given entry name.  The returned {@code ZipEntry} is the versioned entry
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   454
     * corresponding to the given base entry name prefixed with the string
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   455
     * {@code "META-INF/versions/{n}/"}, for the largest value of {@code n} for
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   456
     * which an entry exists.  If such a versioned entry does not exist, then
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   457
     * the {@code ZipEntry} for the base entry is returned, otherwise
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   458
     * {@code null} is returned if no entries are found.  The initial value for
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   459
     * the version {@code n} is the maximum version as returned by the method
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   460
     * {@link JarFile#getVersion()}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   461
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param name the jar file entry name
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 31469
diff changeset
   463
     * @return the {@code ZipEntry} for the given entry name or
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   464
     *         the versioned entry name or {@code null} if not found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *         may be thrown if the jar file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @see java.util.zip.ZipEntry
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   470
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   471
     * @implSpec
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   472
     * <div class="block">
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   473
     * This implementation may return a versioned entry for the requested name
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   474
     * even if there is not a corresponding base entry.  This can occur
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   475
     * if there is a private or package-private versioned entry that matches.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   476
     * If a subclass overrides this method, assure that the override method
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   477
     * invokes {@code super.getEntry(name)} to obtain all versioned entries.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   478
     * </div>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public ZipEntry getEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        ZipEntry ze = super.getEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (ze != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return new JarFileEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   485
        // no matching base entry, but maybe there is a versioned entry,
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   486
        // like a new private class
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   487
        if (isMultiRelease()) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   488
            ze = new ZipEntry(name);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   489
            ZipEntry vze = getVersionedEntry(ze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   490
            if (ze != vze) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   491
                return new JarFileEntry(name, vze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   492
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   493
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   497
    private class JarEntryIterator implements Enumeration<JarEntry>,
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   498
            Iterator<JarEntry>
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   499
    {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   500
        final Enumeration<? extends ZipEntry> e = JarFile.super.entries();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   501
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   502
        public boolean hasNext() {
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   503
            return e.hasMoreElements();
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   504
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   505
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   506
        public JarEntry next() {
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   507
            ZipEntry ze = e.nextElement();
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   508
            return new JarFileEntry(ze.getName(), ze);
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   509
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   510
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   511
        public boolean hasMoreElements() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   512
            return hasNext();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   513
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   514
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   515
        public JarEntry nextElement() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   516
            return next();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   517
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   518
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   519
        public Iterator<JarEntry> asIterator() {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   520
            return this;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   521
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   522
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   523
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   525
     * Returns an enumeration of the jar file entries.
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   526
     *
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   527
     * @return an enumeration of the jar file entries
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   528
     * @throws IllegalStateException
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   529
     *         may be thrown if the jar file has been closed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public Enumeration<JarEntry> entries() {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   532
        return new JarEntryIterator();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   533
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   534
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   535
    /**
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   536
     * Returns an ordered {@code Stream} over the jar file entries.
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   537
     * Entries appear in the {@code Stream} in the order they appear in
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   538
     * the central directory of the jar file.
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   539
     *
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   540
     * @return an ordered {@code Stream} of entries in this jar file
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   541
     * @throws IllegalStateException if the jar file has been closed
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   542
     * @since 1.8
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   543
     */
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   544
    public Stream<JarEntry> stream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   545
        return StreamSupport.stream(Spliterators.spliterator(
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   546
                new JarEntryIterator(), size(),
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   547
                Spliterator.ORDERED | Spliterator.DISTINCT |
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 17421
diff changeset
   548
                        Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   551
    private ZipEntry searchForVersionedEntry(final int version, String name) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   552
        ZipEntry vze = null;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   553
        String sname = "/" + name;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   554
        int i = version;
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   555
        while (i > BASE_VERSION_MAJOR) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   556
            vze = super.getEntry(META_INF_VERSIONS + i + sname);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   557
            if (vze != null) break;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   558
            i--;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   559
        }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   560
        return vze;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   561
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   562
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   563
    private ZipEntry getVersionedEntry(ZipEntry ze) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   564
        ZipEntry vze = null;
40940
1413b2ff89e4 8163798: Create a JarFile versionedStream method
sdrach
parents: 40812
diff changeset
   565
        if (BASE_VERSION_MAJOR < versionMajor) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   566
            String name = ze.getName();
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   567
            if (!name.startsWith(META_INF)) {
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   568
                vze = searchForVersionedEntry(versionMajor, name);
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   569
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   570
        }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   571
        return vze == null ? ze : vze;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   572
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   573
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   574
    /**
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   575
     * Returns the real name of a {@code JarEntry}.  If this {@code JarFile} is
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   576
     * a multi-release jar file and is configured to be processed as such, the
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   577
     * name returned by this method is the path name of the versioned entry
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   578
     * that the {@code JarEntry} represents, rather than the path name of the
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   579
     * base entry that {@link JarEntry#getName()} returns.  If the
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   580
     * {@code JarEntry} does not represent a versioned entry, or the
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   581
     * jar file is not a multi-release jar file or {@code JarFile} is not
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   582
     * configured for processing a multi-release jar file, this method returns
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   583
     * the same name that {@link JarEntry#getName()} returns.
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   584
     *
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   585
     * @param entry the JarEntry
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   586
     * @return the real name of the JarEntry
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   587
     * @since 9
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   588
     */
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   589
    String getRealName(JarEntry entry) {
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   590
        if (entry instanceof JarFileEntry) {
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   591
            return ((JarFileEntry)entry).realName();
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   592
        }
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   593
        return entry.getName();
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   594
    }
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   595
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    private class JarFileEntry extends JarEntry {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   597
        final private String name;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   598
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        JarFileEntry(ZipEntry ze) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   600
            super(isMultiRelease() ? getVersionedEntry(ze) : ze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   601
            this.name = ze.getName();
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   602
        }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   603
        JarFileEntry(String name, ZipEntry vze) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   604
            super(vze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   605
            this.name = name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        public Attributes getAttributes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Manifest man = JarFile.this.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (man != null) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   610
                return man.getAttributes(super.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        public Certificate[] getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                maybeInstantiateVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (certs == null && jv != null) {
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   622
                certs = jv.getCerts(JarFile.this, realEntry());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return certs == null ? null : certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        public CodeSigner[] getCodeSigners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                maybeInstantiateVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            if (signers == null && jv != null) {
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   633
                signers = jv.getCodeSigners(JarFile.this, realEntry());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            return signers == null ? null : signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   637
        JarFileEntry realEntry() {
40539
12bb7768ae6e 8164585: JarFile::isMultiRelease does not return true in all cases where it should return true
sdrach
parents: 39758
diff changeset
   638
            if (isMultiRelease() && versionMajor != BASE_VERSION_MAJOR) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   639
                String entryName = super.getName();
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   640
                return entryName.equals(this.name) ? this : new JarFileEntry(entryName, this);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   641
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   642
            return this;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   643
        }
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   644
        String realName() {
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   645
            return super.getName();
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   646
        }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   647
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   648
        @Override
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   649
        public String getName() {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   650
            return name;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   651
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Ensures that the JarVerifier has been created if one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * necessary (i.e., the jar appears to be signed.) This is done as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * a quick check to avoid processing of the manifest for unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * jars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    private void maybeInstantiateVerifier() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (jv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            String[] names = getMetaInfEntryNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (names != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   668
                for (String nameLower : names) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   669
                    String name = nameLower.toUpperCase(Locale.ENGLISH);
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   670
                    if (name.endsWith(".DSA") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   671
                        name.endsWith(".RSA") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   672
                        name.endsWith(".EC") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   673
                        name.endsWith(".SF")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        // Assume since we found a signature-related file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        // that the jar is signed and that we therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        // need a JarVerifier and Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            // No signature-related files; don't instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            // verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            verify = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * Initializes the verifier object by reading all the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * entries and passing them to the verifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    private void initializeVerifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        ManifestEntryVerifier mev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        // Verify "META-INF/" entries...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            String[] names = getMetaInfEntryNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (names != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   700
                for (String name : names) {
22578
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   701
                    String uname = name.toUpperCase(Locale.ENGLISH);
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   702
                    if (MANIFEST_NAME.equals(uname)
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   703
                            || SignatureFileVerifier.isBlockOrSF(uname)) {
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   704
                        JarEntry e = getJarEntry(name);
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   705
                        if (e == null) {
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   706
                            throw new JarException("corrupted jar file");
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   707
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        if (mev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                            mev = new ManifestEntryVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                (getManifestFromReference());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        }
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   712
                        byte[] b = getBytes(e);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   713
                        if (b != null && b.length > 0) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   714
                            jv.beginEntry(e, mev);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   715
                            jv.update(b.length, b, 0, b.length, mev);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   716
                            jv.update(-1, null, 0, 0, mev);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   717
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            // if we had an error parsing any blocks, just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // treat the jar file as being unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            jv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            verify = false;
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   726
            if (JarVerifier.debug != null) {
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   727
                JarVerifier.debug.println("jarfile parsing error!");
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   728
                ex.printStackTrace();
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   729
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        // if after initializing the verifier we have nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        // signed, we null it out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (jv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            jv.doneWithMeta();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (JarVerifier.debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                JarVerifier.debug.println("done with meta!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (jv.nothingToVerify()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                if (JarVerifier.debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    JarVerifier.debug.println("nothing to verify!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                jv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                verify = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Reads all the bytes for a given entry. Used to process the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * META-INF files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    private byte[] getBytes(ZipEntry ze) throws IOException {
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   757
        try (InputStream is = super.getInputStream(ze)) {
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 32834
diff changeset
   758
            int len = (int)ze.getSize();
33235
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   759
            int bytesRead;
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   760
            byte[] b;
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   761
            // trust specified entry sizes when reasonably small
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   762
            if (len != -1 && len <= 65535) {
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   763
                b = new byte[len];
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   764
                bytesRead = is.readNBytes(b, 0, len);
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   765
            } else {
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   766
                b = is.readAllBytes();
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   767
                bytesRead = b.length;
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   768
            }
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   769
            if (len != -1 && len != bytesRead) {
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   770
                throw new EOFException("Expected:" + len + ", read:" + bytesRead);
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   771
            }
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 32834
diff changeset
   772
            return b;
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 8387
diff changeset
   773
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @param ze the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @return an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *         zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws ZipException if a zip file format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @throws SecurityException if any of the jar file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *         are incorrectly signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *         may be thrown if the jar file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public synchronized InputStream getInputStream(ZipEntry ze)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        maybeInstantiateVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (jv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            return super.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (!jvInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            initializeVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            jvInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            // could be set to null after a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // initializeVerifier if we have nothing to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            // verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (jv == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                return super.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // wrap a verifier stream around the real stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        return new JarVerifier.VerifierStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            getManifestFromReference(),
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   809
            verifiableEntry(ze),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            super.getInputStream(ze),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            jv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   814
    private JarEntry verifiableEntry(ZipEntry ze) {
36236
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   815
        if (ze instanceof JarFileEntry) {
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   816
            // assure the name and entry match for verification
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   817
            return ((JarFileEntry)ze).realEntry();
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   818
        }
36236
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   819
        ze = getJarEntry(ze.getName());
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   820
        if (ze instanceof JarFileEntry) {
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   821
            return ((JarFileEntry)ze).realEntry();
36236
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   822
        }
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   823
        return (JarEntry)ze;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   824
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   825
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   826
    // Statics for hand-coded Boyer-Moore search
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   827
    private static final byte[] CLASSPATH_CHARS =
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   828
            {'C','L','A','S','S','-','P','A','T','H', ':', ' '};
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   829
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   830
    // The bad character shift for "class-path: "
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   831
    private static final byte[] CLASSPATH_LASTOCC;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   832
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   833
    // The good suffix shift for "class-path: "
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   834
    private static final byte[] CLASSPATH_OPTOSFT;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   835
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   836
    private static final byte[] MULTIRELEASE_CHARS =
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   837
            {'M','U','L','T','I','-','R','E','L','E', 'A', 'S', 'E', ':',
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   838
                    ' ', 'T', 'R', 'U', 'E'};
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   839
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   840
    // The bad character shift for "multi-release: true"
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   841
    private static final byte[] MULTIRELEASE_LASTOCC;
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   842
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   843
    // The good suffix shift for "multi-release: true"
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   844
    private static final byte[] MULTIRELEASE_OPTOSFT;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   845
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    static {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   847
        CLASSPATH_LASTOCC = new byte[64];
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   848
        CLASSPATH_OPTOSFT = new byte[12];
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   849
        CLASSPATH_LASTOCC[(int)'C' - 32] = 1;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   850
        CLASSPATH_LASTOCC[(int)'L' - 32] = 2;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   851
        CLASSPATH_LASTOCC[(int)'S' - 32] = 5;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   852
        CLASSPATH_LASTOCC[(int)'-' - 32] = 6;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   853
        CLASSPATH_LASTOCC[(int)'P' - 32] = 7;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   854
        CLASSPATH_LASTOCC[(int)'A' - 32] = 8;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   855
        CLASSPATH_LASTOCC[(int)'T' - 32] = 9;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   856
        CLASSPATH_LASTOCC[(int)'H' - 32] = 10;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   857
        CLASSPATH_LASTOCC[(int)':' - 32] = 11;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   858
        CLASSPATH_LASTOCC[(int)' ' - 32] = 12;
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   859
        for (int i = 0; i < 11; i++) {
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   860
            CLASSPATH_OPTOSFT[i] = 12;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   861
        }
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   862
        CLASSPATH_OPTOSFT[11] = 1;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   863
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   864
        MULTIRELEASE_LASTOCC = new byte[64];
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   865
        MULTIRELEASE_OPTOSFT = new byte[19];
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   866
        MULTIRELEASE_LASTOCC[(int)'M' - 32] = 1;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   867
        MULTIRELEASE_LASTOCC[(int)'I' - 32] = 5;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   868
        MULTIRELEASE_LASTOCC[(int)'-' - 32] = 6;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   869
        MULTIRELEASE_LASTOCC[(int)'L' - 32] = 9;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   870
        MULTIRELEASE_LASTOCC[(int)'A' - 32] = 11;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   871
        MULTIRELEASE_LASTOCC[(int)'S' - 32] = 12;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   872
        MULTIRELEASE_LASTOCC[(int)':' - 32] = 14;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   873
        MULTIRELEASE_LASTOCC[(int)' ' - 32] = 15;
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   874
        MULTIRELEASE_LASTOCC[(int)'T' - 32] = 16;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   875
        MULTIRELEASE_LASTOCC[(int)'R' - 32] = 17;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   876
        MULTIRELEASE_LASTOCC[(int)'U' - 32] = 18;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   877
        MULTIRELEASE_LASTOCC[(int)'E' - 32] = 19;
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   878
        for (int i = 0; i < 17; i++) {
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   879
            MULTIRELEASE_OPTOSFT[i] = 19;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   880
        }
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   881
        MULTIRELEASE_OPTOSFT[17] = 6;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   882
        MULTIRELEASE_OPTOSFT[18] = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    private JarEntry getManEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (manEntry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            // First look up manifest entry using standard name
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   888
            ZipEntry manEntry = super.getEntry(MANIFEST_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            if (manEntry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                // If not found, then iterate through all the "META-INF/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                // entries to find a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                String[] names = getMetaInfEntryNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                if (names != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   894
                    for (String name : names) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   895
                        if (MANIFEST_NAME.equals(name.toUpperCase(Locale.ENGLISH))) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   896
                            manEntry = super.getEntry(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   902
            this.manEntry = (manEntry == null)
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   903
                    ? null
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   904
                    : new JarFileEntry(manEntry.getName(), manEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        return manEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   909
   /**
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   910
    * Returns {@code true} iff this JAR file has a manifest with the
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   911
    * Class-Path attribute
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   912
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    boolean hasClassPathAttribute() throws IOException {
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   914
        checkForSpecialAttributes();
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   915
        return hasClassPathAttribute;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   916
    }
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   917
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   918
    /**
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   919
     * Returns true if the pattern {@code src} is found in {@code b}.
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   920
     * The {@code lastOcc} array is the precomputed bad character shifts.
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   921
     * Since there are no repeated substring in our search strings,
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   922
     * the good suffix shifts can be replaced with a comparison.
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   923
     */
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   924
    private int match(byte[] src, byte[] b, byte[] lastOcc, byte[] optoSft) {
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   925
        int len = src.length;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   926
        int last = b.length - len;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   927
        int i = 0;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   928
        next:
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   929
        while (i <= last) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   930
            for (int j = (len - 1); j >= 0; j--) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   931
                byte c = b[i + j];
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   932
                if (c >= ' ' && c <= 'z') {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   933
                    if (c >= 'a') c -= 32; // Canonicalize
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   934
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   935
                    if (c != src[j]) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   936
                        // no match
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   937
                        int badShift = lastOcc[c - 32];
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   938
                        i += Math.max(j + 1 - badShift, optoSft[j]);
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   939
                        continue next;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   940
                    }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   941
                } else {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   942
                    // no match, character not valid for name
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   943
                    i += len;
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   944
                    continue next;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   945
                }
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   946
            }
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   947
            return i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   949
        return -1;
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   950
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   952
    /**
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   953
     * On first invocation, check if the JAR file has the Class-Path
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   954
     * and the Multi-Release attribute. A no-op on subsequent calls.
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   955
     */
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   956
    private void checkForSpecialAttributes() throws IOException {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   957
        if (hasCheckedSpecialAttributes) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   958
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        }
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   960
        synchronized (this) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   961
            if (hasCheckedSpecialAttributes) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   962
                return;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   963
            }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   964
            JarEntry manEntry = getManEntry();
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   965
            if (manEntry != null) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   966
                byte[] b = getBytes(manEntry);
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   967
                hasClassPathAttribute = match(CLASSPATH_CHARS, b,
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   968
                        CLASSPATH_LASTOCC, CLASSPATH_OPTOSFT) != -1;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   969
                // is this a multi-release jar file
40539
12bb7768ae6e 8164585: JarFile::isMultiRelease does not return true in all cases where it should return true
sdrach
parents: 39758
diff changeset
   970
                if (MULTI_RELEASE_ENABLED) {
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   971
                    int i = match(MULTIRELEASE_CHARS, b, MULTIRELEASE_LASTOCC,
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   972
                            MULTIRELEASE_OPTOSFT);
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   973
                    if (i != -1) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   974
                        i += MULTIRELEASE_CHARS.length;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   975
                        if (i < b.length) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   976
                            byte c = b[i++];
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   977
                            // Check that the value is followed by a newline
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   978
                            // and does not have a continuation
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   979
                            if (c == '\n' &&
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   980
                                    (i == b.length || b[i] != ' ')) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   981
                                isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   982
                            } else if (c == '\r') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   983
                                if (i == b.length) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   984
                                    isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   985
                                } else {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   986
                                    c = b[i++];
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   987
                                    if (c == '\n') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   988
                                        if (i == b.length || b[i] != ' ') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   989
                                            isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   990
                                        }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   991
                                    } else if (c != ' ') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   992
                                        isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   993
                                    }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   994
                                }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   995
                            }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   996
                        }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   997
                    }
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   998
                }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   999
            }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
  1000
            hasCheckedSpecialAttributes = true;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
  1001
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1004
    private synchronized void ensureInitialization() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1005
        try {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1006
            maybeInstantiateVerifier();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1007
        } catch (IOException e) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1008
            throw new RuntimeException(e);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1009
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1010
        if (jv != null && !jvInitialized) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1011
            initializeVerifier();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1012
            jvInitialized = true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1013
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1014
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1015
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1016
    JarEntry newEntry(ZipEntry ze) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1017
        return new JarFileEntry(ze);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1018
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1019
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1020
    Enumeration<String> entryNames(CodeSource[] cs) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1021
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1022
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1023
            return jv.entryNames(this, cs);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1024
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1025
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1026
        /*
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1027
         * JAR file has no signed content. Is there a non-signing
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1028
         * code source?
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1029
         */
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1030
        boolean includeUnsigned = false;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
  1031
        for (CodeSource c : cs) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
  1032
            if (c.getCodeSigners() == null) {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1033
                includeUnsigned = true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1034
                break;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1035
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1036
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1037
        if (includeUnsigned) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1038
            return unsignedEntryNames();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1039
        } else {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
  1040
            return new Enumeration<>() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1041
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1042
                public boolean hasMoreElements() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1043
                    return false;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1044
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1045
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1046
                public String nextElement() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1047
                    throw new NoSuchElementException();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1048
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1049
            };
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1050
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1051
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1052
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1053
    /**
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1054
     * Returns an enumeration of the zip file entries
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1055
     * excluding internal JAR mechanism entries and including
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1056
     * signed entries missing from the ZIP directory.
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1057
     */
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1058
    Enumeration<JarEntry> entries2() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1059
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1060
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1061
            return jv.entries2(this, super.entries());
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1062
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1063
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1064
        // screen out entries which are never signed
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1065
        final Enumeration<? extends ZipEntry> enum_ = super.entries();
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
  1066
        return new Enumeration<>() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1067
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1068
            ZipEntry entry;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1069
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1070
            public boolean hasMoreElements() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1071
                if (entry != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1072
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1073
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1074
                while (enum_.hasMoreElements()) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1075
                    ZipEntry ze = enum_.nextElement();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1076
                    if (JarVerifier.isSigningRelated(ze.getName())) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1077
                        continue;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1078
                    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1079
                    entry = ze;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1080
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1081
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1082
                return false;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1083
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1084
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1085
            public JarFileEntry nextElement() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1086
                if (hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1087
                    ZipEntry ze = entry;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1088
                    entry = null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1089
                    return new JarFileEntry(ze);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1090
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1091
                throw new NoSuchElementException();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1092
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1093
        };
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1094
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1095
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1096
    CodeSource[] getCodeSources(URL url) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1097
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1098
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1099
            return jv.getCodeSources(this, url);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1100
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1101
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1102
        /*
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1103
         * JAR file has no signed content. Is there a non-signing
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1104
         * code source?
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1105
         */
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1106
        Enumeration<String> unsigned = unsignedEntryNames();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1107
        if (unsigned.hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1108
            return new CodeSource[]{JarVerifier.getUnsignedCS(url)};
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1109
        } else {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1110
            return null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1111
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1112
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1113
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1114
    private Enumeration<String> unsignedEntryNames() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1115
        final Enumeration<JarEntry> entries = entries();
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
  1116
        return new Enumeration<>() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1117
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1118
            String name;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1119
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1120
            /*
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1121
             * Grab entries from ZIP directory but screen out
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1122
             * metadata.
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1123
             */
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1124
            public boolean hasMoreElements() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1125
                if (name != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1126
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1127
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1128
                while (entries.hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1129
                    String value;
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1130
                    ZipEntry e = entries.nextElement();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1131
                    value = e.getName();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1132
                    if (e.isDirectory() || JarVerifier.isSigningRelated(value)) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1133
                        continue;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1134
                    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1135
                    name = value;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1136
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1137
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1138
                return false;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1139
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1140
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1141
            public String nextElement() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1142
                if (hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1143
                    String value = name;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1144
                    name = null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1145
                    return value;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1146
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1147
                throw new NoSuchElementException();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1148
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1149
        };
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1150
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1151
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1152
    CodeSource getCodeSource(URL url, String name) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1153
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1154
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1155
            if (jv.eagerValidation) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1156
                CodeSource cs = null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1157
                JarEntry je = getJarEntry(name);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1158
                if (je != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1159
                    cs = jv.getCodeSource(url, this, je);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1160
                } else {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1161
                    cs = jv.getCodeSource(url, name);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1162
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1163
                return cs;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1164
            } else {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1165
                return jv.getCodeSource(url, name);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1166
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1167
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1168
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1169
        return JarVerifier.getUnsignedCS(url);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1170
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1171
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1172
    void setEagerValidation(boolean eager) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1173
        try {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1174
            maybeInstantiateVerifier();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1175
        } catch (IOException e) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1176
            throw new RuntimeException(e);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1177
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1178
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1179
            jv.setEagerValidation(eager);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1180
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1181
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1182
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1183
    List<Object> getManifestDigests() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1184
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1185
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1186
            return jv.getManifestDigests();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1187
        }
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
  1188
        return new ArrayList<>();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1189
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
}