home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / java / util / zip / GZIPInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  2.8 KB  |  129 lines

  1. package java.util.zip;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.EOFException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.SequenceInputStream;
  8.  
  9. public class GZIPInputStream extends InflaterInputStream {
  10.    protected CRC32 crc;
  11.    protected boolean eos;
  12.    public static final int GZIP_MAGIC = 35615;
  13.    private static final int FTEXT = 1;
  14.    private static final int FHCRC = 2;
  15.    private static final int FEXTRA = 4;
  16.    private static final int FNAME = 8;
  17.    private static final int FCOMMENT = 16;
  18.  
  19.    public GZIPInputStream(InputStream var1, int var2) throws IOException {
  20.       super(var1, new Inflater(true), var2);
  21.       this.crc = new CRC32();
  22.       this.readHeader();
  23.       this.crc.reset();
  24.    }
  25.  
  26.    public GZIPInputStream(InputStream var1) throws IOException {
  27.       this(var1, 512);
  28.    }
  29.  
  30.    public int read(byte[] var1, int var2, int var3) throws IOException {
  31.       if (this.eos) {
  32.          return -1;
  33.       } else {
  34.          var3 = super.read(var1, var2, var3);
  35.          if (var3 == -1) {
  36.             this.readTrailer();
  37.             this.eos = true;
  38.          } else {
  39.             this.crc.update(var1, var2, var3);
  40.          }
  41.  
  42.          return var3;
  43.       }
  44.    }
  45.  
  46.    public void close() throws IOException {
  47.       super.inf.end();
  48.       super.in.close();
  49.       this.eos = true;
  50.    }
  51.  
  52.    private void readHeader() throws IOException {
  53.       CheckedInputStream var1 = new CheckedInputStream(super.in, this.crc);
  54.       this.crc.reset();
  55.       if (this.readUShort(var1) != 35615) {
  56.          throw new IOException("Not in GZIP format");
  57.       } else if (this.readUByte(var1) != 8) {
  58.          throw new IOException("Unsupported compression method");
  59.       } else {
  60.          int var2 = this.readUByte(var1);
  61.          this.skipBytes(var1, 6);
  62.          if ((var2 & 4) == 4) {
  63.             this.skipBytes(var1, this.readUShort(var1));
  64.          }
  65.  
  66.          if ((var2 & 8) == 8) {
  67.             while(this.readUByte(var1) != 0) {
  68.             }
  69.          }
  70.  
  71.          if ((var2 & 16) == 16) {
  72.             while(this.readUByte(var1) != 0) {
  73.             }
  74.          }
  75.  
  76.          if ((var2 & 2) == 2) {
  77.             int var3 = (int)this.crc.getValue() & '\uffff';
  78.             if (this.readUShort(var1) != var3) {
  79.                throw new IOException("Corrupt GZIP header");
  80.             }
  81.          }
  82.  
  83.       }
  84.    }
  85.  
  86.    private void readTrailer() throws IOException {
  87.       Object var1 = super.in;
  88.       int var2 = super.inf.getRemaining();
  89.       if (var2 > 0) {
  90.          var1 = new SequenceInputStream(new ByteArrayInputStream(super.buf, super.len - var2, var2), (InputStream)var1);
  91.       }
  92.  
  93.       long var3 = this.crc.getValue();
  94.       if (this.readUInt((InputStream)var1) != var3 || this.readUInt((InputStream)var1) != (long)super.inf.getTotalOut()) {
  95.          throw new IOException("Corrupt GZIP trailer");
  96.       }
  97.    }
  98.  
  99.    private long readUInt(InputStream var1) throws IOException {
  100.       long var2 = (long)this.readUShort(var1);
  101.       return (long)this.readUShort(var1) << 16 | var2;
  102.    }
  103.  
  104.    private int readUShort(InputStream var1) throws IOException {
  105.       int var2 = this.readUByte(var1);
  106.       return this.readUByte(var1) << 8 | var2;
  107.    }
  108.  
  109.    private int readUByte(InputStream var1) throws IOException {
  110.       int var2 = var1.read();
  111.       if (var2 == -1) {
  112.          throw new EOFException();
  113.       } else {
  114.          return var2;
  115.       }
  116.    }
  117.  
  118.    private void skipBytes(InputStream var1, int var2) throws IOException {
  119.       int var4;
  120.       for(byte[] var3 = new byte[128]; var2 > 0; var2 -= var4) {
  121.          var4 = var1.read(var3, 0, var2 < var3.length ? var2 : var3.length);
  122.          if (var4 == -1) {
  123.             throw new EOFException();
  124.          }
  125.       }
  126.  
  127.    }
  128. }
  129.