jdk/src/java.base/share/classes/java/util/zip/ZipFile.java
author sherman
Fri, 22 May 2015 08:56:33 -0700
changeset 30786 fb0d6aaea1dc
parent 29226 b675016fabfd
child 31469 92cc72d2a11a
permissions -rw-r--r--
8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip Summary: update the api doc for entries()/stream() accordingly Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
     2
 * Copyright (c) 1995, 2015, 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;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    33
import java.nio.charset.Charset;
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
    34
import java.nio.charset.StandardCharsets;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    35
import java.util.ArrayDeque;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    36
import java.util.Deque;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    38
import java.util.HashMap;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    39
import java.util.Iterator;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    40
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.NoSuchElementException;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    42
import java.util.Spliterator;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    43
import java.util.Spliterators;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    44
import java.util.WeakHashMap;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    45
import java.util.stream.Stream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    46
import java.util.stream.StreamSupport;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
    47
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
    48
import static java.util.zip.ZipConstants64.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This class is used to read entries from a zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
    60
class ZipFile implements ZipConstants, Closeable {
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
    61
    private long jzfile;           // address of jzfile data
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
    62
    private final String name;     // zip file name
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
    63
    private final int total;       // total number of entries
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
    64
    private final boolean locsig;  // if zip file starts with LOCSIG (usually true)
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
    65
    private volatile boolean closeRequested = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Mode flag to open a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final int OPEN_READ = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Mode flag to open a zip file and mark it for deletion.  The file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * deleted some time between the moment that it is opened and the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * that it is closed, but its contents will remain accessible via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <tt>ZipFile</tt> object until either the close method is invoked or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * virtual machine exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int OPEN_DELETE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    91
    private static final boolean usemmap;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    92
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    93
    static {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    94
        // A system prpperty to disable mmap use to avoid vm crash when
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    95
        // in-use zip file is accidently overwritten by others.
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
    96
        String prop = sun.misc.VM.getSavedProperty("sun.zip.disableMemoryMapping");
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    97
        usemmap = (prop == null ||
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    98
                   !(prop.length() == 0 || prop.equalsIgnoreCase("true")));
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
    99
    }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   100
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Opens a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   104
     * <p>First, if there is a security manager, its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   105
     * method is called with the <code>name</code> argument as its argument
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   106
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   107
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   108
     * <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
   109
     * decode the entry names and comments.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param name the name of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *         <code>checkRead</code> 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
   116
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public ZipFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this(new File(name), OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Opens a new <code>ZipFile</code> to read from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>File</code> object in the specified mode.  The mode argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>First, if there is a security manager, its <code>checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * method is called with the <code>name</code> argument as its argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   132
     * <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
   133
     * decode the entry names and comments
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   134
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         doesn't allow read access to the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         or its <code>checkDelete</code> method doesn't allow deleting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *         the file when the <tt>OPEN_DELETE</tt> flag is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public ZipFile(File file, int mode) throws IOException {
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
   149
        this(file, mode, StandardCharsets.UTF_8);
2592
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
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   153
     * 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
   154
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   155
     * <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
   156
     * decode the entry names and comments.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   157
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   158
     * @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
   159
     * @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
   160
     * @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
   161
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   162
    public ZipFile(File file) throws ZipException, IOException {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   163
        this(file, OPEN_READ);
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
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   166
    private ZipCoder zc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   167
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   168
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   169
     * Opens a new <code>ZipFile</code> to read from the specified
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   170
     * <code>File</code> object in the specified mode.  The mode argument
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   171
     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   172
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   173
     * <p>First, if there is a security manager, its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   174
     * method is called with the <code>name</code> argument as its argument to
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   175
     * ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   176
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   177
     * @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
   178
     * @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
   179
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   180
     *        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
   181
     *        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
   182
     *        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
   183
     *        purpose flag).
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 ZipException if a ZIP format error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   186
     * @throws IOException if an I/O error has occurred
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   187
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   188
     * @throws SecurityException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   189
     *         if a security manager exists and its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   190
     *         method doesn't allow read access to the file,or its
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   191
     *         <code>checkDelete</code> method doesn't allow deleting the
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   192
     *         file when the <tt>OPEN_DELETE</tt> flag is set
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   193
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   194
     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   195
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   196
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   197
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   198
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   199
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   200
    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
   201
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (((mode & OPEN_READ) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IllegalArgumentException("Illegal mode: 0x"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                               Integer.toHexString(mode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            sm.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if ((mode & OPEN_DELETE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                sm.checkDelete(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   215
        if (charset == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   216
            throw new NullPointerException("charset is null");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   217
        this.zc = ZipCoder.get(charset);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   218
        long t0 = System.nanoTime();
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   219
        jzfile = open(name, mode, file.lastModified(), usemmap);
3849
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   220
        sun.misc.PerfCounter.getZipFileOpenTime().addElapsedTimeFrom(t0);
34469964aa98 6878481: Add performance counters in the JDK
mchung
parents: 3078
diff changeset
   221
        sun.misc.PerfCounter.getZipFileCount().increment();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.total = getTotal(jzfile);
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   224
        this.locsig = startsWithLOC(jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
2592
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
     * Opens a zip file for reading.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   229
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   230
     * <p>First, if there is a security manager, its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   231
     * method is called with the <code>name</code> argument as its argument
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   232
     * to ensure the read is allowed.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   233
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   234
     * @param name the name of the zip file
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   235
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   236
     *        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
   237
     *        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
   238
     *        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
   239
     *        purpose flag).
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
     * @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
   242
     * @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
   243
     * @throws SecurityException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   244
     *         if a security manager exists and its <code>checkRead</code>
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   245
     *         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
   246
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   247
     * @see SecurityManager#checkRead(java.lang.String)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   248
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   249
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   250
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   251
    public ZipFile(String name, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   252
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   253
        this(new File(name), OPEN_READ, charset);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   254
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Opens a ZIP file for reading given the specified File object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @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
   259
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
   260
     *        The {@linkplain java.nio.charset.Charset charset} to be
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   261
     *        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
   262
     *        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
   263
     *        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
   264
     *        flag is set).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   265
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   266
     * @throws ZipException if a ZIP format error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @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
   268
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   269
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   271
    public ZipFile(File file, Charset charset) throws IOException
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   272
    {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   273
        this(file, OPEN_READ, charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   277
     * Returns the zip file comment, or null if none.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   278
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   279
     * @return the comment string for the zip file, or null if none
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   280
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   281
     * @throws IllegalStateException if the zip file has been closed
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   282
     *
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   283
     * Since 1.7
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   284
     */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   285
    public String getComment() {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   286
        synchronized (this) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   287
            ensureOpen();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   288
            byte[] bcomm = getCommentBytes(jzfile);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   289
            if (bcomm == null)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   290
                return null;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   291
            return zc.toString(bcomm, bcomm.length);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   292
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   293
    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   294
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   295
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns the zip file entry for the specified name, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param name the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return the zip file entry, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public ZipEntry getEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            ensureOpen();
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   310
            jzentry = getEntry(jzfile, zc.getBytes(name), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (jzentry != 0) {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   312
                ZipEntry ze = getZipEntry(name, jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   320
    private static native long getEntry(long jzfile, byte[] name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                        boolean addSlash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    // freeEntry releases the C jzentry struct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static native void freeEntry(long jzfile, long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   326
    // the outstanding inputstreams that need to be closed,
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   327
    // mapped to the inflater objects they use.
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   328
    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
   329
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p> Closing this ZIP file will, in turn, close all input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * streams that have been returned by invocations of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param entry the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return the input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public InputStream getInputStream(ZipEntry entry) throws IOException {
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   345
        if (entry == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   346
            throw new NullPointerException("entry");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        ZipFileInputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            ensureOpen();
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   352
            if (!zc.isUTF8() && (entry.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   353
                jzentry = getEntry(jzfile, zc.getBytesUTF8(entry.name), false);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   354
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   355
                jzentry = getEntry(jzfile, zc.getBytes(entry.name), false);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   356
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            in = new ZipFileInputStream(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   362
            switch (getEntryMethod(jzentry)) {
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   363
            case STORED:
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(in, null);
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 in;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   368
            case DEFLATED:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   369
                // MORE: Compute good size for inflater stream:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   370
                long size = getEntrySize(jzentry) + 2; // Inflater likes a bit of slack
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   371
                if (size > 65536) size = 8192;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   372
                if (size <= 0) size = 4096;
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   373
                Inflater inf = getInflater();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   374
                InputStream is =
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   375
                    new ZipFileInflaterInputStream(in, inf, (int)size);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   376
                synchronized (streams) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   377
                    streams.put(is, inf);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   378
                }
7550
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   379
                return is;
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   380
            default:
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   381
                throw new ZipException("invalid compression method");
700dd1df3aa8 7003462: cannot read InputStream returned by java.util.ZipFile.getInputStream(ZipEntry)
sherman
parents: 6882
diff changeset
   382
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   386
    private class ZipFileInflaterInputStream extends InflaterInputStream {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   387
        private volatile boolean closeRequested = false;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   388
        private boolean eof = false;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   389
        private final ZipFileInputStream zfin;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   390
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   391
        ZipFileInflaterInputStream(ZipFileInputStream zfin, Inflater inf,
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   392
                int size) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   393
            super(zfin, inf, size);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   394
            this.zfin = zfin;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   395
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   396
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   397
        public void close() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   398
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   399
                return;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   400
            closeRequested = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   401
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   402
            super.close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   403
            Inflater inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   404
            synchronized (streams) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   405
                inf = streams.remove(this);
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
            if (inf != null) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   408
                releaseInflater(inf);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   409
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   410
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   411
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   412
        // Override fill() method to provide an extra "dummy" byte
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   413
        // at the end of the input stream. This is required when
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   414
        // using the "nowrap" Inflater option.
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   415
        protected void fill() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   416
            if (eof) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   417
                throw new EOFException("Unexpected end of ZLIB input stream");
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   418
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   419
            len = in.read(buf, 0, buf.length);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   420
            if (len == -1) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   421
                buf[0] = 0;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   422
                len = 1;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   423
                eof = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   424
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   425
            inf.setInput(buf, 0, len);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   426
        }
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
        public int available() throws IOException {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   429
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   430
                return 0;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   431
            long avail = zfin.size() - inf.getBytesWritten();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   432
            return (avail > (long) Integer.MAX_VALUE ?
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   433
                    Integer.MAX_VALUE : (int) avail);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   434
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   435
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   436
        protected void finalize() throws Throwable {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   437
            close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   438
        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   439
    }
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
     * Gets an inflater from the list of available inflaters or allocates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private Inflater getInflater() {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   446
        Inflater inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   447
        synchronized (inflaterCache) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   448
            while (null != (inf = inflaterCache.poll())) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   449
                if (false == inf.ended()) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   450
                    return inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   451
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   454
        return new Inflater(true);
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
     * Releases the specified inflater to the list of available inflaters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private void releaseInflater(Inflater inf) {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   461
        if (false == inf.ended()) {
1158
34d64e750f8e 6661861: Decrease memory use of Inflaters by ZipFile
bristor
parents: 2
diff changeset
   462
            inf.reset();
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   463
            synchronized (inflaterCache) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   464
                inflaterCache.add(inf);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   465
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // List of available Inflater objects for decompression
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   470
    private Deque<Inflater> inflaterCache = new ArrayDeque<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Returns the path name of the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return the path name of the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   480
    private class ZipEntryIterator implements Enumeration<ZipEntry>, Iterator<ZipEntry> {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   481
        private int i = 0;
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
        public ZipEntryIterator() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   484
            ensureOpen();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   485
        }
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
        public boolean hasMoreElements() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   488
            return hasNext();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   489
        }
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
        public boolean hasNext() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   492
            synchronized (ZipFile.this) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   493
                ensureOpen();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   494
                return i < total;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   495
            }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   496
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   497
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   498
        public ZipEntry nextElement() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   499
            return next();
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
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   502
        public ZipEntry next() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   503
            synchronized (ZipFile.this) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   504
                ensureOpen();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   505
                if (i >= total) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   506
                    throw new NoSuchElementException();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   507
                }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   508
                long jzentry = getNextEntry(jzfile, i++);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   509
                if (jzentry == 0) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   510
                    String message;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   511
                    if (closeRequested) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   512
                        message = "ZipFile concurrently closed";
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   513
                    } else {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   514
                        message = getZipMessage(ZipFile.this.jzfile);
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
                    throw new ZipError("jzentry == 0" +
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   517
                                       ",\n jzfile = " + ZipFile.this.jzfile +
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   518
                                       ",\n total = " + ZipFile.this.total +
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   519
                                       ",\n name = " + ZipFile.this.name +
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   520
                                       ",\n i = " + i +
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   521
                                       ",\n message = " + message
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   522
                        );
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   523
                }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   524
                ZipEntry ze = getZipEntry(null, jzentry);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   525
                freeEntry(jzfile, jzentry);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   526
                return ze;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   527
            }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   528
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   529
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   530
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Returns an enumeration of the ZIP file entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return an enumeration of the ZIP file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public Enumeration<? extends ZipEntry> entries() {
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   537
        return new ZipEntryIterator();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   538
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   539
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   540
    /**
30786
fb0d6aaea1dc 8064736: Part of java.util.jar.JarFile spec looks confusing with references to Zip
sherman
parents: 29226
diff changeset
   541
     * 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
   542
     * 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
   543
     * the central directory of the ZIP file.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   544
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   545
     * @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
   546
     * @throws IllegalStateException if the zip file has been closed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   547
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   548
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   549
    public Stream<? extends ZipEntry> stream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   550
        return StreamSupport.stream(Spliterators.spliterator(
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   551
                new ZipEntryIterator(), size(),
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 9676
diff changeset
   552
                Spliterator.ORDERED | Spliterator.DISTINCT |
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18253
diff changeset
   553
                        Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   556
    private ZipEntry getZipEntry(String name, long jzentry) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   557
        ZipEntry e = new ZipEntry();
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   558
        e.flag = getEntryFlag(jzentry);  // get the flag first
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   559
        if (name != null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   560
            e.name = name;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   561
        } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   562
            byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   563
            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   564
                e.name = zc.toStringUTF8(bname, bname.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   565
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   566
                e.name = zc.toString(bname, bname.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   567
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   568
        }
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   569
        e.xdostime = getEntryTime(jzentry);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   570
        e.crc = getEntryCrc(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   571
        e.size = getEntrySize(jzentry);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 18822
diff changeset
   572
        e.csize = getEntryCSize(jzentry);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   573
        e.method = getEntryMethod(jzentry);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 18822
diff changeset
   574
        e.setExtra0(getEntryBytes(jzentry, JZENTRY_EXTRA), false);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   575
        byte[] bcomm = getEntryBytes(jzentry, JZENTRY_COMMENT);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   576
        if (bcomm == null) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   577
            e.comment = null;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   578
        } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   579
            if (!zc.isUTF8() && (e.flag & EFS) != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   580
                e.comment = zc.toStringUTF8(bcomm, bcomm.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   581
            } else {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   582
                e.comment = zc.toString(bcomm, bcomm.length);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   583
            }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   584
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   585
        return e;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   586
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   587
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private static native long getNextEntry(long jzfile, int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns the number of entries in the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return the number of entries in the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Closes the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <p> Closing this ZIP file will close all of the input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * previously returned by invocations of the {@link #getInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * getInputStream} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public void close() throws IOException {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   609
        if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   610
            return;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   611
        closeRequested = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   613
        synchronized (this) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   614
            // Close streams, release their inflaters
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   615
            synchronized (streams) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   616
                if (false == streams.isEmpty()) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   617
                    Map<InputStream, Inflater> copy = new HashMap<>(streams);
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   618
                    streams.clear();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   619
                    for (Map.Entry<InputStream, Inflater> e : copy.entrySet()) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   620
                        e.getKey().close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   621
                        Inflater inf = e.getValue();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   622
                        if (inf != null) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   623
                            inf.end();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   624
                        }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   625
                    }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   626
                }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   627
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   628
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   629
            // Release cached inflaters
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   630
            Inflater inf;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   631
            synchronized (inflaterCache) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   632
                while (null != (inf = inflaterCache.poll())) {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   633
                    inf.end();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   634
                }
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
   635
            }
92e31faf9ffc 6728376: Wrong error handling in Java_java_util_zip_Deflater_deflateBytes leads to size 0 if compress fails
sherman
parents: 2704
diff changeset
   636
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            if (jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                // Close the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                long zf = this.jzfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                jzfile = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                close(zf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   648
     * Ensures that the system resources held by this ZipFile object are
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   649
     * released when there are no more references to it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Since the time when GC would invoke this method is undetermined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * it is strongly recommended that applications invoke the <code>close</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * method as soon they have finished accessing this <code>ZipFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * This will prevent holding up system resources for an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * length of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @see    java.util.zip.ZipFile#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    private static native void close(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            throw new IllegalStateException("zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (jzfile == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            throw new IllegalStateException("The object is not initialized.");
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
    private void ensureOpenOrZipException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            throw new ZipException("ZipFile closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Inner class implementing the input stream used to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * (possibly compressed) zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
   private class ZipFileInputStream extends InputStream {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   688
        private volatile boolean closeRequested = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        protected long jzentry; // address of jzentry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        private   long pos;     // current position within entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        protected long rem;     // number of remaining bytes within entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        protected long size;    // uncompressed size of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        ZipFileInputStream(long jzentry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            pos = 0;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   696
            rem = getEntryCSize(jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   697
            size = getEntrySize(jzentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            this.jzentry = jzentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            synchronized (ZipFile.this) {
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   703
                long rem = this.rem;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   704
                long pos = this.pos;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   705
                if (rem == 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   706
                    return -1;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   707
                }
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   708
                if (len <= 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   709
                    return 0;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   710
                }
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   711
                if (len > rem) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   712
                    len = (int) rem;
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   713
                }
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   714
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                ensureOpenOrZipException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                                   off, len);
23741
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   718
                if (len > 0) {
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   719
                    this.pos = (pos + len);
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   720
                    this.rem = (rem - len);
03987fb914a9 8038491: Improve synchronization in ZipFile.read()
coffeys
parents: 19374
diff changeset
   721
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (read(b, 0, 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        public long skip(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (n > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                n = rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            rem -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        public long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        public void close() {
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   758
            if (closeRequested)
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   759
                return;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   760
            closeRequested = true;
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   761
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            rem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                if (jzentry != 0 && ZipFile.this.jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    freeEntry(ZipFile.this.jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   768
            }
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   769
            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
   770
                streams.remove(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
9281
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   773
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   774
        protected void finalize() {
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   775
            close();
41e796d1b6c1 7031076: Retained ZipFile InputStreams increase heap demand
sherman
parents: 9035
diff changeset
   776
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   779
    static {
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   780
        sun.misc.SharedSecrets.setJavaUtilZipFileAccess(
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   781
            new sun.misc.JavaUtilZipFileAccess() {
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   782
                public boolean startsWithLocHeader(ZipFile zip) {
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   783
                    return zip.startsWithLocHeader();
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   784
                }
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   785
             }
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   786
        );
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   787
    }
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   788
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   789
    /**
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   790
     * Returns {@code true} if, and only if, the zip file begins with {@code
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   791
     * LOCSIG}.
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   792
     */
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   793
    private boolean startsWithLocHeader() {
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   794
        return locsig;
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   795
    }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   796
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   797
    private static native long open(String name, int mode, long lastModified,
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3849
diff changeset
   798
                                    boolean usemmap) throws IOException;
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   799
    private static native int getTotal(long jzfile);
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 9676
diff changeset
   800
    private static native boolean startsWithLOC(long jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    private static native int read(long jzfile, long jzentry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                   long pos, byte[] b, int off, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   804
    // access to the native zentry object
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   805
    private static native long getEntryTime(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   806
    private static native long getEntryCrc(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   807
    private static native long getEntryCSize(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   808
    private static native long getEntrySize(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   809
    private static native int getEntryMethod(long jzentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   810
    private static native int getEntryFlag(long jzentry);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2915
diff changeset
   811
    private static native byte[] getCommentBytes(long jzfile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   813
    private static final int JZENTRY_NAME = 0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   814
    private static final int JZENTRY_EXTRA = 1;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   815
    private static final int JZENTRY_COMMENT = 2;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 1158
diff changeset
   816
    private static native byte[] getEntryBytes(long jzentry, int type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    private static native String getZipMessage(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
}