jdk/src/java.base/share/classes/java/util/zip/ZipFile.java
author mli
Wed, 31 May 2017 19:54:16 -0700
changeset 45434 4582657c7260
parent 44534 a076dffbc2c1
permissions -rw-r--r--
8181082: class-level since tag issues in java.base & java.datatransfer module Reviewed-by: alanb, serb
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;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    34
import java.nio.charset.Charset;
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
    35
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
    36
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
    37
import java.nio.file.Path;
29ea8310a27a 8145260: To bring j.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.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
    39
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    40
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
    41
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
    42
import java.util.Arrays;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    43
import java.util.Deque;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Enumeration;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    45
import java.util.HashMap;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    46
import java.util.Iterator;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    47
import java.util.Map;
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
    48
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.NoSuchElementException;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    50
import java.util.Spliterator;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    51
import java.util.Spliterators;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    52
import java.util.WeakHashMap;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    53
import java.util.stream.Stream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    54
import java.util.stream.StreamSupport;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    55
import jdk.internal.misc.JavaUtilZipFileAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    56
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
    57
import jdk.internal.misc.JavaIORandomAccessFileAccess;
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
    58
import jdk.internal.misc.VM;
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34874
diff changeset
    59
import jdk.internal.perf.PerfCounter;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    60
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
    61
import static java.util.zip.ZipConstants.*;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    62
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
    63
import static java.util.zip.ZipUtils.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * This class is used to read entries from a zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
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
    68
 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44534
