f21829cef130122538a10bde31315edc56202dd5
[jalviewjs.git] / site / j2s / java / util / zip / GZIPInputStream.js
1 Clazz.declarePackage ("java.util.zip");
2 Clazz.load (["java.util.zip.InflaterInputStream", "$.CRC32"], "java.util.zip.GZIPInputStream", ["java.io.EOFException", "$.IOException", "java.util.zip.CheckedInputStream", "$.Inflater", "$.ZipException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.crc = null;
5 this.eos = false;
6 this.$closed = false;
7 this.tmpbuf = null;
8 Clazz.instantialize (this, arguments);
9 }, java.util.zip, "GZIPInputStream", java.util.zip.InflaterInputStream);
10 Clazz.prepareFields (c$, function () {
11 this.crc =  new java.util.zip.CRC32 ();
12 this.tmpbuf =  Clazz.newByteArray (128, 0);
13 });
14 Clazz.defineMethod (c$, "ensureOpen", 
15  function () {
16 if (this.$closed) {
17 throw  new java.io.IOException ("Stream closed");
18 }});
19 Clazz.makeConstructor (c$, 
20 function ($in, size) {
21 Clazz.superConstructor (this, java.util.zip.GZIPInputStream, [$in,  new java.util.zip.Inflater ().init (0, true), size]);
22 this.readHeader ($in);
23 }, "java.io.InputStream,~N");
24 Clazz.overrideMethod (c$, "read", 
25 function (buf, off, len) {
26 this.ensureOpen ();
27 if (this.eos) {
28 return -1;
29 }var n = this.readInf (buf, off, len);
30 if (n == -1) {
31 if (this.readTrailer ()) this.eos = true;
32  else return this.read (buf, off, len);
33 } else {
34 this.crc.update (buf, off, n);
35 }return n;
36 }, "~A,~N,~N");
37 Clazz.defineMethod (c$, "close", 
38 function () {
39 if (!this.$closed) {
40 Clazz.superCall (this, java.util.zip.GZIPInputStream, "close", []);
41 this.eos = true;
42 this.$closed = true;
43 }});
44 Clazz.defineMethod (c$, "readHeader", 
45  function (this_in) {
46 var $in =  new java.util.zip.CheckedInputStream (this_in).set (this.crc);
47 this.crc.reset ();
48 if (this.readUShort ($in) != 35615) {
49 throw  new java.util.zip.ZipException ("Not in GZIP format");
50 }if (this.readUByte ($in) != 8) {
51 throw  new java.util.zip.ZipException ("Unsupported compression method");
52 }var flg = this.readUByte ($in);
53 this.skipBytes ($in, 6);
54 var n = 10;
55 if ((flg & 4) == 4) {
56 var m = this.readUShort ($in);
57 this.skipBytes ($in, m);
58 n += m + 2;
59 }if ((flg & 8) == 8) {
60 do {
61 n++;
62 } while (this.readUByte ($in) != 0);
63 }if ((flg & 16) == 16) {
64 do {
65 n++;
66 } while (this.readUByte ($in) != 0);
67 }if ((flg & 2) == 2) {
68 var v = this.crc.getValue () & 0xffff;
69 if (this.readUShort ($in) != v) {
70 throw  new java.util.zip.ZipException ("Corrupt GZIP header");
71 }n += 2;
72 }this.crc.reset ();
73 return n;
74 }, "java.io.InputStream");
75 Clazz.defineMethod (c$, "readTrailer", 
76  function () {
77 return true;
78 });
79 Clazz.defineMethod (c$, "readUShort", 
80  function ($in) {
81 var b = this.readUByte ($in);
82 return (this.readUByte ($in) << 8) | b;
83 }, "java.io.InputStream");
84 Clazz.defineMethod (c$, "readUByte", 
85  function ($in) {
86 var b = $in.readByteAsInt ();
87 if (b == -1) {
88 throw  new java.io.EOFException ();
89 }if (b < -1 || b > 255) {
90 throw  new java.io.IOException (this.$in.getClass ().getName () + ".read() returned value out of range -1..255: " + b);
91 }return b;
92 }, "java.io.InputStream");
93 Clazz.defineMethod (c$, "skipBytes", 
94  function ($in, n) {
95 while (n > 0) {
96 var len = $in.read (this.tmpbuf, 0, n < this.tmpbuf.length ? n : this.tmpbuf.length);
97 if (len == -1) {
98 throw  new java.io.EOFException ();
99 }n -= len;
100 }
101 }, "java.io.InputStream,~N");
102 Clazz.defineStatics (c$,
103 "GZIP_MAGIC", 0x8b1f,
104 "FHCRC", 2,
105 "FEXTRA", 4,
106 "FNAME", 8,
107 "FCOMMENT", 16);
108 });