< prev index next >

src/java.base/share/classes/java/util/jar/JarFile.java

Print this page

  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.jar;
  27 
  28 import jdk.internal.access.SharedSecrets;
  29 import jdk.internal.access.JavaUtilZipFileAccess;

  30 import sun.security.action.GetPropertyAction;
  31 import sun.security.util.ManifestEntryVerifier;
  32 
  33 import java.io.ByteArrayInputStream;
  34 import java.io.EOFException;
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.lang.ref.SoftReference;
  39 import java.net.URL;
  40 import java.security.CodeSigner;
  41 import java.security.CodeSource;
  42 import java.security.cert.Certificate;
  43 import java.util.ArrayList;
  44 import java.util.Collections;
  45 import java.util.Enumeration;
  46 import java.util.List;
  47 import java.util.Locale;
  48 import java.util.NoSuchElementException;
  49 import java.util.Objects;

 133  * the method {@link JarFile#isMultiRelease()} returns <em>false</em>. The value
 134  * <em>force</em> causes the {@code JarFile} to be initialized to runtime
 135  * versioning after construction.  It effectively does the same as this code:
 136  * {@code (new JarFile(File, boolean, int, JarFile.runtimeVersion())}.
 137  * </li>
 138  * </ul>
 139  * </div>
 140  *
 141  * @author  David Connelly
 142  * @see     Manifest
 143  * @see     java.util.zip.ZipFile
 144  * @see     java.util.jar.JarEntry
 145  * @since   1.2
 146  */
 147 public class JarFile extends ZipFile {
 148     private static final Runtime.Version BASE_VERSION;
 149     private static final int BASE_VERSION_FEATURE;
 150     private static final Runtime.Version RUNTIME_VERSION;
 151     private static final boolean MULTI_RELEASE_ENABLED;
 152     private static final boolean MULTI_RELEASE_FORCED;
 153     private static final ThreadLocal<Boolean> isInitializing = new ThreadLocal<>();
 154     // The maximum size of array to allocate. Some VMs reserve some header words in an array.
 155     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 156 
 157     private SoftReference<Manifest> manRef;
 158     private JarEntry manEntry;
 159     private JarVerifier jv;
 160     private boolean jvInitialized;
 161     private boolean verify;
 162     private final Runtime.Version version;  // current version
 163     private final int versionFeature;       // version.feature()
 164     private boolean isMultiRelease;         // is jar multi-release?
 165 
 166     // indicates if Class-Path attribute present
 167     private boolean hasClassPathAttribute;
 168     // true if manifest checked for special attributes
 169     private volatile boolean hasCheckedSpecialAttributes;
 170 
 171     private static final JavaUtilZipFileAccess JUZFA;
 172 
 173     static {

1011                         byte[] lbuf = new byte[512];
1012                         Attributes attr = new Attributes();
1013                         attr.read(new Manifest.FastInputStream(
1014                                 new ByteArrayInputStream(b)), lbuf);
1015                         isMultiRelease = Boolean.parseBoolean(
1016                             attr.getValue(Attributes.Name.MULTI_RELEASE));
1017                     }
1018                 }
1019             }
1020             hasCheckedSpecialAttributes = true;
1021         }
1022     }
1023 
1024     synchronized void ensureInitialization() {
1025         try {
1026             maybeInstantiateVerifier();
1027         } catch (IOException e) {
1028             throw new RuntimeException(e);
1029         }
1030         if (jv != null && !jvInitialized) {
1031             isInitializing.set(Boolean.TRUE);
1032             try {
1033                 initializeVerifier();
1034                 jvInitialized = true;
1035             } finally {
1036                 isInitializing.set(Boolean.FALSE);
1037             }
1038         }
1039     }
1040 
1041     static boolean isInitializing() {
1042         Boolean value = isInitializing.get();
1043         return (value == null) ? false : value;
1044     }
1045 
1046     /*
1047      * Returns a versioned {@code JarFileEntry} for the given entry,
1048      * if there is one. Otherwise returns the original entry. This
1049      * is invoked by the {@code entries2} for verifier.
1050      */
1051     JarEntry newEntry(JarEntry je) {
1052         if (isMultiRelease()) {
1053             return getVersionedEntry(je.getName(), je);
1054         }
1055         return je;
1056     }
1057 
1058     /*
1059      * Returns a versioned {@code JarFileEntry} for the given entry
1060      * name, if there is one. Otherwise returns a {@code JarFileEntry}
1061      * with the given name. It is invoked from JarVerifier's entries2
1062      * for {@code singers}.
1063      */

  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.jar;
  27 
  28 import jdk.internal.access.SharedSecrets;
  29 import jdk.internal.access.JavaUtilZipFileAccess;
  30 import jdk.internal.misc.Gate;
  31 import sun.security.action.GetPropertyAction;
  32 import sun.security.util.ManifestEntryVerifier;
  33 
  34 import java.io.ByteArrayInputStream;
  35 import java.io.EOFException;
  36 import java.io.File;
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.lang.ref.SoftReference;
  40 import java.net.URL;
  41 import java.security.CodeSigner;
  42 import java.security.CodeSource;
  43 import java.security.cert.Certificate;
  44 import java.util.ArrayList;
  45 import java.util.Collections;
  46 import java.util.Enumeration;
  47 import java.util.List;
  48 import java.util.Locale;
  49 import java.util.NoSuchElementException;
  50 import java.util.Objects;

 134  * the method {@link JarFile#isMultiRelease()} returns <em>false</em>. The value
 135  * <em>force</em> causes the {@code JarFile} to be initialized to runtime
 136  * versioning after construction.  It effectively does the same as this code:
 137  * {@code (new JarFile(File, boolean, int, JarFile.runtimeVersion())}.
 138  * </li>
 139  * </ul>
 140  * </div>
 141  *
 142  * @author  David Connelly
 143  * @see     Manifest
 144  * @see     java.util.zip.ZipFile
 145  * @see     java.util.jar.JarEntry
 146  * @since   1.2
 147  */
 148 public class JarFile extends ZipFile {
 149     private static final Runtime.Version BASE_VERSION;
 150     private static final int BASE_VERSION_FEATURE;
 151     private static final Runtime.Version RUNTIME_VERSION;
 152     private static final boolean MULTI_RELEASE_ENABLED;
 153     private static final boolean MULTI_RELEASE_FORCED;
 154     private static final Gate INITIALIZING_GATE = Gate.create();
 155     // The maximum size of array to allocate. Some VMs reserve some header words in an array.
 156     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 157 
 158     private SoftReference<Manifest> manRef;
 159     private JarEntry manEntry;
 160     private JarVerifier jv;
 161     private boolean jvInitialized;
 162     private boolean verify;
 163     private final Runtime.Version version;  // current version
 164     private final int versionFeature;       // version.feature()
 165     private boolean isMultiRelease;         // is jar multi-release?
 166 
 167     // indicates if Class-Path attribute present
 168     private boolean hasClassPathAttribute;
 169     // true if manifest checked for special attributes
 170     private volatile boolean hasCheckedSpecialAttributes;
 171 
 172     private static final JavaUtilZipFileAccess JUZFA;
 173 
 174     static {

1012                         byte[] lbuf = new byte[512];
1013                         Attributes attr = new Attributes();
1014                         attr.read(new Manifest.FastInputStream(
1015                                 new ByteArrayInputStream(b)), lbuf);
1016                         isMultiRelease = Boolean.parseBoolean(
1017                             attr.getValue(Attributes.Name.MULTI_RELEASE));
1018                     }
1019                 }
1020             }
1021             hasCheckedSpecialAttributes = true;
1022         }
1023     }
1024 
1025     synchronized void ensureInitialization() {
1026         try {
1027             maybeInstantiateVerifier();
1028         } catch (IOException e) {
1029             throw new RuntimeException(e);
1030         }
1031         if (jv != null && !jvInitialized) {
1032             INITIALIZING_GATE.enter();
1033             try {
1034                 initializeVerifier();
1035                 jvInitialized = true;
1036             } finally {
1037                 INITIALIZING_GATE.exit();
1038             }
1039         }
1040     }
1041 
1042     static boolean isInitializing() {
1043         return INITIALIZING_GATE.inside();

1044     }
1045 
1046     /*
1047      * Returns a versioned {@code JarFileEntry} for the given entry,
1048      * if there is one. Otherwise returns the original entry. This
1049      * is invoked by the {@code entries2} for verifier.
1050      */
1051     JarEntry newEntry(JarEntry je) {
1052         if (isMultiRelease()) {
1053             return getVersionedEntry(je.getName(), je);
1054         }
1055         return je;
1056     }
1057 
1058     /*
1059      * Returns a versioned {@code JarFileEntry} for the given entry
1060      * name, if there is one. Otherwise returns a {@code JarFileEntry}
1061      * with the given name. It is invoked from JarVerifier's entries2
1062      * for {@code singers}.
1063      */
< prev index next >