diff changeset
    73
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
    76
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
    77
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
    78
    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
    79
    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
    80
    private Source zsrc;
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
    81
    private ZipCoder zc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Mode flag to open a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final int OPEN_READ = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Mode flag to open a zip file and mark it for deletion.  The file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * deleted some time between the moment that it is opened and the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * 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
    95
     * {@code ZipFile} object until either the close method is invoked or the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * virtual machine exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static final int OPEN_DELETE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Opens a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
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
   103
     * <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
   104
     * 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
   105
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   106
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   107
     * <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
   108
     * decode the entry names and comments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param name the name of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @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
   114
     *         {@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
   115
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public ZipFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this(new File(name), OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
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
   123
     * 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
   124
     * {@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
   125
     * must be either {@code OPEN_READ} or {@code OPEN_READ | OPEN_DELETE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
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
   127
     * <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
   128
     * method is called with the {@code name} argument as its argument to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   131
     * <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
   132
     * decode the entry names and comments
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   133
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @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
   139
     *         its {@code checkRead} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         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
   141
     *         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
   142
     *         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
   143
     * @throws IllegalArgumentException if the {@code mode} argument is invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public ZipFile(File file, int mode) throws IOException {
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
   148
        this(file, mode, StandardCharsets.UTF_8);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   149
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   150
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   151
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   152
     * 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
   153
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   154
     * <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
   155
     * decode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   156
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   157
     * @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
   158
     * @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
   159
     * @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
   160
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   161
    public ZipFile(File file) throws ZipException, IOException {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   162
        this(file, OPEN_READ);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   163
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   164
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   165
    /**
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
     * 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
   167
     * {@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
   168
     * 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
   169
     *
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
   170
     * <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
   171
     * 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
   172
     * ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   173
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   174
     * @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
   175
     * @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
   176
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   177
     *        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
   178
     *        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
   179
     *        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
   180
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   181
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   182
     * @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
   183
     * @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
   184
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   185
     * @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
   186
     *         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
   187
     *         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
   188
     *         {@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
   189
     *         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
   190
     *
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
   191
     * @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
   192
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   193
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   194
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   195
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   196
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   197
    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
   198
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (((mode & OPEN_READ) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new IllegalArgumentException("Illegal mode: 0x"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                               Integer.toHexString(mode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            sm.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if ((mode & OPEN_DELETE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                sm.checkDelete(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
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
   212
        Objects.requireNonNull(charset, "charset");
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   213
        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
   214
        this.name = name;
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   215
        long t0 = System.nanoTime();
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
   216
        this.zsrc = Source.get(file, (mode & OPEN_DELETE) != 0);
34953
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34874
diff changeset
   217
        PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
67245e3259bf 8146736: Move sun.misc performance counters to jdk.internal.perf
chegar
parents: 34874
diff changeset
   218
        PerfCounter.getZipFileCount().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
2592
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
     * Opens a zip file for reading.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   223
     *
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
   224
     * <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
   225
     * 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
   226
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   227
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   228
     * @param name the name of the zip file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   229
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   230
     *        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
   231
     *        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
   232
     *        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
   233
     *        purpose flag).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   234
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   235
     * @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
   236
     * @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
   237
     * @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
   238
     *         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
   239
     *         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
   240
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   241
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   242
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   243
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   244
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   245
    public ZipFile(String name, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   246
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   247
        this(new File(name), OPEN_READ, charset);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   248
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * 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
   252
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @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
   254
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   255
     *        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
   256
     *        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
   257
     *        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
   258
     *        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
   259
     *        flag is set).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   260
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   261
     * @throws ZipException if a ZIP format error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @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
   263
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   264
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   266
    public ZipFile(File file, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   267
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   268
        this(file, OPEN_READ, charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   272
     * Returns the zip file comment, or null if none.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   273
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   274
     * @return the comment string for the zip file, or null if none
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   275
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   276
     * @throws IllegalStateException if the zip file has been closed
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   277
     *
43794
497288c158bf 8173094: Error in API documentation for SwingWorker
coffeys
parents: 42444
diff changeset
   278
     * @since 1.7
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   279
     */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   280
    public String getComment() {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   281
        synchronized (this) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   282
            ensureOpen();
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
   283
            if (zsrc.comment == null) {
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   284
                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
   285
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   286
            return zc.toString(zsrc.comment);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   287
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   288
    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   289
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   290
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the zip file entry for the specified name, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param name the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return the zip file entry, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public ZipEntry getEntry(String name) {
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
   299
        Objects.requireNonNull(name, "name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            ensureOpen();
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   302
            byte[] bname = zc.getBytes(name);
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   303
            int pos = 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
   304
            if (pos != -1) {
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   305
                return getZipEntry(name, bname, pos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
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
   311
    // The outstanding inputstreams that need to be closed,
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   312
    // mapped to the inflater objects they use.
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   313
    private final Map<InputStream, Inflater> streams = new WeakHashMap<>();
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   314
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * 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
   318
     * <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
   319
     * 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
   320
     * have been returned by invocations of this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param entry the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return the input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    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
   330
        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
   331
        int pos = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        ZipFileInputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            ensureOpen();
42444
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   335
            if (Objects.equals(lastEntryName, entry.name)) {
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   336
                pos = lastEntryPos;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   337
            } 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
   338
                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
   339
            } 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
   340
                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
   341
            }
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
   342
            if (pos == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
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
   345
            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
   346
            switch (CENHOW(zsrc.cen, pos)) {
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   347
            case STORED:
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   348
                synchronized (streams) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   349
                    streams.put(in, null);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   350
                }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   351
                return in;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   352
            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
   353
                // Inflater likes a bit of slack
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   354
                // 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
   355
                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
   356
                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
   357
                    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
   358
                }
29ea8310a27a 8145260: To bring j.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
                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
   360
                    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
   361
                }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   362
                Inflater inf = getInflater();
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
   363
                InputStream is = new ZipFileInflaterInputStream(in, inf, (int)size);
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   364
                synchronized (streams) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   365
                    streams.put(is, inf);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   366
                }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   367
                return is;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   368
            default:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   369
                throw new ZipException("invalid compression method");
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   370
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   374
    private class ZipFileInflaterInputStream extends InflaterInputStream {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34693
diff changeset
   375
        private volatile boolean closeRequested;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   376
        private boolean eof = false;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   377
        private final ZipFileInputStream zfin;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   378
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   379
        ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf,
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   380
                int size) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   381
            super(zfin, inf, size);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   382
            this.zfin = zfin;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   383
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   384
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   385
        public void close() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   386
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   387
                return;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   388
            closeRequested = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   389
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   390
            super.close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   391
            Inflater inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   392
            synchronized (streams) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   393
                inf = streams.remove(this);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   394
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   395
            if (inf != null) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   396
                releaseInflater(inf);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   397
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   398
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   399
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   400
        // Override fill() method to provide an extra "dummy" byte
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   401
        // at the end of the input stream. This is required when
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   402
        // using the "nowrap" Inflater option.
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   403
        protected void fill() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   404
            if (eof) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   405
                throw new EOFException("Unexpected end of ZLIB input stream");
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   406
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   407
            len = in.read(buf, 0, buf.length);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   408
            if (len == -1) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   409
                buf[0] = 0;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   410
                len = 1;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   411
                eof = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   412
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   413
            inf.setInput(buf, 0, len);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   414
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   415
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   416
        public int available() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   417
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   418
                return 0;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   419
            long avail = zfin.size() - inf.getBytesWritten();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   420
            return (avail > (long) Integer.MAX_VALUE ?
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   421
                    Integer.MAX_VALUE : (int) avail);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   422
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   423
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   424
        @SuppressWarnings("deprecation")
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   425
        protected void finalize() throws Throwable {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   426
            close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   427
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   428
    }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   429
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Gets an inflater from the list of available inflaters or allocates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private Inflater getInflater() {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   435
        Inflater inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   436
        synchronized (inflaterCache) {
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
   437
            while ((inf = inflaterCache.poll()) != 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
   438
                if (!inf.ended()) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   439
                    return inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   440
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   443
        return new Inflater(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Releases the specified inflater to the list of available inflaters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    private void releaseInflater(Inflater inf) {
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
   450
        if (!inf.ended()) {
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
   451
            inf.reset();
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   452
            synchronized (inflaterCache) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   453
                inflaterCache.add(inf);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   454
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    // List of available Inflater objects for decompression
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
   459
    private final Deque<Inflater> inflaterCache = new ArrayDeque<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Returns the path name of the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @return the path name of the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   469
    private class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   470
        private int i = 0;
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   471
        private final int entryCount;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   472
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   473
        public ZipEntryIterator() {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   474
            synchronized (ZipFile.this) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   475
                ensureOpen();
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   476
                this.entryCount = zsrc.total;
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   477
            }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   478
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   479
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   480
        public boolean hasMoreElements() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   481
            return hasNext();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   482
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   483
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   484
        public boolean hasNext() {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   485
            return i < entryCount;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   486
        }
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
        public ZipEntry nextElement() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   489
            return next();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   490
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   491
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   492
        public ZipEntry next() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   493
            synchronized (ZipFile.this) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   494
                ensureOpen();
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   495
                if (!hasNext()) {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   496
                    throw new NoSuchElementException();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   497
                }
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
   498
                // each "entry" has 3 ints in table entries
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   499
                return getZipEntry(null, null, zsrc.getEntryPos(i++ * 3));
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   500
            }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   501
        }
31469
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   502
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   503
        public Iterator<ZipEntry> asIterator() {
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   504
            return this;
92cc72d2a11a 8081678: Add Stream returning methods to classes where there currently exist only Enumeration returning methods
psandoz
parents: 30786
diff changeset
   505
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   506
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   507
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Returns an enumeration of the ZIP file entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @return an enumeration of the ZIP file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public Enumeration<? extends ZipEntry> entries() {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   514
        return new ZipEntryIterator();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   515
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   516
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   517
    /**
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29226
diff changeset
   518
     * Returns an ordered {@code Stream} over the ZIP file entries.
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   519
     * 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
   520
     * the central directory of the ZIP file.
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
     * @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
   523
     * @throws IllegalStateException if the zip file has been closed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   524
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   525
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   526
    public Stream<? extends ZipEntry> stream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   527
        return StreamSupport.stream(Spliterators.spliterator(
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   528
                new ZipEntryIterator(), size(),
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   529
                Spliterator.ORDERED | Spliterator.DISTINCT |
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18253
diff changeset
   530
                        Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
42444
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   533
    private String lastEntryName;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   534
    private int lastEntryPos;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   535
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
   536
    /* Checks ensureOpen() before invoke this method */
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   537
    private ZipEntry getZipEntry(String name, byte[] bname, int 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
   538
        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
   539
        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
   540
        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
   541
        int clen = CENCOM(cen, pos);
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   542
        int flag = CENFLG(cen, pos);
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   543
        if (name == null || bname.length != nlen) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   544
            // 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
   545
            // (1) null, invoked from iterator, or
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 38467
diff changeset
   546
            // (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
   547
            // getEntryPos() search.
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   548
            if (!zc.isUTF8() && (flag & EFS) != 0) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   549
                name = zc.toStringUTF8(cen, pos + CENHDR, nlen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   550
            } else {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   551
                name = zc.toString(cen, pos + CENHDR, nlen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   552
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   553
        }
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   554
        ZipEntry e = new ZipEntry(name);
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   555
        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
   556
        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
   557
        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
   558
        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
   559
        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
   560
        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
   561
        if (elen != 0) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   562
            int start = pos + CENHDR + nlen;
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   563
            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
   564
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   565
        if (clen != 0) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   566
            int start = pos + CENHDR + nlen + elen;
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   567
            if (!zc.isUTF8() && (flag & EFS) != 0) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   568
                e.comment = zc.toStringUTF8(cen, start, clen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   569
            } else {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   570
                e.comment = zc.toString(cen, start, clen);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   571
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   572
        }
42444
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   573
        lastEntryName = e.name;
628321b5a236 8170831: ZipFile implementation no longer caches the last accessed entry/pos
sherman
parents: 39310
diff changeset
   574
        lastEntryPos = pos;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   575
        return e;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   576
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   577
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * 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
   580
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return the number of entries in the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    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
   585
        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
   586
            ensureOpen();
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   587
            return zsrc.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
   588
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Closes the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * <p> Closing this ZIP file will close all of the input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * previously returned by invocations of the {@link #getInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * getInputStream} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    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
   600
        if (closeRequested) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   601
            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
   602
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   603
        closeRequested = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   605
        synchronized (this) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   606
            // Close streams, release their inflaters
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   607
            synchronized (streams) {
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
   608
                if (!streams.isEmpty()) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   609
                    Map<InputStream, Inflater> copy = new HashMap<>(streams);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   610
                    streams.clear();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   611
                    for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   612
                        e.getKey().close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   613
                        Inflater inf = e.getValue();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   614
                        if (inf != null) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   615
                            inf.end();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   616
                        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   617
                    }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   618
                }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   619
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   620
            // Release cached inflaters
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   621
            synchronized (inflaterCache) {
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
   622
                Inflater inf;
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   623
                while ((inf = inflaterCache.poll()) != null) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   624
                    inf.end();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   625
                }
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   626
            }
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
   627
            // Release zip 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
   628
            if (zsrc != 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
   629
                Source.close(zsrc);
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   630
                zsrc = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   636
     * Ensures that the system resources held by this ZipFile object are
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   637
     * released when there are no more references to it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Since the time when GC would invoke this method is undetermined,
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
   641
     * it is strongly recommended that applications invoke the {@code close}
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
   642
     * method as soon they have finished accessing this {@code ZipFile}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * This will prevent holding up system resources for an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * length of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   646
     * @deprecated The {@code finalize} method has been deprecated.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   647
     *     Subclasses that override {@code finalize} in order to perform cleanup
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   648
     *     should be modified to use alternative cleanup mechanisms and
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   649
     *     to remove the overriding {@code finalize} method.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   650
     *     When overriding the {@code finalize} method, its implementation must explicitly
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   651
     *     ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   652
     *     See the specification for {@link Object#finalize()} for further
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   653
     *     information about migration options.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @see    java.util.zip.ZipFile#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   657
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            throw new IllegalStateException("zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
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
   666
        if (zsrc == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            throw new IllegalStateException("The object is not initialized.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    private void ensureOpenOrZipException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            throw new ZipException("ZipFile closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Inner class implementing the input stream used to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * (possibly compressed) zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
   private class ZipFileInputStream extends InputStream {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34693
diff changeset
   682
        private volatile boolean closeRequested;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        private   long pos;     // current position within entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        protected long rem;     // number of remaining bytes within entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        protected long size;    // uncompressed size of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
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
   687
        ZipFileInputStream(byte[] cen, int cenpos) 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
   688
            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
   689
            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
   690
            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
   691
            // 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
   692
            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
   693
                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
   694
                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
   695
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   696
            // negative for lazy initialization, see getDataOffset();
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   697
            pos = - (pos + ZipFile.this.zsrc.locpos);
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   698
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   699
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   700
        private void checkZIP64(byte[] cen, int cenpos) 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
   701
            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
   702
            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
   703
            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
   704
                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
   705
                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
   706
                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
   707
                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
   708
                    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
   709
                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
   710
                    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
   711
                        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
   712
                            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
   713
                        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
   714
                        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
   715
                        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
   716
                    }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   717
                    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
   718
                        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
   719
                            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
   720
                        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
   721
                        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
   722
                        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
   723
                    }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   724
                    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
   725
                        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
   726
                            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
   727
                        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
   728
                        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
   729
                        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
   730
                    }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   731
                    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
   732
                }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   733
                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
   734
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   735
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   736
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   737
       /* 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
   738
        * 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
   739
        * 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
   740
        * 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
   741
        */
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   742
        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
   743
            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
   744
                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
   745
                pos = -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
   746
                int len = ZipFile.this.zsrc.readFullyAt(loc, 0, loc.length, 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
   747
                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
   748
                    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
   749
                }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   750
                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
   751
                    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
   752
                }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   753
                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
   754
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   755
            return pos;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            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
   760
                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
   761
                initDataOffset();
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   762
                if (rem == 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   763
                    return -1;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   764
                }
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
   765
                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
   766
                    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
   767
                }
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   768
                if (len <= 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   769
                    return 0;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   770
                }
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
   771
                len = ZipFile.this.zsrc.readAt(b, off, len, pos);
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   772
                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
   773
                    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
   774
                    rem -= len;
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   775
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if (read(b, 0, 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
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
   792
        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
   793
            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
   794
                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
   795
                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
   796
                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
   797
                    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
   798
                }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   799
                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
   800
                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
   801
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        public long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        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
   817
            if (closeRequested) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   818
                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
   819
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   820
            closeRequested = true;
34526
f1f852f5f477 8144958: changes by JDK-8142508 seems to have broken jtreg
sherman
parents: 34525
diff changeset
   821
            rem = 0;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   822
            synchronized (streams) {
2915
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   823
                streams.remove(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   826
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43794
diff changeset
   827
        @SuppressWarnings("deprecation")
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   828
        protected void finalize() {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   829
            close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   830
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   833
    /**
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
   834
     * Returns the names of all non-directory entries that begin with
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
   835
     * "META-INF/" (case ignored). This method is used in JarFile, via
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
   836
     * SharedSecrets, as an optimization when looking up manifest and
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
   837
     * signature file entries. Returns null if no entries were found.
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   838
     */
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
   839
    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
   840
        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
   841
            ensureOpen();
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   842
            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
   843
                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
   844
            }
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   845
            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
   846
            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
   847
            for (int i = 0; i < names.length; i++) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   848
                int pos = zsrc.metanames[i];
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   849
                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
   850
                                      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
   851
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   852
            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
   853
        }
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   854
    }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   855
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   856
    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
   857
    static {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   858
        SharedSecrets.setJavaUtilZipFileAccess(
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   859
            new JavaUtilZipFileAccess() {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   860
                public boolean startsWithLocHeader(ZipFile zip) {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   861
                    return zip.zsrc.startsWithLoc;
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   862
                }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   863
                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
   864
                    return zip.getMetaInfEntryNames();
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   865
                }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   866
             }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   867
        );
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   868
        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
   869
    }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   870
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
   871
    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
   872
        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
   873
        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
   874
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   875
        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
   876
        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
   877
        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
   878
        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
   879
                                             // list of meta entries in META-INF dir
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
   880
        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
   881
        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
   882
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   883
        // 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
   884
        //
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   885
        // 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
   886
        // 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
   887
        // 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
   888
        // 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
   889
        //
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   890
        // 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
   891
        //     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
   892
        //     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
   893
        //     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
   894
        // }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   895
        // 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
   896
        //
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   897
        // 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
   898
        // {@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
   899
        // 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
   900
        //
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   901
        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
   902
        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
   903
            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
   904
            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
   905
            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
   906
            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
   907
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   908
        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
   909
        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
   910
        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
   911
        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
   912
        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
   913
        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
   914
        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
   915
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   916
        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
   917
            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
   918
            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
   919
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   920
            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
   921
                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
   922
                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
   923
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   924
29ea8310a27a 8145260: To bring j.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
            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
   926
                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
   927
                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
   928
            }
29ea8310a27a 8145260: To bring j.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
29ea8310a27a 8145260: To bring j.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
            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
   931
                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
   932
                    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
   933
                    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
   934
                        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
   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
                    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
   937
                    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
   938
                        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
   939
                    } 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
   940
                        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
   941
                    }
29ea8310a27a 8145260: To bring j.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
                }
29ea8310a27a 8145260: To bring j.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
                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
   944
            }
29ea8310a27a 8145260: To bring j.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
        }
29ea8310a27a 8145260: To bring j.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
        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
   947
29ea8310a27a 8145260: To bring j.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
29ea8310a27a 8145260: To bring j.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
        public static Source get(File file, 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
   950
            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
   951
                              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
   952
            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
   953
            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
   954
                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
   955
                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
   956
                    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
   957
                    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
   958
                }
29ea8310a27a 8145260: To bring j.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
            }
29ea8310a27a 8145260: To bring j.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
            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
   961
29ea8310a27a 8145260: To bring j.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
            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
   963
                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
   964
                    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
   965
                    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
   966
                    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
   967
                    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
   968
                }
29ea8310a27a 8145260: To bring j.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
                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
   970
                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
   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
        private static void close(Source src) 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
   975
            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
   976
                if (--src.refs == 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
   977
                    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
   978
                    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
   979
                }
29ea8310a27a 8145260: To bring j.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
            }
29ea8310a27a 8145260: To bring j.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
        }
29ea8310a27a 8145260: To bring j.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
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   983
        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
   984
            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
   985
            if (toDelete) {
38466
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   986
                if (isWindows) {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   987
                    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
   988
                                              .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
   989
                } else {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   990
                    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
   991
                    key.file.delete();
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   992
                }
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   993
            } else {
4bcf5f2bb351 8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag
sherman
parents: 38375
diff changeset
   994
                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
   995
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   996
            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
   997
                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
   998
                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
   999
                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
  1000
                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
  1001
            } 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
  1002
                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
  1003
                    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
  1004
                } 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
  1005
                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
  1006
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1007
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1008
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1009
        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
  1010
            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
  1011
            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
  1012
            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
  1013
            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
  1014
            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
  1015
            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
  1016
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1017
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1018
        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
  1019
        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
  1020
            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
  1021
        {
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1022
            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
  1023
                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
  1024
                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
  1025
                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
  1026
                    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
  1027
                    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
  1028
                    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
  1029
                    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
  1030
                }
29ea8310a27a 8145260: To bring j.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
                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
  1032
            }
29ea8310a27a 8145260: To bring j.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
        }
29ea8310a27a 8145260: To bring j.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
        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
  1036
            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
  1037
        {
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1038
            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
  1039
                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
  1040
                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
  1041
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1042
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1043
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1044
        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
  1045
            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
  1046
            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
  1047
                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
  1048
            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1049
            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
  1050
        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1051
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1052
        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
  1053
            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
  1054
        }
29ea8310a27a 8145260: To bring j.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
        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
  1057
            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
  1058
            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
  1059
            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
  1060
            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
  1061
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
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
  1063
        /*
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1064
         * 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
  1065
         * 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
  1066
         * 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
  1067
         * 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
  1068
         */
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1069
        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
  1070
            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
  1071
            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
  1072
                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
  1073
            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
  1074
            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
  1075
            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
  1076
            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
  1077
            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
  1078
                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
  1079
                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
  1080
                    // 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
  1081
                    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
  1082
                    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
  1083
                }
29ea8310a27a 8145260: To bring j.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
                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
  1085
                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
  1086
                    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
  1087
                }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1088
                // 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
  1089
                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
  1090
                    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
  1091
                        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
  1092
                        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
  1093
                        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
  1094
                        // 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
  1095
                        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
  1096
                        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
  1097
                        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
  1098
                        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
  1099
                        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
  1100
                        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
  1101
                        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
  1102
                            // 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
  1103
                            // 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
  1104
                            // 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
  1105
                            // 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
  1106
                            // 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
  1107
                            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
  1108
                            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
  1109
                            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
  1110
                            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
  1111
                                 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
  1112
                                 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
  1113
                                 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
  1114
                                 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
  1115
                                 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
  1116
                                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
  1117
                            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1118
                        }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1119
                        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
  1120
                            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
  1121
                            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
  1122
                                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
  1123
                            }
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
  1124
                        }
