home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &… the Search for Life CD 3 / 0_CD-ROM.iso / install / jre1_3 / lib / rt.jar / java / util / zip / GZIPInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  2.6 KB  |  139 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.    private boolean closed;
  13.    public static final int GZIP_MAGIC = 35615;
  14.    private static final int FTEXT = 1;
  15.    private static final int FHCRC = 2;
  16.    private static final int FEXTRA = 4;
  17.    private static final int FNAME = 8;
  18.    private static final int FCOMMENT = 16;
  19.  
  20.    private void ensureOpen() throws IOException {
  21.       if (this.closed) {
  22.          throw new IOException("Stream closed");
  23.       }
  24.    }
  25.  
  26.    public GZIPInputStream(InputStream var1, int var2) throws IOException {
  27.       super(var1, new Inflater(true), var2);
  28.       this.crc = new CRC32();
  29.       this.closed = false;
  30.       this.readHeader();
  31.       this.crc.reset();
  32.    }
  33.  
  34.    public GZIPInputStream(InputStream var1) throws IOException {
  35.       this(var1, 512);
  36.    }
  37.  
  38.    public int read(byte[] var1, int var2, int var3) throws IOException {
  39.       this.ensureOpen();
  40.       if (this.eos) {
  41.          return -1;
  42.       } else {
  43.          var3 = super.read(var1, var2, var3);
  44.          if (var3 == -1) {
  45.             this.readTrailer();
  46.             this.eos = true;
  47.          } else {
  48.             this.crc.update(var1, var2, var3);
  49.          }
  50.  
  51.          return var3;
  52.       }
  53.    }
  54.  
  55.    public void close() throws IOException {
  56.       super.inf.end();
  57.       super.in.close();
  58.       this.eos = true;
  59.       this.closed = true;
  60.    }
  61.  
  62.    private void readHeader() throws IOException {
  63.       CheckedInputStream var1 = new CheckedInputStream(super.in, this.crc);
  64.       this.crc.reset();
  65.       if (this.readUShort(var1) != 35615) {
  66.          throw new IOException("Not in GZIP format");
  67.       } else if (this.readUByte(var1) != 8) {
  68.          throw new IOException("Unsupported compression method");
  69.       } else {
  70.          int var2 = this.readUByte(var1);
  71.          this.skipBytes(var1, 6);
  72.          if ((var2 & 4) == 4) {
  73.             this.skipBytes(var1, this.readUShort(var1));
  74.          }
  75.  
  76.          if ((var2 & 8) == 8) {
  77.             while(this.readUByte(var1) != 0) {
  78.             }
  79.          }
  80.  
  81.          if ((var2 & 16) == 16) {
  82.             while(this.readUByte(var1) != 0) {
  83.             }
  84.          }
  85.  
  86.          if ((var2 & 2) == 2) {
  87.             int var3 = (int)this.crc.getValue() & '\uffff';
  88.             if (this.readUShort(var1) != var3) {
  89.                throw new IOException("Corrupt GZIP header");
  90.             }
  91.          }
  92.  
  93.       }
  94.    }
  95.  
  96.    private void readTrailer() throws IOException {
  97.       Object var1 = super.in;
  98.       int var2 = super.inf.getRemaining();
  99.       if (var2 > 0) {
  100.          var1 = new SequenceInputStream(new ByteArrayInputStream(super.buf, super.len - var2, var2), (InputStream)var1);
  101.       }
  102.  
  103.       long var3 = this.crc.getValue();
  104.       if (this.readUInt((InputStream)var1) != var3 || this.readUInt((InputStream)var1) != (long)super.inf.getTotalOut()) {
  105.          throw new IOException("Corrupt GZIP trailer");
  106.       }
  107.    }
  108.  
  109.    private long readUInt(InputStream var1) throws IOException {
  110.       long var2 = (long)this.readUShort(var1);
  111.       return (long)this.readUShort(var1) << 16 | var2;
  112.    }
  113.  
  114.    private int readUShort(InputStream var1) throws IOException {
  115.       int var2 = this.readUByte(var1);
  116.       return this.readUByte(var1) << 8 | var2;
  117.    }
  118.  
  119.    private int readUByte(InputStream var1) throws IOException {
  120.       int var2 = var1.read();
  121.       if (var2 == -1) {
  122.          throw new EOFException();
  123.       } else {
  124.          return var2;
  125.       }
  126.    }
  127.  
  128.    private void skipBytes(InputStream var1, int var2) throws IOException {
  129.       int var4;
  130.       for(byte[] var3 = new byte[128]; var2 > 0; var2 -= var4) {
  131.          var4 = var1.read(var3, 0, var2 < var3.length ? var2 : var3.length);
  132.          if (var4 == -1) {
  133.             throw new EOFException();
  134.          }
  135.       }
  136.  
  137.    }
  138. }
  139.