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