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