home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / java / util / zip / ZipInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  4.9 KB  |  323 lines

  1. package java.util.zip;
  2.  
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.PushbackInputStream;
  7.  
  8. public class ZipInputStream extends InflaterInputStream implements ZipConstants {
  9.    private ZipEntry entry;
  10.    private CRC32 crc = new CRC32();
  11.    private long remaining;
  12.    private byte[] tmpbuf = new byte[512];
  13.    private static final int STORED = 0;
  14.    private static final int DEFLATED = 8;
  15.    private boolean closed = false;
  16.    private boolean entryEOF = false;
  17.  
  18.    private void ensureOpen() throws IOException {
  19.       if (this.closed) {
  20.          throw new IOException("Stream closed");
  21.       }
  22.    }
  23.  
  24.    public ZipInputStream(InputStream var1) {
  25.       super(new PushbackInputStream(var1, 512), new Inflater(true), 512);
  26.       if (var1 == null) {
  27.          throw new NullPointerException("in is null");
  28.       }
  29.    }
  30.  
  31.    public ZipEntry getNextEntry() throws IOException {
  32.       this.ensureOpen();
  33.       if (this.entry != null) {
  34.          this.closeEntry();
  35.       }
  36.  
  37.       this.crc.reset();
  38.       super.inf.reset();
  39.       if ((this.entry = this.readLOC()) == null) {
  40.          return null;
  41.       } else {
  42.          if (this.entry.method == 0) {
  43.             this.remaining = this.entry.size;
  44.          }
  45.  
  46.          this.entryEOF = false;
  47.          return this.entry;
  48.       }
  49.    }
  50.  
  51.    public void closeEntry() throws IOException {
  52.       this.ensureOpen();
  53.  
  54.       while(this.read(this.tmpbuf, 0, this.tmpbuf.length) != -1) {
  55.       }
  56.  
  57.       this.entryEOF = true;
  58.    }
  59.  
  60.    public int available() throws IOException {
  61.       this.ensureOpen();
  62.       return this.entryEOF ? 0 : 1;
  63.    }
  64.  
  65.    public int read(byte[] var1, int var2, int var3) throws IOException {
  66.       this.ensureOpen();
  67.       if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
  68.          if (var3 == 0) {
  69.             return 0;
  70.          } else if (this.entry == null) {
  71.             return -1;
  72.          } else {
  73.             switch (this.entry.method) {
  74.                case 0:
  75.                   if (this.remaining <= 0L) {
  76.                      this.entryEOF = true;
  77.                      this.entry = null;
  78.                      return -1;
  79.                   } else {
  80.                      if ((long)var3 > this.remaining) {
  81.                         var3 = (int)this.remaining;
  82.                      }
  83.  
  84.                      var3 = super.in.read(var1, var2, var3);
  85.                      if (var3 == -1) {
  86.                         throw new ZipException("unexpected EOF");
  87.                      }
  88.  
  89.                      this.crc.update(var1, var2, var3);
  90.                      this.remaining -= (long)var3;
  91.                      return var3;
  92.                   }
  93.                case 8:
  94.                   var3 = super.read(var1, var2, var3);
  95.                   if (var3 == -1) {
  96.                      this.readEnd(this.entry);
  97.                      this.entryEOF = true;
  98.                      this.entry = null;
  99.                   } else {
  100.                      this.crc.update(var1, var2, var3);
  101.                   }
  102.  
  103.                   return var3;
  104.                default:
  105.                   throw new InternalError("invalid compression method");
  106.             }
  107.          }
  108.       } else {
  109.          throw new IndexOutOfBoundsException();
  110.       }
  111.    }
  112.  
  113.    public long skip(long var1) throws IOException {
  114.       if (var1 < 0L) {
  115.          throw new IllegalArgumentException("negative skip length");
  116.       } else {
  117.          this.ensureOpen();
  118.          int var3 = (int)Math.min(var1, 2147483647L);
  119.  
  120.          int var4;
  121.          int var6;
  122.          for(var4 = 0; var4 < var3; var4 += var6) {
  123.             var6 = var3 - var4;
  124.             if (var6 > this.tmpbuf.length) {
  125.                var6 = this.tmpbuf.length;
  126.             }
  127.  
  128.             var6 = this.read(this.tmpbuf, 0, var6);
  129.             if (var6 == -1) {
  130.                this.entryEOF = true;
  131.                break;
  132.             }
  133.          }
  134.  
  135.          return (long)var4;
  136.       }
  137.    }
  138.  
  139.    public void close() throws IOException {
  140.       super.close();
  141.       this.closed = true;
  142.    }
  143.  
  144.    private ZipEntry readLOC() throws IOException {
  145.       try {
  146.          this.readFully(this.tmpbuf, 0, 30);
  147.       } catch (EOFException var4) {
  148.          return null;
  149.       }
  150.  
  151.       if (get32(this.tmpbuf, 0) != 67324752L) {
  152.          return null;
  153.       } else {
  154.          int var1 = get16(this.tmpbuf, 26);
  155.          if (var1 == 0) {
  156.             throw new ZipException("missing entry name");
  157.          } else {
  158.             byte[] var2 = new byte[var1];
  159.             this.readFully(var2, 0, var1);
  160.             ZipEntry var3 = this.createZipEntry(getUTF8String(var2, 0, var1));
  161.             var3.version = get16(this.tmpbuf, 4);
  162.             var3.flag = get16(this.tmpbuf, 6);
  163.             if ((var3.flag & 1) == 1) {
  164.                throw new ZipException("encrypted ZIP entry not supported");
  165.             } else {
  166.                var3.method = get16(this.tmpbuf, 8);
  167.                var3.time = get32(this.tmpbuf, 10);
  168.                if ((var3.flag & 8) == 8) {
  169.                   if (var3.method != 8) {
  170.                      throw new ZipException("only DEFLATED entries can have EXT descriptor");
  171.                   }
  172.                } else {
  173.                   var3.crc = get32(this.tmpbuf, 14);
  174.                   var3.csize = get32(this.tmpbuf, 18);
  175.                   var3.size = get32(this.tmpbuf, 22);
  176.                }
  177.  
  178.                var1 = get16(this.tmpbuf, 28);
  179.                if (var1 > 0) {
  180.                   var2 = new byte[var1];
  181.                   this.readFully(var2, 0, var1);
  182.                   var3.extra = var2;
  183.                }
  184.  
  185.                return var3;
  186.             }
  187.          }
  188.       }
  189.    }
  190.  
  191.    private static String getUTF8String(byte[] var0, int var1, int var2) {
  192.       int var3 = 0;
  193.       int var4 = var1 + var2;
  194.       int var5 = var1;
  195.  
  196.       while(var5 < var4) {
  197.          int var6 = var0[var5++] & 255;
  198.          switch (var6 >> 4) {
  199.             case 0:
  200.             case 1:
  201.             case 2:
  202.             case 3:
  203.             case 4:
  204.             case 5:
  205.             case 6:
  206.             case 7:
  207.                ++var3;
  208.                break;
  209.             case 8:
  210.             case 9:
  211.             case 10:
  212.             case 11:
  213.             default:
  214.                throw new IllegalArgumentException();
  215.             case 12:
  216.             case 13:
  217.                if ((var0[var5++] & 192) != 128) {
  218.                   throw new IllegalArgumentException();
  219.                }
  220.  
  221.                ++var3;
  222.                break;
  223.             case 14:
  224.                if ((var0[var5++] & 192) != 128 || (var0[var5++] & 192) != 128) {
  225.                   throw new IllegalArgumentException();
  226.                }
  227.  
  228.                ++var3;
  229.          }
  230.       }
  231.  
  232.       if (var5 != var4) {
  233.          throw new IllegalArgumentException();
  234.       } else {
  235.          char[] var12 = new char[var3];
  236.          var5 = 0;
  237.  
  238.          while(var1 < var4) {
  239.             int var7 = var0[var1++] & 255;
  240.             switch (var7 >> 4) {
  241.                case 0:
  242.                case 1:
  243.                case 2:
  244.                case 3:
  245.                case 4:
  246.                case 5:
  247.                case 6:
  248.                case 7:
  249.                   var12[var5++] = (char)var7;
  250.                   break;
  251.                case 8:
  252.                case 9:
  253.                case 10:
  254.                case 11:
  255.                default:
  256.                   throw new IllegalArgumentException();
  257.                case 12:
  258.                case 13:
  259.                   var12[var5++] = (char)((var7 & 31) << 6 | var0[var1++] & 63);
  260.                   break;
  261.                case 14:
  262.                   int var8 = (var0[var1++] & 63) << 6;
  263.                   var12[var5++] = (char)((var7 & 15) << 12 | var8 | var0[var1++] & 63);
  264.             }
  265.          }
  266.  
  267.          return new String(var12, 0, var3);
  268.       }
  269.    }
  270.  
  271.    protected ZipEntry createZipEntry(String var1) {
  272.       return new ZipEntry(var1);
  273.    }
  274.  
  275.    private void readEnd(ZipEntry var1) throws IOException {
  276.       int var2 = super.inf.getRemaining();
  277.       if (var2 > 0) {
  278.          ((PushbackInputStream)super.in).unread(super.buf, super.len - var2, var2);
  279.       }
  280.  
  281.       if ((var1.flag & 8) == 8) {
  282.          this.readFully(this.tmpbuf, 0, 16);
  283.          long var3 = get32(this.tmpbuf, 0);
  284.          if (var3 != 134695760L) {
  285.             throw new ZipException("invalid EXT descriptor signature");
  286.          }
  287.  
  288.          var1.crc = get32(this.tmpbuf, 4);
  289.          var1.csize = get32(this.tmpbuf, 8);
  290.          var1.size = get32(this.tmpbuf, 12);
  291.       }
  292.  
  293.       if (var1.size != (long)super.inf.getTotalOut()) {
  294.          throw new ZipException("invalid entry size (expected " + var1.size + " but got " + super.inf.getTotalOut() + " bytes)");
  295.       } else if (var1.csize != (long)super.inf.getTotalIn()) {
  296.          throw new ZipException("invalid entry compressed size (expected " + var1.csize + " but got " + super.inf.getTotalIn() + " bytes)");
  297.       } else if (var1.crc != this.crc.getValue()) {
  298.          throw new ZipException("invalid entry CRC (expected 0x" + Long.toHexString(var1.crc) + " but got 0x" + Long.toHexString(this.crc.getValue()) + ")");
  299.       }
  300.    }
  301.  
  302.    private void readFully(byte[] var1, int var2, int var3) throws IOException {
  303.       while(var3 > 0) {
  304.          int var4 = super.in.read(var1, var2, var3);
  305.          if (var4 == -1) {
  306.             throw new EOFException();
  307.          }
  308.  
  309.          var2 += var4;
  310.          var3 -= var4;
  311.       }
  312.  
  313.    }
  314.  
  315.    private static final int get16(byte[] var0, int var1) {
  316.       return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
  317.    }
  318.  
  319.    private static final long get32(byte[] var0, int var1) {
  320.       return (long)get16(var0, var1) | (long)get16(var0, var1 + 2) << 16;
  321.    }
  322. }
  323.