29ea8310a27a 8145260: To bring j.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
                        if (end.cenlen == 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
  1126
                            end.cenoff == 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
  1127
                            end.centot == ZIP64_MAGICCOUNT)
29ea8310a27a 8145260: To bring j.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
                            // need to find the zip64 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
  1130
                            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
  1131
                                byte[] loc64 = new byte[ZIP64_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
  1132
                                if (readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_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
  1133
                                    != loc64.length || GETSIG(loc64) != ZIP64_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
  1134
                                    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
  1135
                                }
29ea8310a27a 8145260: To bring j.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
                                long end64pos = ZIP64_LOCOFF(loc64);
29ea8310a27a 8145260: To bring j.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
                                byte[] end64buf = new byte[ZIP64_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
  1138
                                if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
29ea8310a27a 8145260: To bring j.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
                                    != end64buf.length || GETSIG(end64buf) != ZIP64_ENDSIG) {
29ea8310a27a 8145260: To bring j.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
                                    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
  1141
                                }
29ea8310a27a 8145260: To bring j.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
                                // end64 found, re-calcualte everything.
29ea8310a27a 8145260: To bring j.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
                                end.cenlen = ZIP64_ENDSIZ(end64buf);
29ea8310a27a 8145260: To bring j.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
                                end.cenoff = ZIP64_ENDOFF(end64buf);
29ea8310a27a 8145260: To bring j.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
                                end.centot = (int)ZIP64_ENDTOT(end64buf); // assume total < 2g
29ea8310a27a 8145260: To bring j.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
                                end.endpos = end64pos;
29ea8310a27a 8145260: To bring j.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
                            } catch (IOException x) {}    // no zip64 loc/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
  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
                        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
  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
                }
