src/share/classes/java/util/zip/GZIPInputStream.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.io.SequenceInputStream;
  29 import java.io.ByteArrayInputStream;

  30 import java.io.InputStream;
  31 import java.io.IOException;
  32 import java.io.EOFException;
  33 
  34 /**
  35  * This class implements a stream filter for reading compressed data in
  36  * the GZIP file format.
  37  *
  38  * @see         InflaterInputStream
  39  * @author      David Connelly
  40  *
  41  */
  42 public
  43 class GZIPInputStream extends InflaterInputStream {
  44     /**
  45      * CRC-32 for uncompressed data.
  46      */
  47     protected CRC32 crc = new CRC32();
  48 
  49     /**


 195             int v = (int)crc.getValue() & 0xffff;
 196             if (readUShort(in) != v) {
 197                 throw new ZipException("Corrupt GZIP header");
 198             }
 199             n += 2;
 200         }
 201         crc.reset();
 202         return n;
 203     }
 204 
 205     /*
 206      * Reads GZIP member trailer and returns true if the eos
 207      * reached, false if there are more (concatenated gzip
 208      * data set)
 209      */
 210     private boolean readTrailer() throws IOException {
 211         InputStream in = this.in;
 212         int n = inf.getRemaining();
 213         if (n > 0) {
 214             in = new SequenceInputStream(
 215                         new ByteArrayInputStream(buf, len - n, n), in);



 216         }
 217         // Uses left-to-right evaluation order
 218         if ((readUInt(in) != crc.getValue()) ||
 219             // rfc1952; ISIZE is the input size modulo 2^32
 220             (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL)))
 221             throw new ZipException("Corrupt GZIP trailer");
 222 
 223         // If there are more bytes available in "in" or
 224         // the leftover in the "inf" is > 26 bytes:
 225         // this.trailer(8) + next.header.min(10) + next.trailer(8)
 226         // try concatenated case
 227         if (this.in.available() > 0 || n > 26) {
 228             int m = 8;                  // this.trailer
 229             try {
 230                 m += readHeader(in);    // next.header
 231             } catch (IOException ze) {
 232                 return true;  // ignore any malformed, do nothing
 233             }
 234             inf.reset();
 235             if (n > m)


   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.io.SequenceInputStream;
  29 import java.io.ByteArrayInputStream;
  30 import java.io.FilterInputStream;
  31 import java.io.InputStream;
  32 import java.io.IOException;
  33 import java.io.EOFException;
  34 
  35 /**
  36  * This class implements a stream filter for reading compressed data in
  37  * the GZIP file format.
  38  *
  39  * @see         InflaterInputStream
  40  * @author      David Connelly
  41  *
  42  */
  43 public
  44 class GZIPInputStream extends InflaterInputStream {
  45     /**
  46      * CRC-32 for uncompressed data.
  47      */
  48     protected CRC32 crc = new CRC32();
  49 
  50     /**


 196             int v = (int)crc.getValue() & 0xffff;
 197             if (readUShort(in) != v) {
 198                 throw new ZipException("Corrupt GZIP header");
 199             }
 200             n += 2;
 201         }
 202         crc.reset();
 203         return n;
 204     }
 205 
 206     /*
 207      * Reads GZIP member trailer and returns true if the eos
 208      * reached, false if there are more (concatenated gzip
 209      * data set)
 210      */
 211     private boolean readTrailer() throws IOException {
 212         InputStream in = this.in;
 213         int n = inf.getRemaining();
 214         if (n > 0) {
 215             in = new SequenceInputStream(
 216                         new ByteArrayInputStream(buf, len - n, n),
 217                         new FilterInputStream(in) {
 218                             public void close() throws IOException {}
 219                         });
 220         }
 221         // Uses left-to-right evaluation order
 222         if ((readUInt(in) != crc.getValue()) ||
 223             // rfc1952; ISIZE is the input size modulo 2^32
 224             (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL)))
 225             throw new ZipException("Corrupt GZIP trailer");
 226 
 227         // If there are more bytes available in "in" or
 228         // the leftover in the "inf" is > 26 bytes:
 229         // this.trailer(8) + next.header.min(10) + next.trailer(8)
 230         // try concatenated case
 231         if (this.in.available() > 0 || n > 26) {
 232             int m = 8;                  // this.trailer
 233             try {
 234                 m += readHeader(in);    // next.header
 235             } catch (IOException ze) {
 236                 return true;  // ignore any malformed, do nothing
 237             }
 238             inf.reset();
 239             if (n > m)