src/java.base/share/classes/java/util/zip/ZipFile.java
author sherman
Mon, 11 Dec 2017 11:45:02 -0800
changeset 48238 9f225d4387e2
parent 47987 85ea7e83af30
child 48294 2608240fc957
permissions -rw-r--r--
8185582: Update Zip implementation to use Cleaner, not finalizers Reviewed-by: plevart, rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43794
497288c158bf 8173094: Error in API documentation for SwingWorker
coffeys
parents: 42444
diff changeset
     2
 * Copyright (c) 1995, 2017, 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: 5148
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: 5148
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: 5148
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
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.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
    28
import java.io.Closeable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
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
    33
import java.io.RandomAccessFile;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    34
import java.io.UncheckedIOException;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    35
import java.lang.ref.Cleaner.Cleanable;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    36
import java.nio.charset.Charset;
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
    37
import java.nio.charset.StandardCharsets;
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
    38
import java.nio.file.attribute.BasicFileAttributes;
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
    39
import java.nio.file.Files;
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
    40
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    41
import java.util.ArrayDeque;
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
    42
import java.util.ArrayList;
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
    43
import java.util.Arrays;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    44
import java.util.Collections;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    45
import java.util.Deque;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Enumeration;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    47
import java.util.HashMap;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    48
import java.util.Iterator;
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
    49
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.NoSuchElementException;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    51
import java.util.Set;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    52
import java.util.Spliterator;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    53
import java.util.Spliterators;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    54
import java.util.WeakHashMap;
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
    55
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
    56
import java.util.function.Consumer;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
    57
import java.util.function.Function;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
    58
import java.util.function.IntFunction;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
    59
import java.util.jar.JarEntry;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    60
import java.util.stream.Stream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    61
import java.util.stream.StreamSupport;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    62
import jdk.internal.misc.JavaUtilZipFileAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    63
import jdk.internal.misc.SharedSecrets;
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
    64
import jdk.internal.misc.VM;
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34874
diff changeset
    65
import jdk.internal.perf.PerfCounter;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    66
import jdk.internal.ref.CleanerFactory;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    67
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    68
import static java.util.zip.ZipConstants64.*;
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
    69