29ea8310a27a 8145260: To bring j.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
            }
29ea8310a27a 8145260: To bring j.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
            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
  1154
            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
  1155
        }
29ea8310a27a 8145260: To bring j.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
29ea8310a27a 8145260: To bring j.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
        // 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
  1158
        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
  1159
            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
  1160
                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
  1161
                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
  1162
                    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
  1163
                    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
  1164
                    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
  1165
                    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
  1166
                    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
  1167
                }
29ea8310a27a 8145260: To bring j.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
                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
  1169
                    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
  1170
                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
  1171
                // 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
  1172
                // 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
  1173
                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
  1174
                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
  1175
                    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
  1176
                }
29ea8310a27a 8145260: To bring j.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
                // 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
  1178
                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
  1179
                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
  1180
                    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
  1181
                }
29ea8310a27a 8145260: To bring j.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
                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
  1183
            } 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
  1184
                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
  1185
            }
29ea8310a27a 8145260: To bring j.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
            // 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
  1187
            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
  1188
            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
  1189
            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
  1190
            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
  1191
            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
  1192
            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
  1193
            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
  1194
29ea8310a27a 8145260: To bring j.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
            // list for all meta entries
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1196
            ArrayList<Integer> metanamesList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
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
  1198
            // 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
  1199
            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
  1200
            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
  1201
            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
  1202
            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
  1203
            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
  1204
                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
  1205
                    // 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
  1206
                    // 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
  1207
                    // 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
  1208
                    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
  1209
                    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
  1210
                }
