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