jdk/src/java.base/share/classes/java/util/jar/JarFile.java
author psandoz
Fri, 02 Dec 2016 10:05:53 -0800
changeset 42354 131a371f5efc
parent 41958 ea78a8efc680
child 44844 b2b4d98404ba
permissions -rw-r--r--
8166914: URLClassLoader spec needs to mention that it's MR-aware 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
 *
42354
131a371f5efc 8166914: URLClassLoader spec needs to mention that it's MR-aware
psandoz
parents: 41958
diff changeset
    51
 * <p><a name="multirelease">A multi-release jar file</a> is a jar file that
131a371f5efc 8166914: URLClassLoader spec needs to mention that it's MR-aware
psandoz
parents: 41958
diff changeset
    52
 * contains a manifest with a main attribute named "Multi-Release",
36129
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.
41958
ea78a8efc680 8166735: JarFile#getVersion spec clarification for unversioned jars
psandoz
parents: 41957
diff changeset
   342
     * <p>
ea78a8efc680 8166735: JarFile#getVersion spec clarification for unversioned jars
psandoz
parents: 41957
diff changeset
   343
     * If this {@code JarFile} is not a multi-release jar file or is not
ea78a8efc680 8166735: JarFile#getVersion spec clarification for unversioned jars
psandoz
parents: 41957
diff changeset
   344
     * configured to be processed as such, then the version returned will be the