29ea8310a27a 8145260: To bring j.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
                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
  1212
                    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
  1213
                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
  1214
                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
  1215
                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
  1216
                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
  1217
                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
  1218
                    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
  1219
                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
  1220
                    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
  1221
                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
  1222
                    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
  1223
                // 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
  1224
                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
  1225
                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
  1226
                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
  1227
                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
  1228
                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
  1229
                // 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
  1230
                if (isMetaName(cen, pos + CENHDR, nlen)) {
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1231
                    if (metanamesList == null)
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1232
                        metanamesList = new ArrayList<>(4);
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1233
                    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
  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
                // 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
  1236
                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
  1237
                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
  1238
            }
29ea8310a27a 8145260: To bring j.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
            total = i;
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1240
            if (metanamesList != null) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1241
                metanames = new int[metanamesList.size()];
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1242
                for (int j = 0, len = metanames.length; j < len; j++) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1243
                    metanames[j] = metanamesList.get(j);
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1244
                }
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1245
            }
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
  1246
            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
  1247
                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
  1248
            }
29ea8310a27a 8145260: To bring j.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
29ea8310a27a 8145260: To bring j.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
        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
  1252
            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
  1253
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
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
  1255
        /*
29ea8310a27a 8145260: To bring j.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
         * 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
  1257
         * 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
  1258
         */
