jdk/src/share/classes/java/util/zip/ZipInputStream.java
author sherman
Wed, 04 Mar 2009 09:26:41 -0800
changeset 2178 5a730fff4d0d
parent 2 90ce3da70b43
child 2438 21c111b51aa8
permissions -rw-r--r--
6812879: Excess code line in ArrayList method Summary: Removed the line of "oldData" which is no longer used. Reviewed-by: martin
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 1996-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.PushbackInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements an input stream filter for reading files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ZIP file format. Includes support for both compressed and uncompressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class ZipInputStream extends InflaterInputStream implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private ZipEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private int flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private long remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private byte[] tmpbuf = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // this flag is set to true after EOF has reached for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // one entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean entryEOF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Check to make sure that this stream has not been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a new ZIP input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param in the actual input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public ZipInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(new PushbackInputStream(in, 512), new Inflater(true), 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        usesDefaultInflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if(in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new NullPointerException("in is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Reads the next ZIP file entry and positions the stream at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * beginning of the entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return the next ZIP file entry, or null if there are no more entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public ZipEntry getNextEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        inf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if ((entry = readLOC()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (entry.method == STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            remaining = entry.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        entryEOF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Closes the current ZIP entry and positions the stream for reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void closeEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns 0 after EOF has reached for the current entry data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * otherwise always return 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Programs should not count on this method to return the actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * of bytes that could be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return     1 before EOF and 0 after EOF has reached for current entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (entryEOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Reads from the current ZIP entry into an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * If <code>len</code> is not zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * blocks until some input is available; otherwise, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * bytes are read and <code>0</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param off the start offset in the destination array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param len the maximum number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return the actual number of bytes read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         entry is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        switch (entry.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            len = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                readEnd(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (remaining <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                len = (int)remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            len = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                throw new ZipException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            remaining -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (remaining == 0 && entry.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    "invalid entry CRC (expected 0x" + Long.toHexString(entry.crc) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    " but got 0x" + Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new ZipException("invalid compression method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Skips specified number of bytes in the current ZIP entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param n the number of bytes to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return the actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception IllegalArgumentException if n < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new IllegalArgumentException("negative skip length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int max = (int)Math.min(n, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        while (total < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            int len = max - total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (len > tmpbuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                len = tmpbuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            len = read(tmpbuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            total += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            closed = 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private byte[] b = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Reads local file (LOC) header for next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private ZipEntry readLOC() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            readFully(tmpbuf, 0, LOCHDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (EOFException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (get32(tmpbuf, 0) != LOCSIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // get the entry name and create the ZipEntry first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int len = get16(tmpbuf, LOCNAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        int blen = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (len > blen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                blen = blen * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            while (len > blen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            b = new byte[blen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        readFully(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        ZipEntry e = createZipEntry(getUTF8String(b, 0, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // now get the remaining fields for the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        flag = get16(tmpbuf, LOCFLG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if ((flag & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new ZipException("encrypted ZIP entry not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        e.method = get16(tmpbuf, LOCHOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        e.time = get32(tmpbuf, LOCTIM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if ((flag & 8) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            /* "Data Descriptor" present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (e.method != DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        "only DEFLATED entries can have EXT descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            e.crc = get32(tmpbuf, LOCCRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            e.csize = get32(tmpbuf, LOCSIZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            e.size = get32(tmpbuf, LOCLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        len = get16(tmpbuf, LOCEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            byte[] bb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            readFully(bb, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            e.setExtra(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Fetches a UTF8-encoded String from the specified byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private static String getUTF8String(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // First, count the number of characters in the sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int max = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int i = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        while (i < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int c = b[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            switch (c >> 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // 0xxxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            case 12: case 13:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                // 110xxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if ((b[i++] & 0xc0) != 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            case 14:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                // 1110xxxx 10xxxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (((b[i++] & 0xc0) != 0x80) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    ((b[i++] & 0xc0) != 0x80)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // 10xxxxxx, 1111xxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (i != max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Now decode the characters...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        char[] cs = new char[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        while (off < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int c = b[off++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            switch (c >> 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // 0xxxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                cs[i++] = (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            case 12: case 13:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // 110xxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                cs[i++] = (char)(((c & 0x1f) << 6) | (b[off++] & 0x3f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            case 14:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                // 1110xxxx 10xxxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                int t = (b[off++] & 0x3f) << 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                cs[i++] = (char)(((c & 0x0f) << 12) | t | (b[off++] & 0x3f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                // 10xxxxxx, 1111xxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return new String(cs, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Creates a new <code>ZipEntry</code> object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * entry name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param name the ZIP file entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return the ZipEntry just created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    protected ZipEntry createZipEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Reads end of deflated entry as well as EXT descriptor if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private void readEnd(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int n = inf.getRemaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            ((PushbackInputStream)in).unread(buf, len - n, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if ((flag & 8) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            /* "Data Descriptor" present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            readFully(tmpbuf, 0, EXTHDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            long sig = get32(tmpbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (sig != EXTSIG) { // no EXTSIG present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                e.crc = sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                e.csize = get32(tmpbuf, EXTSIZ - EXTCRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                e.size = get32(tmpbuf, EXTLEN - EXTCRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                ((PushbackInputStream)in).unread(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                           tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                e.crc = get32(tmpbuf, EXTCRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                e.csize = get32(tmpbuf, EXTSIZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                e.size = get32(tmpbuf, EXTLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (e.size != inf.getBytesWritten()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                " but got " + inf.getBytesWritten() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (e.csize != inf.getBytesRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                "invalid entry compressed size (expected " + e.csize +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                " but got " + inf.getBytesRead() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                "invalid entry CRC (expected 0x" + Long.toHexString(e.crc) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                " but got 0x" + Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Reads bytes, blocking until all bytes are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private void readFully(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            int n = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Fetches unsigned 16-bit value from byte array at specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * The bytes are assumed to be in Intel (little-endian) byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private static final int get16(byte b[], int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Fetches unsigned 32-bit value from byte array at specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * The bytes are assumed to be in Intel (little-endian) byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private static final long get32(byte b[], int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return get16(b, off) | ((long)get16(b, off+2) << 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}