home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / GZIPInputStream.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  219 lines

  1. /*
  2.  * @(#)GZIPInputStream.java    1.15 97/01/20
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. import java.io.SequenceInputStream;
  26. import java.io.ByteArrayInputStream;
  27. import java.io.InputStream;
  28. import java.io.IOException;
  29. import java.io.EOFException;
  30.  
  31. /**
  32.  * This class implements a stream filter for reading compressed data in
  33.  * the GZIP format.
  34.  *
  35.  * @see        InflaterInputStream
  36.  * @version     1.15, 01/20/97
  37.  * @author     David Connelly
  38.  *
  39.  */
  40. public
  41. class GZIPInputStream extends InflaterInputStream {
  42.     /**
  43.      * CRC-32 for uncompressed data.
  44.      */
  45.     protected CRC32 crc = new CRC32();
  46.  
  47.     /**
  48.      * Indicates end of input stream.
  49.      */
  50.     protected boolean eos;
  51.  
  52.     /**
  53.      * Creates a new input stream with the specified buffer size.
  54.      * @param in the input stream
  55.      * @param size the input buffer size
  56.      * @exception IOException if an I/O error has occurred
  57.      */
  58.     public GZIPInputStream(InputStream in, int size) throws IOException {
  59.     super(in, new Inflater(true), size);
  60.     readHeader();
  61.     crc.reset();
  62.     }
  63.  
  64.     /**
  65.      * Creates a new input stream with a default buffer size.
  66.      * @param in the input stream
  67.      * @exception IOException if an I/O error has occurred
  68.      */
  69.     public GZIPInputStream(InputStream in) throws IOException {
  70.     this(in, 512);
  71.     }
  72.  
  73.     /**
  74.      * Reads uncompressed data into an array of bytes. Blocks until enough
  75.      * input is available for decompression.
  76.      * @param buf the buffer into which the data is read
  77.      * @param off the start offset of the data
  78.      * @param len the maximum number of bytes read
  79.      * @return    the actual number of bytes read, or -1 if the end of the
  80.      *        compressed input stream is reached
  81.      * @exception IOException if an I/O error has occurred or the compressed
  82.      *                  input data is corrupt
  83.      */
  84.     public int read(byte[] buf, int off, int len) throws IOException {
  85.     if (eos) {
  86.         return -1;
  87.     }
  88.     len = super.read(buf, off, len);
  89.     if (len == -1) {
  90.         readTrailer();
  91.         eos = true;
  92.     } else {
  93.         crc.update(buf, off, len);
  94.     }
  95.     return len;
  96.     }
  97.  
  98.     /**
  99.      * Closes the input stream.
  100.      * @exception IOException if an I/O error has occurred
  101.      */
  102.     public void close() throws IOException {
  103.     inf.end();
  104.     in.close();
  105.     eos = true;
  106.     }
  107.  
  108.     /**
  109.      * GZIP header magic number.
  110.      */
  111.     public final static int GZIP_MAGIC = 0x8b1f;
  112.  
  113.     /*
  114.      * File header flags.
  115.      */
  116.     private final static int FTEXT    = 1;    // Extra text
  117.     private final static int FHCRC    = 2;    // Header CRC
  118.     private final static int FEXTRA    = 4;    // Extra field
  119.     private final static int FNAME    = 8;    // File name
  120.     private final static int FCOMMENT    = 16;    // File comment
  121.  
  122.     /*
  123.      * Reads GZIP member header.
  124.      */
  125.     private void readHeader() throws IOException {
  126.     CheckedInputStream in = new CheckedInputStream(this.in, crc);
  127.     crc.reset();
  128.     // Check header magic
  129.     if (readUShort(in) != GZIP_MAGIC) {
  130.         throw new IOException("Not in GZIP format");
  131.     }
  132.     // Check compression method
  133.     if (readUByte(in) != 8) {
  134.         throw new IOException("Unsupported compression method");
  135.     }
  136.     // Read flags
  137.     int flg = readUByte(in);
  138.     // Skip MTIME, XFL, and OS fields
  139.     skipBytes(in, 6);
  140.     // Skip optional extra field
  141.     if ((flg & FEXTRA) == FEXTRA) {
  142.         skipBytes(in, readUShort(in));
  143.     }
  144.     // Skip optional file name
  145.     if ((flg & FNAME) == FNAME) {
  146.         while (readUByte(in) != 0) ;
  147.     }
  148.     // Skip optional file comment
  149.     if ((flg & FCOMMENT) == FCOMMENT) {
  150.         while (readUByte(in) != 0) ;
  151.     }
  152.     // Check optional header CRC
  153.     if ((flg & FHCRC) == FHCRC) {
  154.         int v = (int)crc.getValue() & 0xffff;
  155.         if (readUShort(in) != v) {
  156.         throw new IOException("Corrupt GZIP header");
  157.         }
  158.     }
  159.     }
  160.  
  161.     /*
  162.      * Reads GZIP member trailer.
  163.      */
  164.     private void readTrailer() throws IOException {
  165.     InputStream in = this.in;
  166.     int n = inf.getRemaining();
  167.     if (n > 0) {
  168.         in = new SequenceInputStream(
  169.             new ByteArrayInputStream(buf, len - n, n), in);
  170.     }
  171.     long v = crc.getValue();
  172.     if (readUInt(in) != v || readUInt(in) != inf.getTotalOut()) {
  173.         throw new IOException("Corrupt GZIP trailer");
  174.     }
  175.     }
  176.  
  177.     /*
  178.      * Reads unsigned integer in Intel byte order.
  179.      */
  180.     private long readUInt(InputStream in) throws IOException {
  181.     long s = readUShort(in);
  182.     return ((long)readUShort(in) << 16) | s;
  183.     }
  184.  
  185.     /*
  186.      * Reads unsigned short in Intel byte order.
  187.      */
  188.     private int readUShort(InputStream in) throws IOException {
  189.     int b = readUByte(in);
  190.     return ((int)readUByte(in) << 8) | b;
  191.     }
  192.  
  193.     /*
  194.      * Reads unsigned byte.
  195.      */
  196.     private int readUByte(InputStream in) throws IOException {
  197.     int b = in.read();
  198.     if (b == -1) {
  199.         throw new EOFException();
  200.     }
  201.     return b;
  202.     }
  203.  
  204.     /*
  205.      * Skips bytes of input data blocking until all bytes are skipped.
  206.      * Does not assume that the input stream is capable of seeking.
  207.      */
  208.     private void skipBytes(InputStream in, int n) throws IOException {
  209.     byte[] buf = new byte[128];
  210.     while (n > 0) {
  211.         int len = in.read(buf, 0, n < buf.length ? n : buf.length);
  212.         if (len == -1) {
  213.         throw new EOFException();
  214.         }
  215.         n -= len;
  216.     }
  217.     }
  218. }
  219.