29ea8310a27a 8145260: To bring j.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
        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
  1260
            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
  1261
                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
  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
            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
  1264
            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
  1265
            /*
29ea8310a27a 8145260: To bring j.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
             * 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
  1267
             * 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
  1268
             * 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
  1269
             * 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
  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
            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
  1272
                /*
29ea8310a27a 8145260: To bring j.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
                 * 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
  1274
                 * 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
  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
                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
  1277
                    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
  1278
                        // 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
  1279
                        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
  1280
                        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
  1281
                            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
  1282
                            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
  1283
                            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
  1284
                                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
  1285
                                    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
  1286
                                    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
  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
                            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
  1290
                                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
  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
                         }
29ea8310a27a 8145260: To bring j.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
                    }
29ea8310a27a 8145260: To bring j.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
                    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
  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
                /* 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
  1297
                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
  1298
                     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
  1299
                }
29ea8310a27a 8145260: To bring j.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
                /* 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
  1301
                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
  1302
                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
  1303
                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
  1304
                //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
  1305
                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
  1306
                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
  1307
            }
29ea8310a27a 8145260: To bring j.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
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1310
        /**
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1311
         * Returns true if the bytes represent a non-directory name
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1312
         * 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
  1313
         */
38375
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1314
        private static boolean isMetaName(byte[] name, int off, int len) {
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1315
            // Use the "oldest ASCII trick in the book"
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1316
            return len > 9                     // "META-INF/".length()
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1317
                && name[off + len - 1] != '/'  // non-directory
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1318
                && (name[off++] | 0x20) == 'm'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1319
                && (name[off++] | 0x20) == 'e'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1320
                && (name[off++] | 0x20) == 't'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1321
                && (name[off++] | 0x20) == 'a'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1322
                && (name[off++]       ) == '-'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1323
                && (name[off++] | 0x20) == 'i'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1324
                && (name[off++] | 0x20) == 'n'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1325
                && (name[off++] | 0x20) == 'f'
5df893ca926c 8157069: Assorted ZipFile improvements
martin
parents: 34953
diff changeset
  1326
                && (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
  1327
        }
29ea8310a27a 8145260: To bring j.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
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1329
        /**
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1330
         * Returns the number of CEN headers in a central directory.
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1331
         * Will not throw, even if the zip file is corrupt.
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1332
         *
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1333
         * @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
  1334
         * @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
  1335
         */
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1336
        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
  1337
            int count = 0;
38467
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1338
            for (int p = 0;
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1339
                 p + CENHDR <= size;
172e0c9007a6 8157613: Internal documentation improvements to ZipFile.java
martin
parents: 38466
diff changeset
  1340
                 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
  1341
                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
  1342
            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
  1343
        }
29ea8310a27a 8145260: To bring j.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
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
}