jdk/src/share/classes/java/util/zip/ZipFile.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1158 34d64e750f8e
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is used to read entries from a zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * or method in this class will cause a {@link NullPointerException} to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class ZipFile implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private long jzfile;  // address of jzfile data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String name;  // zip file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private int total;    // total number of entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean closeRequested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Mode flag to open a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int OPEN_READ = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Mode flag to open a zip file and mark it for deletion.  The file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * deleted some time between the moment that it is opened and the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * that it is closed, but its contents will remain accessible via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <tt>ZipFile</tt> object until either the close method is invoked or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * virtual machine exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int OPEN_DELETE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        /* Zip library is loaded from System.initializeSystemClass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Opens a zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p>First, if there is a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * manager, its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * is called with the <code>name</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * as its argument to ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param name the name of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @throws SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *         <code>checkRead</code> method doesn't allow read access to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public ZipFile(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this(new File(name), OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Opens a new <code>ZipFile</code> to read from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>File</code> object in the specified mode.  The mode argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <p>First, if there is a security manager, its <code>checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * method is called with the <code>name</code> argument as its argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * ensure the read is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param mode the mode in which the file is to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *         its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *         doesn't allow read access to the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *         or its <code>checkDelete</code> method doesn't allow deleting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *         the file when the <tt>OPEN_DELETE</tt> flag is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws IllegalArgumentException if the <tt>mode</tt> argument is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public ZipFile(File file, int mode) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (((mode & OPEN_READ) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ((mode & ~(OPEN_READ | OPEN_DELETE)) != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException("Illegal mode: 0x"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                               Integer.toHexString(mode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String name = file.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            sm.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if ((mode & OPEN_DELETE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                sm.checkDelete(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        jzfile = open(name, mode, file.lastModified());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.total = getTotal(jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static native long open(String name, int mode, long lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static native int getTotal(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Opens a ZIP file for reading given the specified File object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param file the ZIP file to be opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @throws ZipException if a ZIP error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public ZipFile(File file) throws ZipException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this(file, OPEN_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the zip file entry for the specified name, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param name the name of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return the zip file entry, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public ZipEntry getEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            jzentry = getEntry(jzfile, name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (jzentry != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                ZipEntry ze = new ZipEntry(name, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static native long getEntry(long jzfile, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                        boolean addSlash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // freeEntry releases the C jzentry struct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static native void freeEntry(long jzfile, long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p> Closing this ZIP file will, in turn, close all input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * streams that have been returned by invocations of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param entry the zip file entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws ZipException if a ZIP format error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public InputStream getInputStream(ZipEntry entry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return getInputStream(entry.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Returns an input stream for reading the contents of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * entry, or null if the entry was not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private InputStream getInputStream(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        long jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        ZipFileInputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            jzentry = getEntry(jzfile, name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            in = new ZipFileInputStream(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        final ZipFileInputStream zfin = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        switch (getMethod(jzentry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return zfin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // MORE: Compute good size for inflater stream:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            long size = getSize(jzentry) + 2; // Inflater likes a bit of slack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (size > 65536) size = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (size <= 0) size = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return new InflaterInputStream(zfin, getInflater(), (int)size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                private boolean isClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if (!isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                         releaseInflater(inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        this.in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // Override fill() method to provide an extra "dummy" byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                // at the end of the input stream. This is required when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                // using the "nowrap" Inflater option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                protected void fill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        throw new EOFException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            "Unexpected end of ZLIB input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    len = this.in.read(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        buf[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    inf.setInput(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                private boolean eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    if (isClosed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    long avail = zfin.size() - inf.getBytesWritten();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    return avail > (long) Integer.MAX_VALUE ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        Integer.MAX_VALUE : (int) avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static native int getMethod(long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Gets an inflater from the list of available inflaters or allocates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private Inflater getInflater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int size = inflaters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                Inflater inf = (Inflater)inflaters.remove(size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                inf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                return inf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                return new Inflater(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Releases the specified inflater to the list of available inflaters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private void releaseInflater(Inflater inf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            inflaters.add(inf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // List of available Inflater objects for decompression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private Vector inflaters = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Returns the path name of the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return the path name of the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns an enumeration of the ZIP file entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return an enumeration of the ZIP file entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public Enumeration<? extends ZipEntry> entries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return new Enumeration<ZipEntry>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                private int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        return i < total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                public ZipEntry nextElement() throws NoSuchElementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        if (i >= total) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        long jzentry = getNextEntry(jzfile, i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        if (jzentry == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                            String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                            if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                message = "ZipFile concurrently closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                message = getZipMessage(ZipFile.this.jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                            throw new ZipError("jzentry == 0" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                               ",\n jzfile = " + ZipFile.this.jzfile +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                               ",\n total = " + ZipFile.this.total +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                               ",\n name = " + ZipFile.this.name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                               ",\n i = " + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                               ",\n message = " + message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        ZipEntry ze = new ZipEntry(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        freeEntry(jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private static native long getNextEntry(long jzfile, int i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Returns the number of entries in the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return the number of entries in the ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws IllegalStateException if the zip file has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Closes the ZIP file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p> Closing this ZIP file will close all of the input streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * previously returned by invocations of the {@link #getInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * getInputStream} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            closeRequested = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                // Close the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                long zf = this.jzfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                jzfile = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                close(zf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                // Release inflaters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                synchronized (inflaters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    int size = inflaters.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        Inflater inf = (Inflater)inflaters.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        inf.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Ensures that the <code>close</code> method of this ZIP file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * called when there are no more references to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Since the time when GC would invoke this method is undetermined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * it is strongly recommended that applications invoke the <code>close</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * method as soon they have finished accessing this <code>ZipFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * This will prevent holding up system resources for an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * length of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see    java.util.zip.ZipFile#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private static native void close(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throw new IllegalStateException("zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (jzfile == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new IllegalStateException("The object is not initialized.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private void ensureOpenOrZipException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (closeRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw new ZipException("ZipFile closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Inner class implementing the input stream used to read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * (possibly compressed) zip file entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
   private class ZipFileInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        protected long jzentry; // address of jzentry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        private   long pos;     // current position within entry data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        protected long rem;     // number of remaining bytes within entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        protected long size;    // uncompressed size of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        ZipFileInputStream(long jzentry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            rem = getCSize(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            size = getSize(jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            this.jzentry = jzentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (len > rem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                len = (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                ensureOpenOrZipException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                len = ZipFile.read(ZipFile.this.jzfile, jzentry, pos, b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                   off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                rem -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (read(b, 0, 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                return b[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public long skip(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (n > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                n = rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            rem -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (rem == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        public long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            rem = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            synchronized (ZipFile.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                if (jzentry != 0 && ZipFile.this.jzfile != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    freeEntry(ZipFile.this.jzfile, jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    jzentry = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private static native int read(long jzfile, long jzentry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                   long pos, byte[] b, int off, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static native long getCSize(long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private static native long getSize(long jzentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    // Temporary add on for bug troubleshooting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private static native String getZipMessage(long jzfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
}