home *** CD-ROM | disk | FTP | other *** search
- package java.util.zip;
-
- import java.io.ByteArrayInputStream;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.SequenceInputStream;
-
- public class GZIPInputStream extends InflaterInputStream {
- protected CRC32 crc;
- protected boolean eos;
- public static final int GZIP_MAGIC = 35615;
- private static final int FTEXT = 1;
- private static final int FHCRC = 2;
- private static final int FEXTRA = 4;
- private static final int FNAME = 8;
- private static final int FCOMMENT = 16;
-
- public GZIPInputStream(InputStream var1, int var2) throws IOException {
- super(var1, new Inflater(true), var2);
- this.crc = new CRC32();
- this.readHeader();
- this.crc.reset();
- }
-
- public GZIPInputStream(InputStream var1) throws IOException {
- this(var1, 512);
- }
-
- public int read(byte[] var1, int var2, int var3) throws IOException {
- if (this.eos) {
- return -1;
- } else {
- var3 = super.read(var1, var2, var3);
- if (var3 == -1) {
- this.readTrailer();
- this.eos = true;
- } else {
- this.crc.update(var1, var2, var3);
- }
-
- return var3;
- }
- }
-
- public void close() throws IOException {
- super.inf.end();
- super.in.close();
- this.eos = true;
- }
-
- private void readHeader() throws IOException {
- CheckedInputStream var1 = new CheckedInputStream(super.in, this.crc);
- this.crc.reset();
- if (this.readUShort(var1) != 35615) {
- throw new IOException("Not in GZIP format");
- } else if (this.readUByte(var1) != 8) {
- throw new IOException("Unsupported compression method");
- } else {
- int var2 = this.readUByte(var1);
- this.skipBytes(var1, 6);
- if ((var2 & 4) == 4) {
- this.skipBytes(var1, this.readUShort(var1));
- }
-
- if ((var2 & 8) == 8) {
- while(this.readUByte(var1) != 0) {
- }
- }
-
- if ((var2 & 16) == 16) {
- while(this.readUByte(var1) != 0) {
- }
- }
-
- if ((var2 & 2) == 2) {
- int var3 = (int)this.crc.getValue() & '\uffff';
- if (this.readUShort(var1) != var3) {
- throw new IOException("Corrupt GZIP header");
- }
- }
-
- }
- }
-
- private void readTrailer() throws IOException {
- Object var1 = super.in;
- int var2 = super.inf.getRemaining();
- if (var2 > 0) {
- var1 = new SequenceInputStream(new ByteArrayInputStream(super.buf, super.len - var2, var2), (InputStream)var1);
- }
-
- long var3 = this.crc.getValue();
- if (this.readUInt((InputStream)var1) != var3 || this.readUInt((InputStream)var1) != (long)super.inf.getTotalOut()) {
- throw new IOException("Corrupt GZIP trailer");
- }
- }
-
- private long readUInt(InputStream var1) throws IOException {
- long var2 = (long)this.readUShort(var1);
- return (long)this.readUShort(var1) << 16 | var2;
- }
-
- private int readUShort(InputStream var1) throws IOException {
- int var2 = this.readUByte(var1);
- return this.readUByte(var1) << 8 | var2;
- }
-
- private int readUByte(InputStream var1) throws IOException {
- int var2 = var1.read();
- if (var2 == -1) {
- throw new EOFException();
- } else {
- return var2;
- }
- }
-
- private void skipBytes(InputStream var1, int var2) throws IOException {
- int var4;
- for(byte[] var3 = new byte[128]; var2 > 0; var2 -= var4) {
- var4 = var1.read(var3, 0, var2 < var3.length ? var2 : var3.length);
- if (var4 == -1) {
- throw new EOFException();
- }
- }
-
- }
- }
-