import static java.util.zip.ZipUtils.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * This class is used to read entries from a zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
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
    74
 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    78
 * @apiNote
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    79
 * To release resources used by this {@code ZipFile}, the {@link #close()} method
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    80
 * should be called explicitly or by try-with-resources. Subclasses are responsible
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    81
 * for the cleanup of resources acquired by the subclass. Subclasses that override
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    82
 * {@link #finalize()} in order to perform cleanup should be modified to use alternative
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    83
 * cleanup mechanisms such as {@link java.lang.ref.Cleaner} and remove the overriding
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    84
 * {@code finalize} method.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    85
 *
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    86
 * @implSpec
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    87
 * If this {@code ZipFile} has been subclassed and the {@code close} method has
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    88
 * been overridden, the {@code close} method will be called by the finalization
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    89
 * when {@code ZipFile} is unreachable. But the subclasses should not depend on
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    90
 * this specific implementation; the finalization is not reliable and the
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    91
 * {@code finalize} method is deprecated to be removed.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
    92
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44534
diff changeset
    94
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
    97
class ZipFile implements ZipConstants, Closeable {
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
    98
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
    99
    private final String name;     // zip file name
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34693
diff changeset
   100
    private volatile boolean closeRequested;
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
   101
    private ZipCoder zc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   103
    // The "resource" used by this zip file that needs to be
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   104
    // cleaned after use.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   105
    // a) the input streams that need to be closed
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   106
    // b) the list of cached Inflater objects
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   107
    // c) the "native" source of this zip file.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   108
    private final CleanableResource res;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   109
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Mode flag to open a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static final int OPEN_READ = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Mode flag to open a zip file and mark it for deletion.  The file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * deleted some time between the moment that it is opened and the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * that it is closed, but its contents will remain accessible via the
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
   122
     * {@code ZipFile} object until either the close method is invoked or the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * virtual machine exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static final int OPEN_DELETE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Opens a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
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
   130
     * <p>First, if there is a security manager, its {@code checkRead}
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
   131
     * method is called with the {@code name} argument as its argument
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   132
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   133
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   134
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   135
     * decode the entry names and comments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param name the name of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws SecurityException if a security manager exists and its
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
   141
     *         {@code checkRead} method doesn't allow read access to the file.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   142
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public ZipFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this(new File(name), OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
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
   150
     * Opens a new {@code ZipFile} 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
   151
     * {@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
   152
     * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
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
   154
     * <p>First, if there is a security manager, its {@code checkRead}
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
   155
     * method is called with the {@code name} argument as its argument to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   158
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   159
     * decode the entry names and comments
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   160
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws SecurityException if a security manager exists and
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
   166
     *         its {@code checkRead} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         doesn't allow read access to the file,
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
   168
     *         or its {@code checkDelete} method doesn't allow deleting
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
   169
     *         the file when the {@code OPEN_DELETE} flag is set.
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
   170
     * @throws IllegalArgumentException if the {@code mode} argument is invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public ZipFile(File file, int mode) throws IOException {
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
   175
        this(file, mode, StandardCharsets.UTF_8);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   176
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   177
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   178
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   179
     * Opens a ZIP file for reading given the specified File object.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   180
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   181
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   182
     * decode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   183
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   184
     * @param file the ZIP file to be opened for reading
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   185
     * @throws ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   186
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   187
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   188
    public ZipFile(File file) throws ZipException, IOException {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   189
        this(file, OPEN_READ);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   190
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   191
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   192
    /**
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
   193
     * Opens a new {@code ZipFile} 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
   194
     * {@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
   195
     * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   196
     *
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
   197
     * <p>First, if there is a security manager, its {@code checkRead}
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
   198
     * method is called with the {@code name} argument as its argument to
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   199
     * ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   200
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   201
     * @param file the ZIP file to be opened for reading
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   202
     * @param mode the mode in which the file is to be opened
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   203
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   204
     *        the {@linkplain java.nio.charset.Charset charset} to
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   205
     *        be used to decode the ZIP entry name and comment that are not
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   206
     *        encoded by using UTF-8 encoding (indicated by entry's general
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   207
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   208
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   209
     * @throws ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   210
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   211
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   212
     * @throws SecurityException
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
   213
     *         if a security manager exists and its {@code checkRead}
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   214
     *         method doesn't allow read access to the file,or its
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
   215
     *         {@code checkDelete} method doesn't allow deleting the
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
   216
     *         file when the {@code OPEN_DELETE} flag is set
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   217
     *
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
   218
     * @throws IllegalArgumentException if the {@code mode} argument is invalid
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   219
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   220
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   221
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   222
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   223
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   224
    public ZipFile(File file, int mode, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   225
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (((mode & OPEN_READ) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new IllegalArgumentException("Illegal mode: 0x"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                               Integer.toHexString(mode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            sm.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if ((mode & OPEN_DELETE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                sm.checkDelete(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
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
   239
        Objects.requireNonNull(charset, "charset");
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   240
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   241
        this.zc = ZipCoder.get(charset);
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
   242
        this.name = name;
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   243
        long t0 = System.nanoTime();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   244
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   245
        this.res = CleanableResource.get(this, file, mode);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   246
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34874
diff changeset
   247
        PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34874
diff changeset
   248
        PerfCounter.getZipFileCount().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   251
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   252
     * Opens a zip file for reading.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   253
     *
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
   254
     * <p>First, if there is a security manager, its {@code checkRead}
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
   255
     * method is called with the {@code name} argument as its argument
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   256
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   257
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   258
     * @param name the name of the zip file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   259
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   260
     *        the {@linkplain java.nio.charset.Charset charset} to
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   261
     *        be used to decode the ZIP entry name and comment that are not
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   262
     *        encoded by using UTF-8 encoding (indicated by entry's general
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   263
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   264
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   265
     * @throws ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   266
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   267
     * @throws SecurityException
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
   268
     *         if a security manager exists and its {@code checkRead}
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   269
     *         method doesn't allow read access to the file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   270
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   271
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   272
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   273
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   274
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   275
    public ZipFile(String name, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   276
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   277
        this(new File(name), OPEN_READ, charset);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   278
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Opens a ZIP file for reading given the specified File object.
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
   282
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param file the ZIP file to be opened for reading
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   284
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   285
     *        The {@linkplain java.nio.charset.Charset charset} to be
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   286
     *        used to decode the ZIP entry name and comment (ignored if
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   287
     *        the <a href="package-summary.html#lang_encoding"> language
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   288
     *        encoding bit</a> of the ZIP entry's general purpose bit
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   289
     *        flag is set).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   290
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   291
     * @throws ZipException if a ZIP format error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @throws IOException if an I/O error has occurred
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   293
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   294
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   296
    public ZipFile(File file, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   297
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   298
        this(file, OPEN_READ, charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   302
     * Returns the zip file comment, or null if none.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   303
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   304
     * @return the comment string for the zip file, or null if none
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   305
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   306
     * @throws IllegalStateException if the zip file has been closed
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   307
     *
43794
497288c158bf 8173094: Error in API documentation for SwingWorker
coffeys
parents: 42444
diff changeset
   308
     * @since 1.7
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   309
     */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   310
    public String getComment() {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   311
        synchronized (this) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   312
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   313
            if (res.zsrc.comment == null) {
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   314
                return null;
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
   315
            }
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   316
            return zc.toString(res.zsrc.comment);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   317
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   318
    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   319
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   320
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns the zip file entry for the specified name, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param name the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return the zip file entry, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public ZipEntry getEntry(String name) {
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   329
        return getEntry(name, ZipEntry::new);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   330
    }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   331
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   332
    /*
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   333
     * Returns the zip file entry for the specified name, or null
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   334
     * if not found.
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   335
     *
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   336
     * @param name the name of the entry
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   337
     * @param func the function that creates the returned entry
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   338
     *
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   339
     * @return the zip file entry, or null if not found
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   340
     * @throws IllegalStateException if the zip file has been closed
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   341
     */
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   342
    private ZipEntry getEntry(String name, Function<String, ? extends ZipEntry> func) {
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
   343
        Objects.requireNonNull(name, "name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            ensureOpen();
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   346
            byte[] bname = zc.getBytes(name);
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   347
            int pos = res.zsrc.getEntryPos(bname, true);
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
   348
            if (pos != -1) {
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   349
                return getZipEntry(name, bname, pos, func);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * zip file entry.
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
   358
     * <p>
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
   359
     * Closing this ZIP file will, in turn, close all input streams that
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
   360
     * have been returned by invocations of this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param entry the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return the input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public InputStream getInputStream(ZipEntry entry) throws IOException {
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
   370
        Objects.requireNonNull(entry, "entry");
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
   371
        int pos = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        ZipFileInputStream in = null;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   373
        Source zsrc = res.zsrc;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   374
        Set<InputStream> istreams = res.istreams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            ensureOpen();
42444
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   377
            if (Objects.equals(lastEntryName, entry.name)) {
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   378
                pos = lastEntryPos;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   379
            } else if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
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
   380
                pos = zsrc.getEntryPos(zc.getBytesUTF8(entry.name), false);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   381
            } else {
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
   382
                pos = zsrc.getEntryPos(zc.getBytes(entry.name), false);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   383
            }
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
   384
            if (pos == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
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
   387
            in = new ZipFileInputStream(zsrc.cen, pos);
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
   388
            switch (CENHOW(zsrc.cen, pos)) {
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   389
            case STORED:
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   390
                synchronized (istreams) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   391
                    istreams.add(in);
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   392
                }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   393
                return in;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   394
            case DEFLATED:
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
   395
                // Inflater likes a bit of slack
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   396
                // MORE: Compute good size for inflater stream:
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
   397
                long size = CENLEN(zsrc.cen, pos) + 2;
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
   398
                if (size > 65536) {
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
   399
                    size = 8192;
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
   400
                }
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
   401
                if (size <= 0) {
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
   402
                    size = 4096;
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
   403
                }
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   404
                InputStream is = new ZipFileInflaterInputStream(in, res, (int)size);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   405
                synchronized (istreams) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   406
                    istreams.add(is);
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   407
                }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   408
                return is;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   409
            default:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   410
                throw new ZipException("invalid compression method");
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   411
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   415
    private class ZipFileInflaterInputStream extends InflaterInputStream {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34693
diff changeset
   416
        private volatile boolean closeRequested;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   417
        private boolean eof = false;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   418
        private final Cleanable cleanable;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   419
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   420
        ZipFileInflaterInputStream(ZipFileInputStream zfin,
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   421
                                   CleanableResource res, int size) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   422
            this(zfin, res, res.getInflater(), size);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   423
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   424
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   425
        private ZipFileInflaterInputStream(ZipFileInputStream zfin,
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   426
                                           CleanableResource res,
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   427
                                           Inflater inf, int size) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   428
            super(zfin, inf, size);
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   429
            this.cleanable = CleanerFactory.cleaner().register(this,
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   430
                    () -> res.releaseInflater(inf));
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   431
       }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   432
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   433
        public void close() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   434
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   435
                return;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   436
            closeRequested = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   437
            super.close();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   438
            synchronized (res.istreams) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   439
                res.istreams.remove(this);
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   440
            }
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   441
            cleanable.clean();
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   442
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   443
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   444
        // Override fill() method to provide an extra "dummy" byte
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   445
        // at the end of the input stream. This is required when
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   446
        // using the "nowrap" Inflater option.
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   447
        protected void fill() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   448
            if (eof) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   449
                throw new EOFException("Unexpected end of ZLIB input stream");
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   450
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   451
            len = in.read(buf, 0, buf.length);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   452
            if (len == -1) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   453
                buf[0] = 0;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   454
                len = 1;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   455
                eof = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   456
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   457
            inf.setInput(buf, 0, len);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   458
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   459
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   460
        public int available() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   461
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   462
                return 0;
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   463
            long avail = ((ZipFileInputStream)in).size() - inf.getBytesWritten();
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   464
            return (avail > (long) Integer.MAX_VALUE ?
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   465
                    Integer.MAX_VALUE : (int) avail);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   466
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   467
    }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   468
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Returns the path name of the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return the path name of the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   477
    private class ZipEntryIterator<T extends ZipEntry>
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   478
            implements Enumeration<T>, Iterator<T> {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   479
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   480
        private int i = 0;
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   481
        private final int entryCount;
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   482
        private final Function<String, T> gen;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   483
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   484
        public ZipEntryIterator(int entryCount, Function<String, T> gen) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   485
            this.entryCount = entryCount;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   486
            this.gen = gen;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   487
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   488
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   489
        @Override
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   490
        public boolean hasMoreElements() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   491
            return hasNext();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   492
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   493
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   494
        @Override
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   495
        public boolean hasNext() {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   496
            return i < entryCount;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   497
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   498
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   499
        @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   500
        public T nextElement() {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   501
            return next();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   502
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   503
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   504
        @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   505
        @SuppressWarnings("unchecked")
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   506
        public T  next() {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   507
            synchronized (ZipFile.this) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   508
                ensureOpen();
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   509
                if (!hasNext()) {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   510
                    throw new NoSuchElementException();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   511
                }
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
   512
                // each "entry" has 3 ints in table entries
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   513
                return (T)getZipEntry(null, null, res.zsrc.getEntryPos(i++ * 3), gen);
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   514
            }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   515
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   516
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   517
        @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   518
        public Iterator<T> asIterator() {
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   519
            return this;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   520
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   521
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   522
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Returns an enumeration of the ZIP file entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @return an enumeration of the ZIP file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public Enumeration<? extends ZipEntry> entries() {
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   529
        synchronized (this) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   530
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   531
            return new ZipEntryIterator<ZipEntry>(res.zsrc.total, ZipEntry::new);
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   532
        }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   533
    }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   534
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   535
    private Enumeration<JarEntry> entries(Function<String, JarEntry> func) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   536
        synchronized (this) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   537
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   538
            return new ZipEntryIterator<JarEntry>(res.zsrc.total, func);
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   539
        }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   540
    }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   541
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   542
    private class EntrySpliterator<T> extends Spliterators.AbstractSpliterator<T> {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   543
        private int index;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   544
        private final int fence;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   545
        private final IntFunction<T> gen;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   546
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   547
        EntrySpliterator(int index, int fence, IntFunction<T> gen) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   548
            super((long)fence,
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   549
                  Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.IMMUTABLE |
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   550
                  Spliterator.NONNULL);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   551
            this.index = index;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   552
            this.fence = fence;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   553
            this.gen = gen;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   554
        }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   555
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   556
        @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   557
        public boolean tryAdvance(Consumer<? super T> action) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   558
            if (action == null)
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   559
                throw new NullPointerException();
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   560
            if (index >= 0 && index < fence) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   561
                synchronized (ZipFile.this) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   562
                    ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   563
                    action.accept(gen.apply(res.zsrc.getEntryPos(index++ * 3)));
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   564
                }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   565
                return true;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   566
            }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   567
            return false;
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   568
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   569
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   570
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   571
    /**
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29226
diff changeset
   572
     * Returns an ordered {@code Stream} over the ZIP file entries.
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   573
     *
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   574
     * Entries appear in the {@code Stream} in the order they appear in
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   575
     * the central directory of the ZIP file.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   576
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   577
     * @return an ordered {@code Stream} of entries in this ZIP file
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   578
     * @throws IllegalStateException if the zip file has been closed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   579
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   580
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   581
    public Stream<? extends ZipEntry> stream() {
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   582
        synchronized (this) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   583
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   584
            return StreamSupport.stream(new EntrySpliterator<>(0, res.zsrc.total,
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   585
                pos -> getZipEntry(null, null, pos, ZipEntry::new)), false);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   586
       }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   587
    }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   588
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   589
    private String getEntryName(int pos) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   590
        byte[] cen = res.zsrc.cen;
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   591
        int nlen = CENNAM(cen, pos);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   592
        int clen = CENCOM(cen, pos);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   593
        int flag = CENFLG(cen, pos);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   594
        if (!zc.isUTF8() && (flag & EFS) != 0) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   595
            return zc.toStringUTF8(cen, pos + CENHDR, nlen);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   596
        } else {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   597
            return zc.toString(cen, pos + CENHDR, nlen);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   598
        }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   599
    }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   600
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   601
    /*
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   602
     * Returns an ordered {@code Stream} over the zip file entry names.
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   603
     *
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   604
     * Entry names appear in the {@code Stream} in the order they appear in
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   605
     * the central directory of the ZIP file.
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   606
     *
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   607
     * @return an ordered {@code Stream} of entry names in this zip file
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   608
     * @throws IllegalStateException if the zip file has been closed
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   609
     * @since 10
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   610
     */
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   611
    private Stream<String> entryNameStream() {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   612
        synchronized (this) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   613
            ensureOpen();
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   614
            return StreamSupport.stream(
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   615
                new EntrySpliterator<>(0, res.zsrc.total, this::getEntryName), false);
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   616
        }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   617
    }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   618
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   619
    /*
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   620
     * Returns an ordered {@code Stream} over the zip file entries.
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   621
     *
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   622
     * Entries appear in the {@code Stream} in the order they appear in
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   623
     * the central directory of the jar file.
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   624
     *
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   625
     * @param func the function that creates the returned entry
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   626
     * @return an ordered {@code Stream} of entries in this zip file
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   627
     * @throws IllegalStateException if the zip file has been closed
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   628
     * @since 10
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   629
     */
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   630
    private Stream<JarEntry> stream(Function<String, JarEntry> func) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   631
        synchronized (this) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   632
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   633
            return StreamSupport.stream(new EntrySpliterator<>(0, res.zsrc.total,
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   634
                pos -> (JarEntry)getZipEntry(null, null, pos, func)), false);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   635
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
42444
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   638
    private String lastEntryName;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   639
    private int lastEntryPos;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   640
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
   641
    /* Checks ensureOpen() before invoke this method */
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   642
    private ZipEntry getZipEntry(String name, byte[] bname, int pos,
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   643
                                 Function<String, ? extends ZipEntry> func) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   644
        byte[] cen = res.zsrc.cen;
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
   645
        int nlen = CENNAM(cen, pos);
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
   646
        int elen = CENEXT(cen, pos);
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
   647
        int clen = CENCOM(cen, pos);
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   648
        int flag = CENFLG(cen, pos);
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   649
        if (name == null || bname.length != nlen) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   650
            // to use the entry name stored in cen, if the passed in name is
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   651
            // (1) null, invoked from iterator, or
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   652
            // (2) not equal to the name stored, a slash is appended during
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   653
            // getEntryPos() search.
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   654
            if (!zc.isUTF8() && (flag & EFS) != 0) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   655
                name = zc.toStringUTF8(cen, pos + CENHDR, nlen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   656
            } else {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   657
                name = zc.toString(cen, pos + CENHDR, nlen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   658
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   659
        }
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
   660
        ZipEntry e = func.apply(name);    //ZipEntry e = new ZipEntry(name);
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   661
        e.flag = flag;
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
   662
        e.xdostime = CENTIM(cen, pos);
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
   663
        e.crc = CENCRC(cen, pos);
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
   664
        e.size = CENLEN(cen, pos);
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
   665
        e.csize = CENSIZ(cen, pos);
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
   666
        e.method = CENHOW(cen, pos);
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
   667
        if (elen != 0) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   668
            int start = pos + CENHDR + nlen;
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   669
            e.setExtra0(Arrays.copyOfRange(cen, start, start + elen), true);
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
   670
        }
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
   671
        if (clen != 0) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   672
            int start = pos + CENHDR + nlen + elen;
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   673
            if (!zc.isUTF8() && (flag & EFS) != 0) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   674
                e.comment = zc.toStringUTF8(cen, start, clen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   675
            } else {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   676
                e.comment = zc.toString(cen, start, clen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   677
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   678
        }
42444
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   679
        lastEntryName = e.name;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   680
        lastEntryPos = pos;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   681
        return e;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   682
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   683
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Returns the number of entries in the ZIP file.
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
   686
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return the number of entries in the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public int size() {
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
   691
        synchronized (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
   692
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   693
            return res.zsrc.total;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   694
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   695
    }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   696
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   697
    private static class CleanableResource implements Runnable {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   698
        // The outstanding inputstreams that need to be closed
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   699
        final Set<InputStream> istreams;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   700
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   701
        // List of cached Inflater objects for decompression
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   702
        Deque<Inflater> inflaterCache;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   703
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   704
        final Cleanable cleanable;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   705
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   706
        Source zsrc;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   707
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   708
        CleanableResource(ZipFile zf, File file, int mode) throws IOException {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   709
            this.cleanable = CleanerFactory.cleaner().register(zf, this);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   710
            this.istreams = Collections.newSetFromMap(new WeakHashMap<>());
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   711
            this.inflaterCache = new ArrayDeque<>();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   712
            this.zsrc = Source.get(file, (mode & OPEN_DELETE) != 0);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   713
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   714
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   715
        void clean() {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   716
            cleanable.clean();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   717
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   718
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   719
        /*
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   720
         * Gets an inflater from the list of available inflaters or allocates
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   721
         * a new one.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   722
         */
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   723
        Inflater getInflater() {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   724
            Inflater inf;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   725
            synchronized (inflaterCache) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   726
                if ((inf = inflaterCache.poll()) != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   727
                    return inf;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   728
                }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   729
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   730
            return new Inflater(true);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   731
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   732
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   733
        /*
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   734
         * Releases the specified inflater to the list of available inflaters.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   735
         */
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   736
        void releaseInflater(Inflater inf) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   737
            Deque<Inflater> inflaters = this.inflaterCache;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   738
            if (inflaters != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   739
                synchronized (inflaters) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   740
                    // double checked!
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   741
                    if (inflaters == this.inflaterCache) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   742
                        inf.reset();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   743
                        inflaters.add(inf);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   744
                        return;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   745
                    }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   746
                }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   747
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   748
            // inflaters cache already closed - just end it.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   749
            inf.end();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   750
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   751
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   752
        public void run() {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   753
            IOException ioe = null;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   754
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   755
            // Release cached inflaters and close the cache first
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   756
            Deque<Inflater> inflaters = this.inflaterCache;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   757
            if (inflaters != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   758
                synchronized (inflaters) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   759
                    // no need to double-check as only one thread gets a
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   760
                    // chance to execute run() (Cleaner guarantee)...
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   761
                    Inflater inf;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   762
                    while ((inf = inflaters.poll()) != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   763
                        inf.end();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   764
                    }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   765
                    // close inflaters cache
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   766
                    this.inflaterCache = null;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   767
                }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   768
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   769
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   770
            // Close streams, release their inflaters
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   771
            if (istreams != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   772
                synchronized (istreams) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   773
                    if (!istreams.isEmpty()) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   774
                        InputStream[] copy = istreams.toArray(new InputStream[0]);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   775
                        istreams.clear();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   776
                        for (InputStream is : copy) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   777
                            try {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   778
                                is.close();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   779
                            }  catch (IOException e) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   780
                                if (ioe == null) ioe = e;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   781
                                else ioe.addSuppressed(e);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   782
                            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   783
                        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   784
                    }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   785
                }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   786
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   787
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   788
            // Release zip src
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   789
            if (zsrc != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   790
                synchronized (zsrc) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   791
                    try {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   792
                        Source.release(zsrc);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   793
                        zsrc = null;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   794
                     }  catch (IOException e) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   795
                         if (ioe == null) ioe = e;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   796
                         else ioe.addSuppressed(e);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   797
                    }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   798
                }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   799
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   800
            if (ioe != null) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   801
                throw new UncheckedIOException(ioe);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   802
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   803
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   804
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   805
        CleanableResource(File file, int mode)
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   806
            throws IOException {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   807
            this.cleanable = null;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   808
            this.istreams = Collections.newSetFromMap(new WeakHashMap<>());
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   809
            this.inflaterCache = new ArrayDeque<>();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   810
            this.zsrc = Source.get(file, (mode & OPEN_DELETE) != 0);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   811
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   812
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   813
        /*
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   814
         * If {@code ZipFile} has been subclassed and the {@code close} method is
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   815
         * overridden, uses the {@code finalizer} mechanism for resource cleanup.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   816
         * So {@code close} method can be called when the the {@code ZipFile} is
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   817
         * unreachable. This mechanism will be removed when {@code finalize} method
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   818
         * is removed from {@code ZipFile}.
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   819
         */
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   820
        static CleanableResource get(ZipFile zf, File file, int mode)
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   821
            throws IOException {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   822
            Class<?> clz = zf.getClass();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   823
            while (clz != ZipFile.class) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   824
                try {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   825
                    clz.getDeclaredMethod("close");
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   826
                    return new FinalizableResource(zf, file, mode);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   827
                } catch (NoSuchMethodException nsme) {}
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   828
                clz = clz.getSuperclass();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   829
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   830
            return new CleanableResource(zf, file, mode);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   831
        }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   832
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   833
        static class FinalizableResource extends CleanableResource {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   834
            ZipFile zf;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   835
            FinalizableResource(ZipFile zf, File file, int mode)
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   836
                throws IOException {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   837
                super(file, mode);
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   838
                this.zf = zf;
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   839
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   840
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   841
            @Override
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   842
            void clean() {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   843
                run();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   844
            }
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   845
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   846
            @Override
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   847
            @SuppressWarnings("deprecation")
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   848
            protected void finalize() throws IOException {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   849
                zf.close();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   850
            }
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
   851
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Closes the ZIP file.
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   856
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <p> Closing this ZIP file will close all of the input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * previously returned by invocations of the {@link #getInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * getInputStream} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public void close() throws IOException {
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
   864
        if (closeRequested) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   865
            return;
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
   866
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   867
        closeRequested = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   869
        synchronized (this) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   870
            // Close streams, release their inflaters, release cached inflaters
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   871
            // and release zip source
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   872
            try {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   873
                res.clean();
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   874
            } catch (UncheckedIOException ioe) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   875
                throw ioe.getCause();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    /**
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   881
     * Ensures that the system resources held by this ZipFile object are
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   882
     * released when there are no more references to it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   884
     * @deprecated The {@code finalize} method has been deprecated and will be
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   885
     *     removed. It is implemented as a no-op. Subclasses that override
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   886
     *     {@code finalize} in order to perform cleanup should be modified to
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   887
     *     use alternative cleanup mechanisms and to remove the overriding
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   888
     *     {@code finalize} method. The recommended cleanup for ZipFile object
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   889
     *     is to explicitly invoke {@code close} method when it is no longer in
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   890
     *     use, or use try-with-resources. If the {@code close} is not invoked
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   891
     *     explicitly the resources held by this object will be released when
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   892
     *     the instance becomes unreachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   896
    @Deprecated(since="9", forRemoval=true)
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   897
    protected void finalize() throws IOException {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            throw new IllegalStateException("zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   903
        if (res.zsrc == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            throw new IllegalStateException("The object is not initialized.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    private void ensureOpenOrZipException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            throw new ZipException("ZipFile closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Inner class implementing the input stream used to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * (possibly compressed) zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
   private class ZipFileInputStream extends InputStream {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34693
diff changeset
   919
        private volatile boolean closeRequested;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        private   long pos;     // current position within entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        protected long rem;     // number of remaining bytes within entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        protected long size;    // uncompressed size of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   924
        ZipFileInputStream(byte[] cen, int cenpos) {
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
   925
            rem = CENSIZ(cen, cenpos);
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
   926
            size = CENLEN(cen, cenpos);
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
   927
            pos = CENOFF(cen, cenpos);
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
   928
            // zip64
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
   929
            if (rem == ZIP64_MAGICVAL || size == ZIP64_MAGICVAL ||
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
   930
                pos == ZIP64_MAGICVAL) {
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
   931
                checkZIP64(cen, cenpos);
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
   932
            }
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
   933
            // negative for lazy initialization, see getDataOffset();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   934
            pos = - (pos + ZipFile.this.res.zsrc.locpos);
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
   935
        }
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
   936
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   937
         private void checkZIP64(byte[] cen, int cenpos) {
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
   938
            int off = cenpos + CENHDR + CENNAM(cen, cenpos);
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
   939
            int end = off + CENEXT(cen, cenpos);
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
   940
            while (off + 4 < end) {
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
   941
                int tag = get16(cen, off);
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
   942
                int sz = get16(cen, off + 2);
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
   943
                off += 4;
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
   944
                if (off + sz > end)         // invalid data
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
   945
                    break;
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
   946
                if (tag == EXTID_ZIP64) {
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
   947
                    if (size == ZIP64_MAGICVAL) {
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
   948
                        if (sz < 8 || (off + 8) > end)
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
   949
                            break;
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
   950
                        size = get64(cen, off);
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
   951
                        sz -= 8;
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
   952
                        off += 8;
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
   953
                    }
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
   954
                    if (rem == ZIP64_MAGICVAL) {
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
   955
                        if (sz < 8 || (off + 8) > end)
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
   956
                            break;
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
   957
                        rem = get64(cen, off);
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
   958
                        sz -= 8;
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
   959
                        off += 8;
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
   960
                    }
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
   961
                    if (pos == ZIP64_MAGICVAL) {
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
   962
                        if (sz < 8 || (off + 8) > end)
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
   963
                            break;
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
   964
                        pos = get64(cen, off);
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
   965
                        sz -= 8;
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
   966
                        off += 8;
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
   967
                    }
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
   968
                    break;
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
   969
                }
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
   970
                off += sz;
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
   971
            }
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
   972
        }
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
   973
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
   974
       /* The Zip file spec explicitly allows the LOC extra data size to
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
   975
        * be different from the CEN extra data size. Since we cannot trust
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
   976
        * the CEN extra data size, we need to read the LOC to determine
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
   977
        * the entry data offset.
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
   978
        */
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
   979
        private long initDataOffset() throws IOException {
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
   980
            if (pos <= 0) {
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
   981
                byte[] loc = new byte[LOCHDR];
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
   982
                pos = -pos;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
   983
                int len = ZipFile.this.res.zsrc.readFullyAt(loc, 0, loc.length, pos);
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
   984
                if (len != LOCHDR) {
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
   985
                    throw new ZipException("ZipFile error reading zip file");
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
   986
                }
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
   987
                if (LOCSIG(loc) != LOCSIG) {
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
   988
                    throw new ZipException("ZipFile invalid LOC header (bad signature)");
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
   989
                }
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
   990
                pos += LOCHDR + LOCNAM(loc) + LOCEXT(loc);
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
   991
            }
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
   992
            return pos;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            synchronized (ZipFile.this) {
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
   997
                ensureOpenOrZipException();
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
   998
                initDataOffset();
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   999
                if (rem == 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1000
                    return -1;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1001
                }
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
  1002
                if (len > rem) {
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
  1003
                    len = (int) rem;
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
  1004
                }
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1005
                if (len <= 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1006
                    return 0;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1007
                }
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1008
                len = ZipFile.this.res.zsrc.readAt(b, off, len, pos);
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1009
                if (len > 0) {
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
  1010
                    pos += len;
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
  1011
                    rem -= len;
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
  1012
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            if (read(b, 0, 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
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
  1029
        public long skip(long n) throws IOException {
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
  1030
            synchronized (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
  1031
                initDataOffset();
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
  1032
                if (n > rem) {
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
  1033
                    n = rem;
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
  1034
                }
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
  1035
                pos += n;
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
  1036
                rem -= n;
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
  1037
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        public long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        public void close() {
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
  1053
            if (closeRequested) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
  1054
                return;
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
  1055
            }
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
  1056
            closeRequested = true;
34526
f1f852f5f477 8144958: changes by JDK-8142508 seems to have broken jtreg
sherman
parents: 34525
diff changeset
  1057
            rem = 0;
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1058
            synchronized (res.istreams) {
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1059
                res.istreams.remove(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
  1062
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1065
    /**
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1066
     * Returns the names of all non-directory entries that begin with
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1067
     * "META-INF/" (case ignored). This method is used in JarFile, via
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1068
     * SharedSecrets, as an optimization when looking up manifest and
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1069
     * signature file entries. Returns null if no entries were found.
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
  1070
     */
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
  1071
    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
  1072
        synchronized (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
  1073
            ensureOpen();
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1074
            Source zsrc = res.zsrc;
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1075
            if (zsrc.metanames == null) {
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
  1076
                return null;
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
  1077
            }
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1078
            String[] names = new String[zsrc.metanames.length];
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
  1079
            byte[] cen = zsrc.cen;
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
  1080
            for (int i = 0; i < names.length; i++) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1081
                int pos = zsrc.metanames[i];
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1082
                names[i] = new String(cen, pos + CENHDR, CENNAM(cen, pos),
34693
5b58b6cdc196 8145343: CorruptEntry.java fails after push for JDK-8145260
sherman
parents: 34686
diff changeset
  1083
                                      StandardCharsets.UTF_8);
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
  1084
            }
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
  1085
            return names;
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
  1086
        }
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
  1087
    }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
  1088
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1089
    private static boolean isWindows;
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1090
    static {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1091
        SharedSecrets.setJavaUtilZipFileAccess(
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1092
            new JavaUtilZipFileAccess() {
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1093
                @Override
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1094
                public boolean startsWithLocHeader(ZipFile zip) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1095
                    return zip.res.zsrc.startsWithLoc;
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1096
                }
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1097
                @Override
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1098
                public String[] getMetaInfEntryNames(ZipFile zip) {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1099
                    return zip.getMetaInfEntryNames();
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1100
                }
47987
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1101
                @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1102
                public JarEntry getEntry(ZipFile zip, String name,
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1103
                    Function<String, JarEntry> func) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1104
                    return (JarEntry)zip.getEntry(name, func);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1105
                }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1106
                @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1107
                public Enumeration<JarEntry> entries(ZipFile zip,
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1108
                    Function<String, JarEntry> func) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1109
                    return zip.entries(func);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1110
                }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1111
                @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1112
                public Stream<JarEntry> stream(ZipFile zip,
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1113
                    Function<String, JarEntry> func) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1114
                    return zip.stream(func);
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1115
                }
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1116
                @Override
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1117
                public Stream<String> entryNameStream(ZipFile zip) {
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1118
                    return zip.entryNameStream();
85ea7e83af30 8189611: JarFile versioned stream and real name support
sherman
parents: 47223
diff changeset
  1119
                }
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1120
             }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1121
        );
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1122
        isWindows = VM.getSavedProperty("os.name").contains("Windows");
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1123
    }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1124
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
  1125
    private static class Source {
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
  1126
        private final Key key;               // the key in files
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
  1127
        private int refs = 1;
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
  1128
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
  1129
        private RandomAccessFile zfile;      // zfile of the underlying zip file
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
  1130
        private byte[] cen;                  // CEN & ENDHDR
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
  1131
        private long locpos;                 // position of first LOC header (usually 0)
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
  1132
        private byte[] comment;              // zip file comment
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
  1133
                                             // list of meta entries in META-INF dir
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1134
        private int[] metanames;
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
  1135
        private final boolean startsWithLoc; // true, if zip file starts with LOCSIG (usually true)
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
  1136
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
  1137
        // A Hashmap for all entries.
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
  1138
        //
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
  1139
        // A cen entry of Zip/JAR file. As we have one for every entry in every active Zip/JAR,
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
  1140
        // We might have a lot of these in a typical system. In order to save space we don't
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
  1141
        // keep the name in memory, but merely remember a 32 bit {@code hash} value of the
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
  1142
        // entry name and its offset {@code pos} in the central directory hdeader.
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
  1143
        //
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
  1144
        // private static class Entry {
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
  1145
        //     int hash;       // 32 bit hashcode on name
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
  1146
        //     int next;       // hash chain: index into entries
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
  1147
        //     int pos;        // Offset of central directory file header
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
  1148
        // }
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
  1149
        // private Entry[] entries;             // array of hashed cen entry
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
  1150
        //
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
  1151
        // To reduce the total size of entries further, we use a int[] here to store 3 "int"
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
  1152
        // {@code hash}, {@code next and {@code "pos for each entry. The entry can then be
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
  1153
        // referred by their index of their positions in the {@code entries}.
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
  1154
        //
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
  1155
        private int[] entries;                  // array of hashed cen entry
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
  1156
        private int addEntry(int index, int hash, int next, int pos) {
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
  1157
            entries[index++] = hash;
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
  1158
            entries[index++] = next;
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
  1159
            entries[index++] = pos;
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
  1160
            return index;
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
  1161
        }
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
  1162
        private int getEntryHash(int index) { return entries[index]; }
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
  1163
        private int getEntryNext(int index) { return entries[index + 1]; }
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
  1164
        private int getEntryPos(int index)  { return entries[index + 2]; }
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
  1165
        private static final int ZIP_ENDCHAIN  = -1;
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
  1166
        private int total;                   // total number of entries
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
  1167
        private int[] table;                 // Hash chain heads: indexes into entries
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
  1168
        private int tablelen;                // number of hash heads
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
  1169
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
  1170
        private static class Key {
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
  1171
            BasicFileAttributes attrs;
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
  1172
            File file;
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
  1173
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
  1174
            public Key(File file, BasicFileAttributes attrs) {
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
  1175
                this.attrs = attrs;
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
  1176
                this.file = file;
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
  1177
            }
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
  1178
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
  1179
            public int hashCode() {
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
  1180
                long t = attrs.lastModifiedTime().toMillis();
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
  1181
                return ((int)(t ^ (t >>> 32))) + file.hashCode();
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
  1182
            }
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
  1183
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
  1184
            public boolean equals(Object obj) {
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
  1185
                if (obj instanceof Key) {
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
  1186
                    Key key = (Key)obj;
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
  1187
                    if (!attrs.lastModifiedTime().equals(key.attrs.lastModifiedTime())) {
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
  1188
                        return false;
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
  1189
                    }
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
  1190
                    Object fk = attrs.fileKey();
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
  1191
                    if (fk != null) {
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
  1192
                        return  fk.equals(key.attrs.fileKey());
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
  1193
                    } else {
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
  1194
                        return file.equals(key.file);
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
  1195
                    }
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
  1196
                }
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
  1197
                return false;
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
  1198
            }
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
  1199
        }
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
  1200
        private static final HashMap<Key, Source> files = new HashMap<>();
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
  1201
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
  1202
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1203
        static Source get(File file, boolean toDelete) throws IOException {
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
  1204
            Key key = new Key(file,
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
  1205
                              Files.readAttributes(file.toPath(), BasicFileAttributes.class));
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
  1206
            Source src = null;
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
  1207
            synchronized (files) {
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
  1208
                src = files.get(key);
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
  1209
                if (src != null) {
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
  1210
                    src.refs++;
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
  1211
                    return src;
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
  1212
                }
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
  1213
            }
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
  1214
            src = new Source(key, toDelete);
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
  1215
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
  1216
            synchronized (files) {
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
  1217
                if (files.containsKey(key)) {    // someone else put in first
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
  1218
                    src.close();                 // close the newly created one
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
  1219
                    src = files.get(key);
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
  1220
                    src.refs++;
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
  1221
                    return src;
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
  1222
                }
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
  1223
                files.put(key, src);
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
  1224
                return src;
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
  1225
            }
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
  1226
        }
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
  1227
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1228
        static void release(Source src) throws IOException {
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
  1229
            synchronized (files) {
48238
9f225d4387e2 8185582: Update Zip implementation to use Cleaner, not finalizers
sherman
parents: 47987
diff changeset
  1230
                if (src != null && --src.refs == 0) {
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
  1231
                    files.remove(src.key);
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
  1232
                    src.close();
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
  1233
                }
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
  1234
            }
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
  1235
        }
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
  1236
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
  1237
        private Source(Key key, boolean toDelete) throws IOException {
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
  1238
            this.key = key;
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
  1239
            if (toDelete) {
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1240
                if (isWindows) {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1241
                    this.zfile = SharedSecrets.getJavaIORandomAccessFileAccess()
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1242
                                              .openAndDelete(key.file, "r");
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1243
                } else {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1244
                    this.zfile = new RandomAccessFile(key.file, "r");
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1245
                    key.file.delete();
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1246
                }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1247
            } else {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
  1248
                this.zfile = new RandomAccessFile(key.file, "r");
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
  1249
            }
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
  1250
            try {
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
  1251
                initCEN(-1);
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
  1252
                byte[] buf = new byte[4];
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
  1253
                readFullyAt(buf, 0, 4, 0);
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
  1254
                this.startsWithLoc = (LOCSIG(buf) == LOCSIG);
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
  1255
            } catch (IOException x) {
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
  1256
                try {
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
  1257
                    this.zfile.close();
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
  1258
                } catch (IOException xx) {}
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
  1259
                throw x;
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
  1260
            }
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
  1261
        }
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
  1262
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
  1263
        private void close() throws IOException {
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
  1264
            zfile.close();
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
  1265
            zfile = null;
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
  1266
            cen = null;
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
  1267
            entries = null;
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
  1268
            table = null;
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
  1269
            metanames = null;
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
  1270
        }
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
  1271
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
  1272
        private static final int BUF_SIZE = 8192;
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
  1273
        private final int readFullyAt(byte[] buf, int off, int len, long pos)
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
  1274
            throws IOException
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
  1275
        {
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
  1276
            synchronized(zfile) {
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
  1277
                zfile.seek(pos);
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
  1278
                int N = len;
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
  1279
                while (N > 0) {
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
  1280
                    int n = Math.min(BUF_SIZE, N);
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
  1281
                    zfile.readFully(buf, off, n);
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
  1282
                    off += n;
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
  1283
                    N -= n;
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
  1284
                }
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
  1285
                return len;
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
  1286
            }
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
  1287
        }
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
  1288
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
  1289
        private final int readAt(byte[] buf, int off, int len, long pos)
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
  1290
            throws IOException
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
  1291
        {
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
  1292
            synchronized(zfile) {
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
  1293
                zfile.seek(pos);
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
  1294
                return zfile.read(buf, off, len);
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
  1295
            }
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
  1296
        }
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
  1297
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
  1298
        private static final int hashN(byte[] a, int off, int len) {
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
  1299
            int h = 1;
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
  1300
            while (len-- > 0) {
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
  1301
                h = 31 * h + a[off++];
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
  1302
            }
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
  1303
            return h;
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
  1304
        }
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
  1305
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
  1306
        private static final int hash_append(int hash, byte b) {
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
  1307
            return hash * 31 + b;
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
  1308
        }
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
  1309
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
  1310
        private static class End {
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
  1311
            int  centot;     // 4 bytes
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
  1312
            long cenlen;     // 4 bytes
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
  1313
            long cenoff;     // 4 bytes
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
  1314
            long endpos;     // 4 bytes
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
  1315
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
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
  1317
        /*
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
  1318
         * Searches for end of central directory (END) header. The contents of
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
  1319
         * the END header will be read and placed in endbuf. Returns the file
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
  1320
         * position of the END header, otherwise returns -1 if the END header
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
  1321
         * was not found or an error occurred.
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
  1322
         */
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
  1323
        private End findEND() throws IOException {
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
  1324
            long ziplen = zfile.length();
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
  1325
            if (ziplen <= 0)
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
  1326
                zerror("zip file is empty");
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
  1327
            End end = new End();
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
  1328
            byte[] buf = new byte[READBLOCKSZ];
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
  1329
            long minHDR = (ziplen - END_MAXLEN) > 0 ? ziplen - END_MAXLEN : 0;
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
  1330
            long minPos = minHDR - (buf.length - ENDHDR);
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
  1331
            for (long pos = ziplen - buf.length; pos >= minPos; pos -= (buf.length - ENDHDR)) {
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
  1332
                int off = 0;
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
  1333
                if (pos < 0) {
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
  1334
                    // Pretend there are some NUL bytes before start of file
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
  1335
                    off = (int)-pos;
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
  1336
                    Arrays.fill(buf, 0, off, (byte)0);
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
  1337
                }
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
  1338
                int len = buf.length - off;
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
  1339
                if (readFullyAt(buf, off, len, pos + off) != len ) {
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
  1340
                    zerror("zip END header not found");
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
  1341
                }
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
  1342
                // Now scan the block backwards for END header signature
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
  1343
                for (int i = buf.length - ENDHDR; i >= 0; i--) {
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
  1344
                    if (buf[i+0] == (byte)'P'    &&
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
  1345
                        buf[i+1] == (byte)'K'    &&
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
  1346
                        buf[i+2] == (byte)'\005' &&
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
  1347
                        buf[i+3] == (byte)'\006') {
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
  1348
                        // Found ENDSIG header
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
  1349
                        byte[] endbuf = Arrays.copyOfRange(buf, i, i + ENDHDR);
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
  1350
                        end.centot = ENDTOT(endbuf);
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
  1351
                        end.cenlen = ENDSIZ(endbuf);
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
  1352
                        end.cenoff = ENDOFF(endbuf);
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
  1353
                        end.endpos = pos + i;
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
  1354
                        int comlen = ENDCOM(endbuf);
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
  1355
                        if (end.endpos + ENDHDR + comlen != ziplen) {
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
  1356
                            // ENDSIG matched, however the size of file comment in it does
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
  1357
                            // not match the real size. One "common" cause for this problem
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
  1358
                            // is some "extra" bytes are padded at the end of the zipfile.
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
  1359
                            // Let's do some extra verification, we don't care about the
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
  1360
                            // performance in this situation.
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
  1361
                            byte[] sbuf = new byte[4];
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
  1362
                            long cenpos = end.endpos - end.cenlen;
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
  1363
                            long locpos = cenpos - end.cenoff;
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
  1364
                            if  (cenpos < 0 ||
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
  1365
                                 locpos < 0 ||
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
  1366
                                 readFullyAt(sbuf, 0, sbuf.length, cenpos) != 4 ||
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
  1367
                                 GETSIG(sbuf) != CENSIG ||
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
  1368
                                 readFullyAt(sbuf, 0, sbuf.length, locpos) != 4 ||
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
  1369
                                 GETSIG(sbuf) != LOCSIG) {
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
  1370
                                continue;
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
  1371
                            }
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
  1372
                        }
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
  1373
                        if (comlen > 0) {    // this zip file has comlen
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
  1374
                            comment = new byte[comlen];
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
  1375
                            if (readFullyAt(comment, 0, comlen, end.endpos + ENDHDR) != comlen) {
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
  1376
                                zerror("zip comment read failed");
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
  1377
                            }
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
  1378
                        }
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1379
                        // must check for a zip64 end record; it is always permitted to be present
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1380
                        try {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1381
                            byte[] loc64 = new byte[ZIP64_LOCHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1382
                            if (end.endpos < ZIP64_LOCHDR ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1383
                                readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1384
                                != loc64.length || GETSIG(loc64) != ZIP64_LOCSIG) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1385
                                return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1386
                            }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1387
                            long end64pos = ZIP64_LOCOFF(loc64);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1388
                            byte[] end64buf = new byte[ZIP64_ENDHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1389
                            if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1390
                                != end64buf.length || GETSIG(end64buf) != ZIP64_ENDSIG) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1391
                                return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1392
                            }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1393
                            // end64 candidate found,
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1394
                            long cenlen64 = ZIP64_ENDSIZ(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1395
                            long cenoff64 = ZIP64_ENDOFF(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1396
                            long centot64 = ZIP64_ENDTOT(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1397
                            // double-check
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1398
                            if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MAGICVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1399
                                cenoff64 != end.cenoff && end.cenoff != ZIP64_MAGICVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1400
                                centot64 != end.centot && end.centot != ZIP64_MAGICCOUNT) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1401
                                return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1402
                            }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1403
                            // to use the end64 values
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1404
                            end.cenlen = cenlen64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1405
                            end.cenoff = cenoff64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1406
                            end.centot = (int)centot64; // assume total < 2g
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1407
                            end.endpos = end64pos;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1408
                        } catch (IOException x) {}    // no zip64 loc/end
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
  1409
                        return end;
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
  1410
                    }
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
  1411
                }
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
  1412
            }
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
  1413
            zerror("zip END header not found");
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
  1414
            return null; //make compiler happy
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
  1415
        }
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
  1416
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
  1417
        // Reads zip file central directory.
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
  1418
        private void initCEN(int knownTotal) throws IOException {
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
  1419
            if (knownTotal == -1) {
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
  1420
                End end = findEND();
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
  1421
                if (end.endpos == 0) {
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
  1422
                    locpos = 0;
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
  1423
                    total = 0;
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
  1424
                    entries  = new int[0];
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
  1425
                    cen = null;
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
  1426
                    return;         // only END header present
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
  1427
                }
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
  1428
                if (end.cenlen > end.endpos)
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
  1429
                    zerror("invalid END header (bad central directory size)");
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
  1430
                long cenpos = end.endpos - end.cenlen;     // position of CEN table
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
  1431
                // Get position of first local file (LOC) header, taking into
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
  1432
                // account that there may be a stub prefixed to the zip file.
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
  1433
                locpos = cenpos - end.cenoff;
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
  1434
                if (locpos < 0) {
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
  1435
                    zerror("invalid END header (bad central directory offset)");
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
  1436
                }
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
  1437
                // read in the CEN and END
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
  1438
                cen = new byte[(int)(end.cenlen + ENDHDR)];
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
  1439
                if (readFullyAt(cen, 0, cen.length, cenpos) != end.cenlen + ENDHDR) {
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
  1440
                    zerror("read CEN tables failed");
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
  1441
                }
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
  1442
                total = end.centot;
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
  1443
            } else {
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
  1444
                total = knownTotal;
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
  1445
            }
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
  1446
            // hash table for entries
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
  1447
            entries  = new int[total * 3];
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
  1448
            tablelen = ((total/2) | 1); // Odd -> fewer collisions
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
  1449
            table    =  new int[tablelen];
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
  1450
            Arrays.fill(table, ZIP_ENDCHAIN);
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
  1451
            int idx = 0;
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
  1452
            int hash = 0;
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
  1453
            int next = -1;
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
  1454
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
  1455
            // list for all meta entries
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1456
            ArrayList<Integer> metanamesList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
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
  1458
            // Iterate through the entries in the central directory
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
  1459
            int i = 0;
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
  1460
            int hsh = 0;
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
  1461
            int pos = 0;
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
  1462
            int limit = cen.length - ENDHDR;
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
  1463
            while (pos + CENHDR  <= limit) {
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
  1464
                if (i >= total) {
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
  1465
                    // This will only happen if the zip file has an incorrect
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
  1466
                    // ENDTOT field, which usually means it contains more than
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
  1467
                    // 65535 entries.
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
  1468
                    initCEN(countCENHeaders(cen, limit));
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
  1469
                    return;
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
  1470
                }
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
  1471
                if (CENSIG(cen, pos) != CENSIG)
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
  1472
                    zerror("invalid CEN header (bad signature)");
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
  1473
                int method = CENHOW(cen, pos);
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
  1474
                int nlen   = CENNAM(cen, pos);
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
  1475
                int elen   = CENEXT(cen, pos);
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
  1476
                int clen   = CENCOM(cen, pos);
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
  1477
                if ((CENFLG(cen, pos) & 1) != 0)
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
  1478
                    zerror("invalid CEN header (encrypted entry)");
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
  1479
                if (method != STORED && method != DEFLATED)
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
  1480
                    zerror("invalid CEN header (bad compression method: " + method + ")");
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
  1481
                if (pos + CENHDR + nlen > limit)
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
  1482
                    zerror("invalid CEN header (bad header size)");
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
  1483
                // Record the CEN offset and the name hash in our hash cell.
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
  1484
                hash = hashN(cen, pos + CENHDR, nlen);
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
  1485
                hsh = (hash & 0x7fffffff) % tablelen;
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
  1486
                next = table[hsh];
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
  1487
                table[hsh] = idx;
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
  1488
                idx = addEntry(idx, hash, next, pos);
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
  1489
                // Adds name to metanames.
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
  1490
                if (isMetaName(cen, pos + CENHDR, nlen)) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1491
                    if (metanamesList == null)
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1492
                        metanamesList = new ArrayList<>(4);
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1493
                    metanamesList.add(pos);
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
  1494
                }
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
  1495
                // skip ext and comment
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
  1496
                pos += (CENHDR + nlen + elen + clen);
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
  1497
                i++;
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
  1498
            }
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
  1499
            total = i;
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1500
            if (metanamesList != null) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1501
                metanames = new int[metanamesList.size()];
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1502
                for (int j = 0, len = metanames.length; j < len; j++) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1503
                    metanames[j] = metanamesList.get(j);
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1504
                }
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1505
            }
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
  1506
            if (pos + ENDHDR != cen.length) {
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
  1507
                zerror("invalid CEN header (bad header size)");
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
  1508
            }
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
  1509
        }
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
  1510
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
  1511
        private static void zerror(String msg) throws ZipException {
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
  1512
            throw new ZipException(msg);
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
  1513
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
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
  1515
        /*
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
  1516
         * Returns the {@code pos} of the zip cen entry corresponding to the
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
  1517
         * specified entry name, or -1 if not found.
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
  1518
         */
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
  1519
        private int getEntryPos(byte[] name, boolean addSlash) {
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
  1520
            if (total == 0) {
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
  1521
                return -1;
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
  1522
            }
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
  1523
            int hsh = hashN(name, 0, name.length);
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
  1524
            int idx = table[(hsh & 0x7fffffff) % tablelen];
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
  1525
            /*
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
  1526
             * This while loop is an optimization where a double lookup
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
  1527
             * for name and name+/ is being performed. The name char
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
  1528
             * array has enough room at the end to try again with a
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
  1529
             * slash appended if the first table lookup does not succeed.
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
  1530
             */
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
  1531
            while(true) {
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
  1532
                /*
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
  1533
                 * Search down the target hash chain for a entry whose
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
  1534
                 * 32 bit hash matches the hashed name.
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
  1535
                 */
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
  1536
                while (idx != ZIP_ENDCHAIN) {
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
  1537
                    if (getEntryHash(idx) == hsh) {
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
  1538
                        // The CEN name must match the specfied one
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
  1539
                        int pos = getEntryPos(idx);
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
  1540
                        if (name.length == CENNAM(cen, pos)) {
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
  1541
                            boolean matched = true;
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
  1542
                            int nameoff = pos + CENHDR;
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
  1543
                            for (int i = 0; i < name.length; i++) {
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
  1544
                                if (name[i] != cen[nameoff++]) {
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
  1545
                                    matched = false;
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
  1546
                                    break;
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
  1547
                                }
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
  1548
                            }
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
  1549
                            if (matched) {
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
  1550
                                return pos;
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
  1551
                            }
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
  1552
                         }
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
  1553
                    }
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
  1554
                    idx = getEntryNext(idx);
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
  1555
                }
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
  1556
                /* If not addSlash, or slash is already there, we are done */
34874
55a62be87e89 8146431: j.u.z.ZipFile.getEntry("") throws AIOOBE
sherman
parents: 34774
diff changeset
  1557
                if (!addSlash  || name.length == 0 || name[name.length - 1] == '/') {
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
  1558
                     return -1;
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
  1559
                }
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
  1560
                /* Add slash and try once more */
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
  1561
                name = Arrays.copyOf(name, name.length + 1);
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
  1562
                name[name.length - 1] = '/';
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
  1563
                hsh = hash_append(hsh, (byte)'/');
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
  1564
                //idx = table[hsh % tablelen];
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
  1565
                idx = table[(hsh & 0x7fffffff) % tablelen];
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
  1566
                addSlash = false;
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
  1567
            }
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
  1568
        }
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
  1569
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1570
        /**
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1571
         * Returns true if the bytes represent a non-directory name
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1572
         * beginning with "META-INF/", disregarding ASCII case.
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
  1573
         */
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1574
        private static boolean isMetaName(byte[] name, int off, int len) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1575
            // Use the "oldest ASCII trick in the book"
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1576
            return len > 9                     // "META-INF/".length()
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1577
                && name[off + len - 1] != '/'  // non-directory
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1578
                && (name[off++] | 0x20) == 'm'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1579
                && (name[off++] | 0x20) == 'e'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1580
                && (name[off++] | 0x20) == 't'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1581
                && (name[off++] | 0x20) == 'a'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1582
                && (name[off++]       ) == '-'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1583
                && (name[off++] | 0x20) == 'i'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1584
                && (name[off++] | 0x20) == 'n'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1585
                && (name[off++] | 0x20) == 'f'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1586
                && (name[off]         ) == '/';
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
  1587
        }
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
  1588
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1589
        /**
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1590
         * Returns the number of CEN headers in a central directory.
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1591
         * Will not throw, even if the zip file is corrupt.
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1592
         *
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1593
         * @param cen copy of the bytes in a zip file's central directory
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1594
         * @param size number of bytes in central directory
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
  1595
         */
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1596
        private static int countCENHeaders(byte[] cen, int size) {
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
  1597
            int count = 0;
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1598
            for (int p = 0;
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1599
                 p + CENHDR <= size;
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1600
                 p += CENHDR + CENNAM(cen, p) + CENEXT(cen, p) + CENCOM(cen, p))
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
  1601
                count++;
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
  1602
            return count;
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
  1603
        }
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
  1604
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
}