src/java.base/share/classes/java/util/zip/GZIPInputStream.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
child 58288 48e480e56aad
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.SequenceInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
17457
2046a488d605 7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents: 14342
diff changeset
    30
import java.io.FilterInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.EOFException;
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 a stream filter for reading compressed data in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the GZIP file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see         InflaterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author      David Connelly
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 32649
diff changeset
    41
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class GZIPInputStream extends InflaterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * CRC-32 for uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected CRC32 crc = new CRC32();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Indicates end of input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected boolean eos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Check to make sure that this stream has not been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a new input stream with the specified buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param in the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param size the input buffer size
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
    71
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    72
     * @throws    ZipException if a GZIP format error has occurred or the
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
    73
     *                         compression method used is unsupported
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    74
     * @throws    IOException if an I/O error has occurred
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    75
     * @throws    IllegalArgumentException if {@code size <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public GZIPInputStream(InputStream in, int size) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(in, new Inflater(true), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        usesDefaultInflater = true;
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
    80
        readHeader(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Creates a new input stream with a default buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param in the input stream
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
    86
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    87
     * @throws    ZipException if a GZIP format error has occurred or the
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
    88
     *                         compression method used is unsupported
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    89
     * @throws    IOException if an I/O error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public GZIPInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this(in, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Reads uncompressed data into an array of bytes. If <code>len</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * zero, the method will block until some input can be decompressed; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * no bytes are read and <code>0</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param buf the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param off the start offset in the destination array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param len the maximum number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @return  the actual number of bytes read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *          compressed input stream is reached
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
   104
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   105
     * @throws     NullPointerException If <code>buf</code> is <code>null</code>.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   106
     * @throws     IndexOutOfBoundsException If <code>off</code> is negative,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <code>buf.length - off</code>
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   109
     * @throws    ZipException if the compressed input data is corrupt.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   110
     * @throws    IOException if an I/O error has occurred.
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
   111
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public int read(byte[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (eos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   118
        int n = super.read(buf, off, len);
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   119
        if (n == -1) {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   120
            if (readTrailer())
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   121
                eos = true;
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   122
            else
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   123
                return this.read(buf, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else {
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   125
            crc.update(buf, off, n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   127
        return n;
2
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 this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * with the stream.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   133
     * @throws    IOException if an I/O error has occurred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            eos = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            closed = 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * GZIP header magic number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   146
    public static final int GZIP_MAGIC = 0x8b1f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * File header flags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   151
    private static final int FTEXT      = 1;    // Extra text
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   152
    private static final int FHCRC      = 2;    // Header CRC
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   153
    private static final int FEXTRA     = 4;    // Extra field
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   154
    private static final int FNAME      = 8;    // File name
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   155
    private static final int FCOMMENT   = 16;   // File comment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   158
     * Reads GZIP member header and returns the total byte number
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   159
     * of this member header.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   161
    private int readHeader(InputStream this_in) throws IOException {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   162
        CheckedInputStream in = new CheckedInputStream(this_in, crc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        crc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // Check header magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (readUShort(in) != GZIP_MAGIC) {
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
   166
            throw new ZipException("Not in GZIP format");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // Check compression method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (readUByte(in) != 8) {
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
   170
            throw new ZipException("Unsupported compression method");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Read flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int flg = readUByte(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Skip MTIME, XFL, and OS fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        skipBytes(in, 6);
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   176
        int n = 2 + 2 + 6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // Skip optional extra field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if ((flg & FEXTRA) == FEXTRA) {
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   179
            int m = readUShort(in);
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   180
            skipBytes(in, m);
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   181
            n += m + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Skip optional file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if ((flg & FNAME) == FNAME) {
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   185
            do {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   186
                n++;
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   187
            } while (readUByte(in) != 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Skip optional file comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if ((flg & FCOMMENT) == FCOMMENT) {
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   191
            do {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   192
                n++;
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   193
            } while (readUByte(in) != 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Check optional header CRC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if ((flg & FHCRC) == FHCRC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            int v = (int)crc.getValue() & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (readUShort(in) != v) {
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
   199
                throw new ZipException("Corrupt GZIP header");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   201
            n += 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   203
        crc.reset();
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   204
        return n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /*
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   208
     * Reads GZIP member trailer and returns true if the eos
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   209
     * reached, false if there are more (concatenated gzip
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   210
     * data set)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   212
    private boolean readTrailer() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        InputStream in = this.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int n = inf.getRemaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            in = new SequenceInputStream(
17457
2046a488d605 7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents: 14342
diff changeset
   217
                        new ByteArrayInputStream(buf, len - n, n),
2046a488d605 7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents: 14342
diff changeset
   218
                        new FilterInputStream(in) {
2046a488d605 7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents: 14342
diff changeset
   219
                            public void close() throws IOException {}
2046a488d605 7021870: GzipInputStream closes underlying stream during reading
dmeetry
parents: 14342
diff changeset
   220
                        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // Uses left-to-right evaluation order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if ((readUInt(in) != crc.getValue()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // rfc1952; ISIZE is the input size modulo 2^32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL)))
5605
bba86030a87b 4263582: RFE: GZIPInputStream throws IOException on non-gzipped data
sherman
parents: 2
diff changeset
   226
            throw new ZipException("Corrupt GZIP trailer");
5618
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   227
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   228
        // If there are more bytes available in "in" or
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   229
        // the leftover in the "inf" is > 26 bytes:
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   230
        // this.trailer(8) + next.header.min(10) + next.trailer(8)
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   231
        // try concatenated case
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   232
        if (this.in.available() > 0 || n > 26) {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   233
            int m = 8;                  // this.trailer
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   234
            try {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   235
                m += readHeader(in);    // next.header
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   236
            } catch (IOException ze) {
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   237
                return true;  // ignore any malformed, do nothing
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   238
            }
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   239
            inf.reset();
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   240
            if (n > m)
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   241
                inf.setInput(buf, len - n + m, n - m);
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   242
            return false;
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   243
        }
d17b52843430 4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents: 5605
diff changeset
   244
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Reads unsigned integer in Intel byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private long readUInt(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        long s = readUShort(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return ((long)readUShort(in) << 16) | s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Reads unsigned short in Intel byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private int readUShort(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int b = readUByte(in);
13158
f628cbfcbfa0 7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents: 5627
diff changeset
   260
        return (readUByte(in) << 8) | b;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Reads unsigned byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private int readUByte(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (b < -1 || b > 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // Report on this.in, not argument in; see read{Header, Trailer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new IOException(this.in.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                + ".read() returned value out of range -1..255: " + b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private byte[] tmpbuf = new byte[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Skips bytes of input data blocking until all bytes are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Does not assume that the input stream is capable of seeking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private void skipBytes(InputStream in, int n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        while (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            int len = in.read(tmpbuf, 0, n < tmpbuf.length ? n : tmpbuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            n -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}