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