ea78a8efc680 8166735: JarFile#getVersion spec clarification for unversioned jars
psandoz
parents: 41957
diff changeset
   345
     * same as that returned from {@link #baseVersion()}.
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   346
     *
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   347
     * @return the maximum version
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   348
     * @since 9
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   349
     */
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   350
    public final Runtime.Version getVersion() {
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   351
        return isMultiRelease() ? this.version : BASE_VERSION;
36129
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
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   354
    /**
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   355
     * 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
   356
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   357
     * @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
   358
     * @since 9
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   359
     */
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   360
    public final boolean isMultiRelease() {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   361
        if (isMultiRelease) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   362
            return true;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   363
        }
40539
12bb7768ae6e 8164585: JarFile::isMultiRelease does not return true in all cases where it should return true
sdrach
parents: 39758
diff changeset
   364
        if (MULTI_RELEASE_ENABLED) {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   365
            try {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   366
                checkForSpecialAttributes();
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   367
            } catch (IOException io) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   368
                isMultiRelease = false;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   369
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   370
        }
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   371
        return isMultiRelease;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   372
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   373
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
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
   375
     * Returns the jar file manifest, or {@code null} if none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
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
   377
     * @return the jar file manifest, or {@code null} if none
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *         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
   381
     * @throws IOException  if an I/O error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public Manifest getManifest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return getManifestFromReference();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private Manifest getManifestFromReference() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        Manifest man = manRef != null ? manRef.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (man == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            JarEntry manEntry = getManEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            // If found then load the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (manEntry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    byte[] b = getBytes(manEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    man = new Manifest(new ByteArrayInputStream(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    if (!jvInitialized) {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   400
                        jv = new JarVerifier(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    man = new Manifest(super.getInputStream(manEntry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
   405
                manRef = new SoftReference<>(man);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return man;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
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
   411
    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
   412
        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
   413
                                              .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
   414
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   417
     * 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
   418
     * {@code null} if not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   420
     * <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
   421
     * 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
   422
     * 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
   423
     * 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
   424
     * 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
   425
     * {@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
   426
     * 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
   427
     * 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
   428
     * {@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
   429
     * 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
   430
     * {@link JarFile#getVersion()}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   431
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @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
   433
     * @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
   434
     *         the versioned entry name, or {@code null} if not found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         may be thrown if the jar file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @see java.util.jar.JarEntry
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   440
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   441
     * @implSpec
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   442
     * <div class="block">
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   443
     * This implementation invokes {@link JarFile#getEntry(String)}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   444
     * </div>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public JarEntry getJarEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return (JarEntry)getEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   451
     * 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
   452
     * {@code null} if not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   454
     * <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
   455
     * 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
   456
     * 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
   457
     * 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
   458
     * 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
   459
     * {@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
   460
     * 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
   461
     * 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
   462
     * {@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
   463
     * 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
   464
     * {@link JarFile#getVersion()}.
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   465
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @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
   467
     * @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
   468
     *         the versioned entry name or {@code null} if not found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *         may be thrown if the jar file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @see java.util.zip.ZipEntry
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   474
     *
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   475
     * @implSpec
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   476
     * <div class="block">
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   477
     * 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
   478
     * 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
   479
     * 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
   480
     * 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
   481
     * 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
   482
     * </div>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public ZipEntry getEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        ZipEntry ze = super.getEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (ze != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return new JarFileEntry(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   489
        // 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
   490
        // like a new private class
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   491
        if (isMultiRelease()) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   492
            ze = new ZipEntry(name);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   493
            ZipEntry vze = getVersionedEntry(ze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   494
            if (ze != vze) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   495
                return new JarFileEntry(name, vze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   496
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   497
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   501
    private class JarEntryIterator implements Enumeration<JarEntry>,
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   502
            Iterator<JarEntry>
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   503
    {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   504
        final Enumeration<? extends ZipEntry> e = JarFile.super.entries();
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 boolean hasNext() {
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   507
            return e.hasMoreElements();
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   508
        }
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
        public JarEntry next() {
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   511
            ZipEntry ze = e.nextElement();
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   512
            return new JarFileEntry(ze.getName(), ze);
17421
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 boolean hasMoreElements() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   516
            return hasNext();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   517
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   518
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   519
        public JarEntry nextElement() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   520
            return next();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   521
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   522
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   523
        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
   524
            return this;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   525
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   526
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   527
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   529
     * 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
   530
     *
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   531
     * @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
   532
     * @throws IllegalStateException
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   533
     *         may be thrown if the jar file has been closed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public Enumeration<JarEntry> entries() {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   536
        return new JarEntryIterator();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   537
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   538
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   539
    /**
39758
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   540
     * 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
   541
     * 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
   542
     * the central directory of the jar file.
28c2c63fc09f 8157524: Revert JarFile methods "entries" and "stream" to Java 8 behavior
sdrach
parents: 39646
diff changeset
   543
     *
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   544
     * @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
   545
     * @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
   546
     * @since 1.8
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29986
diff changeset
   547
     */
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   548
    public Stream<JarEntry> stream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   549
        return StreamSupport.stream(Spliterators.spliterator(
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   550
                new JarEntryIterator(), size(),
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 16117
diff changeset
   551
                Spliterator.ORDERED | Spliterator.DISTINCT |
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 17421
diff changeset
   552
                        Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   555
    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
   556
        ZipEntry vze = null;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   557
        String sname = "/" + name;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   558
        int i = version;
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   559
        while (i > BASE_VERSION_MAJOR) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   560
            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
   561
            if (vze != null) break;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   562
            i--;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   563
        }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   564
        return vze;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   565
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   566
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   567
    private ZipEntry getVersionedEntry(ZipEntry ze) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   568
        ZipEntry vze = null;
40940
1413b2ff89e4 8163798: Create a JarFile versionedStream method
sdrach
parents: 40812
diff changeset
   569
        if (BASE_VERSION_MAJOR < versionMajor) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   570
            String name = ze.getName();
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   571
            if (!name.startsWith(META_INF)) {
39646
2bf99fe5023b 8150680: JarFile.Release enum needs reconsideration with respect to it's values
sdrach
parents: 38432
diff changeset
   572
                vze = searchForVersionedEntry(versionMajor, name);
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   573
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   574
        }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   575
        return vze == null ? ze : vze;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   576
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   577
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   578
    /**
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   579
     * 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
   580
     * 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
   581
     * 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
   582
     * 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
   583
     * 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
   584
     * {@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
   585
     * 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
   586
     * 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
   587
     * 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
   588
     *
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   589
     * @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
   590
     * @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
   591
     * @since 9
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
    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
   594
        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
   595
            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
   596
        }
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   597
        return entry.getName();
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   598
    }
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    private class JarFileEntry extends JarEntry {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   601
        final private String name;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   602
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        JarFileEntry(ZipEntry ze) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   604
            super(isMultiRelease() ? getVersionedEntry(ze) : ze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   605
            this.name = ze.getName();
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   606
        }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   607
        JarFileEntry(String name, ZipEntry vze) {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   608
            super(vze);
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   609
            this.name = name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        public Attributes getAttributes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            Manifest man = JarFile.this.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (man != null) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   614
                return man.getAttributes(super.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        public Certificate[] getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                maybeInstantiateVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            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
   626
                certs = jv.getCerts(JarFile.this, realEntry());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            return certs == null ? null : certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        public CodeSigner[] getCodeSigners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                maybeInstantiateVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            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
   637
                signers = jv.getCodeSigners(JarFile.this, realEntry());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return signers == null ? null : signers.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   641
        JarFileEntry realEntry() {
40539
12bb7768ae6e 8164585: JarFile::isMultiRelease does not return true in all cases where it should return true
sdrach
parents: 39758
diff changeset
   642
            if (isMultiRelease() && versionMajor != BASE_VERSION_MAJOR) {
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   643
                String entryName = super.getName();
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   644
                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
   645
            }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   646
            return this;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   647
        }
37785
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   648
        String realName() {
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   649
            return super.getName();
daf6dcc73e9d 8151542: URL resources for multi-release jar files have a #runtime fragment appended to them
sdrach
parents: 37781
diff changeset
   650
        }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   651
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   652
        @Override
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   653
        public String getName() {
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   654
            return name;
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   655
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Ensures that the JarVerifier has been created if one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * necessary (i.e., the jar appears to be signed.) This is done as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * a quick check to avoid processing of the manifest for unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * jars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private void maybeInstantiateVerifier() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (jv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (verify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            String[] names = getMetaInfEntryNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (names != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   672
                for (String nameLower : names) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   673
                    String name = nameLower.toUpperCase(Locale.ENGLISH);
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   674
                    if (name.endsWith(".DSA") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   675
                        name.endsWith(".RSA") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   676
                        name.endsWith(".EC") ||
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   677
                        name.endsWith(".SF")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        // Assume since we found a signature-related file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        // that the jar is signed and that we therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        // need a JarVerifier and Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            // No signature-related files; don't instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            // verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            verify = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Initializes the verifier object by reading all the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * entries and passing them to the verifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    private void initializeVerifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        ManifestEntryVerifier mev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        // Verify "META-INF/" entries...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            String[] names = getMetaInfEntryNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (names != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   704
                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
   705
                    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
   706
                    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
   707
                            || SignatureFileVerifier.isBlockOrSF(uname)) {
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   708
                        JarEntry e = getJarEntry(name);
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   709
                        if (e == null) {
ff8d54ac7c5c 8031572: jarsigner -verify exits with 0 when a jar file is not properly signed
weijun
parents: 22342
diff changeset
   710
                            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
   711
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        if (mev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            mev = new ManifestEntryVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                (getManifestFromReference());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        }
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   716
                        byte[] b = getBytes(e);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   717
                        if (b != null && b.length > 0) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   718
                            jv.beginEntry(e, mev);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   719
                            jv.update(b.length, b, 0, b.length, mev);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   720
                            jv.update(-1, null, 0, 0, mev);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   721
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            // if we had an error parsing any blocks, just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            // treat the jar file as being unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            jv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            verify = false;
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   730
            if (JarVerifier.debug != null) {
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   731
                JarVerifier.debug.println("jarfile parsing error!");
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   732
                ex.printStackTrace();
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   733
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // if after initializing the verifier we have nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        // signed, we null it out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (jv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            jv.doneWithMeta();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (JarVerifier.debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                JarVerifier.debug.println("done with meta!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (jv.nothingToVerify()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                if (JarVerifier.debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    JarVerifier.debug.println("nothing to verify!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                jv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                verify = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Reads all the bytes for a given entry. Used to process the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * META-INF files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    private byte[] getBytes(ZipEntry ze) throws IOException {
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 11274
diff changeset
   761
        try (InputStream is = super.getInputStream(ze)) {
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 32834
diff changeset
   762
            int len = (int)ze.getSize();
33235
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   763
            int bytesRead;
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   764
            byte[] b;
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   765
            // trust specified entry sizes when reasonably small
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   766
            if (len != -1 && len <= 65535) {
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   767
                b = new byte[len];
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   768
                bytesRead = is.readNBytes(b, 0, len);
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   769
            } else {
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   770
                b = is.readAllBytes();
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   771
                bytesRead = b.length;
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   772
            }
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   773
            if (len != -1 && len != bytesRead) {
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   774
                throw new EOFException("Expected:" + len + ", read:" + bytesRead);
edfac9a33df3 8139706: JarFile.getBytes could use InputStream.readNBytes
redestad
parents: 32933
diff changeset
   775
            }
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 32834
diff changeset
   776
            return b;
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 8387
diff changeset
   777
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @param ze the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @return an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *         zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @throws ZipException if a zip file format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @throws SecurityException if any of the jar file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *         are incorrectly signed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @throws IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *         may be thrown if the jar file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    public synchronized InputStream getInputStream(ZipEntry ze)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        maybeInstantiateVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (jv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            return super.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (!jvInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            initializeVerifier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            jvInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            // could be set to null after a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            // initializeVerifier if we have nothing to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            // verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (jv == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                return super.getInputStream(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        // wrap a verifier stream around the real stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return new JarVerifier.VerifierStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            getManifestFromReference(),
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   813
            verifiableEntry(ze),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            super.getInputStream(ze),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            jv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   818
    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
   819
        if (ze instanceof JarFileEntry) {
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   820
            // 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
   821
            return ((JarFileEntry)ze).realEntry();
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   822
        }
36236
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   823
        ze = getJarEntry(ze.getName());
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   824
        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
   825
            return ((JarFileEntry)ze).realEntry();
36236
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   826
        }
eec5770c5b01 8150679: closed/javax/crypto/CryptoPermission/CallerIdentification.sh fails after fix for JDK-8132734
sdrach
parents: 36219
diff changeset
   827
        return (JarEntry)ze;
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   828
    }
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   829
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   830
    // Statics for hand-coded Boyer-Moore search
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   831
    private static final byte[] CLASSPATH_CHARS =
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   832
            {'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
   833
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   834
    // The bad character shift for "class-path: "
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   835
    private static final byte[] CLASSPATH_LASTOCC;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   836
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   837
    // The good suffix shift for "class-path: "
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   838
    private static final byte[] CLASSPATH_OPTOSFT;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   839
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   840
    private static final byte[] MULTIRELEASE_CHARS =
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   841
            {'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
   842
                    ' ', 'T', 'R', 'U', 'E'};
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   843
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   844
    // 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
   845
    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
   846
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   847
    // 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
   848
    private static final byte[] MULTIRELEASE_OPTOSFT;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   849
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    static {
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   851
        CLASSPATH_LASTOCC = new byte[64];
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   852
        CLASSPATH_OPTOSFT = new byte[12];
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   853
        CLASSPATH_LASTOCC[(int)'C' - 32] = 1;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   854
        CLASSPATH_LASTOCC[(int)'L' - 32] = 2;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   855
        CLASSPATH_LASTOCC[(int)'S' - 32] = 5;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   856
        CLASSPATH_LASTOCC[(int)'-' - 32] = 6;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   857
        CLASSPATH_LASTOCC[(int)'P' - 32] = 7;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   858
        CLASSPATH_LASTOCC[(int)'A' - 32] = 8;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   859
        CLASSPATH_LASTOCC[(int)'T' - 32] = 9;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   860
        CLASSPATH_LASTOCC[(int)'H' - 32] = 10;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   861
        CLASSPATH_LASTOCC[(int)':' - 32] = 11;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   862
        CLASSPATH_LASTOCC[(int)' ' - 32] = 12;
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   863
        for (int i = 0; i < 11; i++) {
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   864
            CLASSPATH_OPTOSFT[i] = 12;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   865
        }
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   866
        CLASSPATH_OPTOSFT[11] = 1;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   867
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   868
        MULTIRELEASE_LASTOCC = new byte[64];
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   869
        MULTIRELEASE_OPTOSFT = new byte[19];
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   870
        MULTIRELEASE_LASTOCC[(int)'M' - 32] = 1;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   871
        MULTIRELEASE_LASTOCC[(int)'I' - 32] = 5;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   872
        MULTIRELEASE_LASTOCC[(int)'-' - 32] = 6;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   873
        MULTIRELEASE_LASTOCC[(int)'L' - 32] = 9;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   874
        MULTIRELEASE_LASTOCC[(int)'A' - 32] = 11;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   875
        MULTIRELEASE_LASTOCC[(int)'S' - 32] = 12;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   876
        MULTIRELEASE_LASTOCC[(int)':' - 32] = 14;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   877
        MULTIRELEASE_LASTOCC[(int)' ' - 32] = 15;
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   878
        MULTIRELEASE_LASTOCC[(int)'T' - 32] = 16;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   879
        MULTIRELEASE_LASTOCC[(int)'R' - 32] = 17;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   880
        MULTIRELEASE_LASTOCC[(int)'U' - 32] = 18;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   881
        MULTIRELEASE_LASTOCC[(int)'E' - 32] = 19;
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   882
        for (int i = 0; i < 17; i++) {
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   883
            MULTIRELEASE_OPTOSFT[i] = 19;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   884
        }
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   885
        MULTIRELEASE_OPTOSFT[17] = 6;
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   886
        MULTIRELEASE_OPTOSFT[18] = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    private JarEntry getManEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        if (manEntry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            // 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
   892
            ZipEntry manEntry = super.getEntry(MANIFEST_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            if (manEntry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                // If not found, then iterate through all the "META-INF/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                // entries to find a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                String[] names = getMetaInfEntryNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                if (names != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   898
                    for (String name : names) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
   899
                        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
   900
                            manEntry = super.getEntry(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
36129
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   906
            this.manEntry = (manEntry == null)
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   907
                    ? null
332b49163fc9 8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents: 34686
diff changeset
   908
                    : new JarFileEntry(manEntry.getName(), manEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        return manEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   913
   /**
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   914
    * 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
   915
    * Class-Path attribute
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   916
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    boolean hasClassPathAttribute() throws IOException {
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   918
        checkForSpecialAttributes();
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   919
        return hasClassPathAttribute;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   920
    }
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   921
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   922
    /**
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   923
     * 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
   924
     * 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
   925
     * 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
   926
     * 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
   927
     */
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   928
    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
   929
        int len = src.length;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   930
        int last = b.length - len;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   931
        int i = 0;
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   932
        next:
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   933
        while (i <= last) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   934
            for (int j = (len - 1); j >= 0; j--) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   935
                byte c = b[i + j];
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   936
                if (c >= ' ' && c <= 'z') {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   937
                    if (c >= 'a') c -= 32; // Canonicalize
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   938
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   939
                    if (c != src[j]) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   940
                        // no match
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   941
                        int badShift = lastOcc[c - 32];
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   942
                        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
   943
                        continue next;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   944
                    }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   945
                } else {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   946
                    // no match, character not valid for name
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   947
                    i += len;
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   948
                    continue next;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   949
                }
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   950
            }
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   951
            return i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   953
        return -1;
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   954
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
16023
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   956
    /**
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   957
     * 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
   958
     * 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
   959
     */
58ecc1b8327b 8008290: (profiles) Startup regression due to additional checking of JAR file manifests
alanb
parents: 11274
diff changeset
   960
    private void checkForSpecialAttributes() throws IOException {
36734
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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   964
        synchronized (this) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   965
            if (hasCheckedSpecialAttributes) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   966
                return;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   967
            }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   968
            JarEntry manEntry = getManEntry();
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   969
            if (manEntry != null) {
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   970
                byte[] b = getBytes(manEntry);
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   971
                hasClassPathAttribute = match(CLASSPATH_CHARS, b,
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   972
                        CLASSPATH_LASTOCC, CLASSPATH_OPTOSFT) != -1;
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
   973
                // 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
   974
                if (MULTI_RELEASE_ENABLED) {
40812
dc3755b63ade 8165723: JarFile::isMultiRelease() method returns false when it should return true
redestad
parents: 40539
diff changeset
   975
                    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
   976
                            MULTIRELEASE_OPTOSFT);
37308
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   977
                    if (i != -1) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   978
                        i += MULTIRELEASE_CHARS.length;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   979
                        if (i < b.length) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   980
                            byte c = b[i++];
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   981
                            // Check that the value is followed by a newline
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   982
                            // and does not have a continuation
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   983
                            if (c == '\n' &&
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   984
                                    (i == b.length || b[i] != ' ')) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   985
                                isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   986
                            } else if (c == '\r') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   987
                                if (i == b.length) {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   988
                                    isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   989
                                } else {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   990
                                    c = b[i++];
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   991
                                    if (c == '\n') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   992
                                        if (i == b.length || b[i] != ' ') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   993
                                            isMultiRelease = true;
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
                                    } else if (c != ' ') {
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   996
                                        isMultiRelease = true;
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   997
                                    }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   998
                                }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
   999
                            }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
  1000
                        }
4c4141aac146 8153213: Jar manifest attribute "Multi-Release" accepts any value
redestad
parents: 36734
diff changeset
  1001
                    }
36734
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
  1002
                }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
  1003
            }
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
  1004
            hasCheckedSpecialAttributes = true;
18c3da127b06 8152733: Avoid creating Manifest when checking for Multi-Release attribute
redestad
parents: 36236
diff changeset
  1005
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1008
    private synchronized void ensureInitialization() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1009
        try {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1010
            maybeInstantiateVerifier();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1011
        } catch (IOException e) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1012
            throw new RuntimeException(e);
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
        if (jv != null && !jvInitialized) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1015
            initializeVerifier();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1016
            jvInitialized = true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1017
        }
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
    JarEntry newEntry(ZipEntry ze) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1021
        return new JarFileEntry(ze);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1022
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1023
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1024
    Enumeration<String> entryNames(CodeSource[] cs) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1025
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1026
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1027
            return jv.entryNames(this, cs);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1028
        }
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
        /*
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1031
         * 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
  1032
         * code source?
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1033
         */
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1034
        boolean includeUnsigned = false;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
  1035
        for (CodeSource c : cs) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
  1036
            if (c.getCodeSigners() == null) {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1037
                includeUnsigned = true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1038
                break;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1039
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1040
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1041
        if (includeUnsigned) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1042
            return unsignedEntryNames();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1043
        } else {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
  1044
            return new Enumeration<>() {
8387
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 boolean hasMoreElements() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1047
                    return false;
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
                public String nextElement() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1051
                    throw new NoSuchElementException();
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
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1055
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1056
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
     * Returns an enumeration of the zip file entries
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1059
     * excluding internal JAR mechanism entries and including
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1060
     * signed entries missing from the ZIP directory.
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1061
     */
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1062
    Enumeration<JarEntry> entries2() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1063
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1064
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1065
            return jv.entries2(this, super.entries());
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1066
        }
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
        // screen out entries which are never signed
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1069
        final Enumeration<? extends ZipEntry> enum_ = super.entries();
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
  1070
        return new Enumeration<>() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1071
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1072
            ZipEntry entry;
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
            public boolean hasMoreElements() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1075
                if (entry != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1076
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1077
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1078
                while (enum_.hasMoreElements()) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1079
                    ZipEntry ze = enum_.nextElement();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1080
                    if (JarVerifier.isSigningRelated(ze.getName())) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1081
                        continue;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1082
                    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1083
                    entry = ze;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1084
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1085
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1086
                return false;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1087
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1088
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1089
            public JarFileEntry nextElement() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1090
                if (hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1091
                    ZipEntry ze = entry;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1092
                    entry = null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1093
                    return new JarFileEntry(ze);
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
                throw new NoSuchElementException();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1096
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1097
        };
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1098
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1099
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1100
    CodeSource[] getCodeSources(URL url) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1101
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1102
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1103
            return jv.getCodeSources(this, url);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1104
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1105
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1106
        /*
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1107
         * 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
  1108
         * code source?
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1109
         */
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1110
        Enumeration<String> unsigned = unsignedEntryNames();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1111
        if (unsigned.hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1112
            return new CodeSource[]{JarVerifier.getUnsignedCS(url)};
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1113
        } else {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1114
            return null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1115
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1116
    }
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
    private Enumeration<String> unsignedEntryNames() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1119
        final Enumeration<JarEntry> entries = entries();
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27565
diff changeset
  1120
        return new Enumeration<>() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1121
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1122
            String name;
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
            /*
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1125
             * Grab entries from ZIP directory but screen out
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1126
             * metadata.
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
            public boolean hasMoreElements() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1129
                if (name != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1130
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1131
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1132
                while (entries.hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1133
                    String value;
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1134
                    ZipEntry e = entries.nextElement();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1135
                    value = e.getName();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1136
                    if (e.isDirectory() || JarVerifier.isSigningRelated(value)) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1137
                        continue;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1138
                    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1139
                    name = value;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1140
                    return true;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1141
                }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1142
                return false;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1143
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1144
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1145
            public String nextElement() {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1146
                if (hasMoreElements()) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1147
                    String value = name;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1148
                    name = null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1149
                    return value;
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
                throw new NoSuchElementException();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1152
            }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1153
        };
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1154
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1155
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1156
    CodeSource getCodeSource(URL url, String name) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1157
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1158
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1159
            if (jv.eagerValidation) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1160
                CodeSource cs = null;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1161
                JarEntry je = getJarEntry(name);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1162
                if (je != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1163
                    cs = jv.getCodeSource(url, this, je);
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
                    cs = 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
                return cs;
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1168
            } else {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1169
                return jv.getCodeSource(url, name);
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
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1173
        return JarVerifier.getUnsignedCS(url);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1174
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1175
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1176
    void setEagerValidation(boolean eager) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1177
        try {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1178
            maybeInstantiateVerifier();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1179
        } catch (IOException e) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1180
            throw new RuntimeException(e);
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
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1183
            jv.setEagerValidation(eager);
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1184
        }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1185
    }
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1186
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9365
diff changeset
  1187
    List<Object> getManifestDigests() {
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1188
        ensureInitialization();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1189
        if (jv != null) {
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1190
            return jv.getManifestDigests();
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1191
        }
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 19409
diff changeset
  1192
        return new ArrayList<>();
8387
f0fa7bbf889e 6742654: Code insertion/replacement attacks against signed jars
weijun
parents: 5506
diff changeset
  1193
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
}