jdk/src/share/classes/java/util/zip/ZipInputStream.java
author darcy
Tue, 07 Feb 2012 17:39:13 -0800
changeset 11828 590711df7828
parent 9676 5663e62f8d7e
child 17910 82d10099a8a6
permissions -rw-r--r--
7143629: JDK jar/zip code should use unsigned library support Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11828
590711df7828 7143629: JDK jar/zip code should use unsigned library support
darcy
parents: 9676
diff changeset
     2
 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2704
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
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;
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
    33
import java.nio.charset.StandardCharsets;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
    34
import static java.util.zip.ZipConstants64.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements an input stream filter for reading files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * ZIP file format. Includes support for both compressed and uncompressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class ZipInputStream extends InflaterInputStream implements ZipConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private ZipEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private int flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private long remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] tmpbuf = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int STORED = ZipEntry.STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int DEFLATED = ZipEntry.DEFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // this flag is set to true after EOF has reached for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // one entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean entryEOF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    59
    private ZipCoder zc;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    60
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Check to make sure that this stream has not been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new IOException("Stream closed");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Creates a new ZIP input stream.
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    72
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    73
     * <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
    74
     * decode the entry names.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    75
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param in the actual input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public ZipInputStream(InputStream in) {
9676
5663e62f8d7e 7041612: Rename StandardCharset to StandardCharsets
mduigou
parents: 9526
diff changeset
    79
        this(in, StandardCharsets.UTF_8);
2592
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
    /**
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    83
     * Creates a new ZIP input stream.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    84
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    85
     * @param in the actual input stream
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    86
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    87
     * @param charset
2704
a92617170304 6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents: 2592
diff changeset
    88
     *        The {@linkplain java.nio.charset.Charset charset} to be
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    89
     *        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
    90
     *        <a href="package-summary.html#lang_encoding"> language
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    91
     *        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
    92
     *        flag is set).
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    93
     *
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    94
     * @since 1.7
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    95
     */
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
    96
    public ZipInputStream(InputStream in, Charset charset) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super(new PushbackInputStream(in, 512), new Inflater(true), 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        usesDefaultInflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if(in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new NullPointerException("in is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   102
        if (charset == null)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   103
            throw new NullPointerException("charset is null");
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   104
        this.zc = ZipCoder.get(charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Reads the next ZIP file entry and positions the stream at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * beginning of the entry data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return the next ZIP file entry, or null if there are no more entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public ZipEntry getNextEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            closeEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        inf.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ((entry = readLOC()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (entry.method == STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            remaining = entry.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        entryEOF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Closes the current ZIP entry and positions the stream for reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void closeEntry() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns 0 after EOF has reached for the current entry data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * otherwise always return 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Programs should not count on this method to return the actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * of bytes that could be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return     1 before EOF and 0 after EOF has reached for current entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (entryEOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Reads from the current ZIP entry into an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * If <code>len</code> is not zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * blocks until some input is available; otherwise, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * bytes are read and <code>0</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param off the start offset in the destination array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param len the maximum number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the actual number of bytes read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *         entry is reached
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   173
     * @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
   174
     * @exception  IndexOutOfBoundsException if <code>off</code> is negative,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (off < 0 || len < 0 || off > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        switch (entry.method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        case DEFLATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            len = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                readEnd(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case STORED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (remaining <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                len = (int)remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            len = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                throw new ZipException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            crc.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            remaining -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (remaining == 0 && entry.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    "invalid entry CRC (expected 0x" + Long.toHexString(entry.crc) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    " but got 0x" + Long.toHexString(crc.getValue()) + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new ZipException("invalid compression method");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Skips specified number of bytes in the current ZIP entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param n the number of bytes to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return the actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception ZipException if a ZIP file error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception IllegalArgumentException if n < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new IllegalArgumentException("negative skip length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int max = (int)Math.min(n, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        while (total < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            int len = max - total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (len > tmpbuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                len = tmpbuf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            len = read(tmpbuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                entryEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            total += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private byte[] b = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Reads local file (LOC) header for next entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private ZipEntry readLOC() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            readFully(tmpbuf, 0, LOCHDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } catch (EOFException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (get32(tmpbuf, 0) != LOCSIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   284
        // get flag first, we need check EFS.
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   285
        flag = get16(tmpbuf, LOCFLG);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // get the entry name and create the ZipEntry first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int len = get16(tmpbuf, LOCNAM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        int blen = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (len > blen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                blen = blen * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            while (len > blen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            b = new byte[blen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        readFully(b, 0, len);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   296
        // 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
   297
        ZipEntry e = createZipEntry(((flag & EFS) != 0)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   298
                                    ? zc.toStringUTF8(b, len)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
   299
                                    : zc.toString(b, len));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // now get the remaining fields for the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ((flag & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new ZipException("encrypted ZIP entry not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        e.method = get16(tmpbuf, LOCHOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        e.time = get32(tmpbuf, LOCTIM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if ((flag & 8) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            /* "Data Descriptor" present */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (e.method != DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        "only DEFLATED entries can have EXT descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            e.crc = get32(tmpbuf, LOCCRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            e.csize = get32(tmpbuf, LOCSIZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            e.size = get32(tmpbuf, LOCLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        len = get16(tmpbuf, LOCEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            byte[] bb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            readFully(bb, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            e.setExtra(bb);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   322
            // 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
   323
            if (e.csize == ZIP64_MAGICVAL || e.size == ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   324
                int off = 0;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   325
                while (off + 4 < len) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   326
                    int sz = get16(bb, off + 2);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   327
                    if (get16(bb, off) == ZIP64_EXTID) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   328
                        off += 4;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   329
                        // LOC extra zip64 entry MUST include BOTH original and
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   330
                        // compressed file size fields
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   331
                        if (sz < 16 || (off + sz) > len ) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   332
                            // Invalid zip64 extra fields, simply skip. Even it's
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   333
                            // 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
   334
                            // the magic value and it "accidnetly" has some bytes
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   335
                            // in extra match the id.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   336
                            return e;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   337
                        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   338
                        e.size = get64(bb, off);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   339
                        e.csize = get64(bb, off + 8);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   340
                        break;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   341
                    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   342
                    off += (sz + 4);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   343
                }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   344
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Creates a new <code>ZipEntry</code> object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * entry name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param name the ZIP file entry name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return the ZipEntry just created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    protected ZipEntry createZipEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return new ZipEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Reads end of deflated entry as well as EXT descriptor if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private void readEnd(ZipEntry e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int n = inf.getRemaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            ((PushbackInputStream)in).unread(buf, len - n, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if ((flag & 8) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            /* "Data Descriptor" present */
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   370
            if (inf.getBytesWritten() > ZIP64_MAGICVAL ||
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   371
                inf.getBytesRead() > ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   372
                // ZIP64 format
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   373
                readFully(tmpbuf, 0, ZIP64_EXTHDR);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   374
                long sig = get32(tmpbuf, 0);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   375
                if (sig != EXTSIG) { // no EXTSIG present
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   376
                    e.crc = sig;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   377
                    e.csize = get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   378
                    e.size = get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   379
                    ((PushbackInputStream)in).unread(
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   380
                        tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC - 1, ZIP64_EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   381
                } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   382
                    e.crc = get32(tmpbuf, ZIP64_EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   383
                    e.csize = get64(tmpbuf, ZIP64_EXTSIZ);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   384
                    e.size = get64(tmpbuf, ZIP64_EXTLEN);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   385
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            } else {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   387
                readFully(tmpbuf, 0, EXTHDR);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   388
                long sig = get32(tmpbuf, 0);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   389
                if (sig != EXTSIG) { // no EXTSIG present
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   390
                    e.crc = sig;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   391
                    e.csize = get32(tmpbuf, EXTSIZ - EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   392
                    e.size = get32(tmpbuf, EXTLEN - EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   393
                    ((PushbackInputStream)in).unread(
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   394
                                               tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   395
                } else {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   396
                    e.crc = get32(tmpbuf, EXTCRC);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   397
                    e.csize = get32(tmpbuf, EXTSIZ);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   398
                    e.size = get32(tmpbuf, EXTLEN);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   399
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (e.size != inf.getBytesWritten()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                "invalid entry size (expected " + e.size +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                " but got " + inf.getBytesWritten() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (e.csize != inf.getBytesRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                "invalid entry compressed size (expected " + e.csize +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                " but got " + inf.getBytesRead() + " bytes)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (e.crc != crc.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new ZipException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                "invalid entry CRC (expected 0x" + Long.toHexString(e.crc) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                " but got 0x" + Long.toHexString(crc.getValue()) + ")");
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Reads bytes, blocking until all bytes are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private void readFully(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            int n = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            len -= n;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Fetches unsigned 16-bit value from byte array at specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * The bytes are assumed to be in Intel (little-endian) byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    private static final int get16(byte b[], int off) {
11828
590711df7828 7143629: JDK jar/zip code should use unsigned library support
darcy
parents: 9676
diff changeset
   438
        return Byte.toUnsignedInt(b[off]) | (Byte.toUnsignedInt(b[off+1]) << 8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Fetches unsigned 32-bit value from byte array at specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * The bytes are assumed to be in Intel (little-endian) byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    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
   446
        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
   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
    /*
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   450
     * 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
   451
     * 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
   452
     */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   453
    private static final long get64(byte b[], int off) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2
diff changeset
   454
        return get32(b, off) | (get32(b, off+4) << 32);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
}