56bcad39b8f9e69cf2f3c5cb633323362bbdbbb6
[jalviewjs.git] / site / j2s / java / util / zip / ZipInputStream.js
1 Clazz.declarePackage ("java.util.zip");\r
2 Clazz.load (["java.util.zip.InflaterInputStream", "$.ZipConstants", "$.CRC32"], "java.util.zip.ZipInputStream", ["java.io.EOFException", "$.IOException", "$.PushbackInputStream", "java.lang.IllegalArgumentException", "$.IndexOutOfBoundsException", "$.Long", "$.NullPointerException", "java.util.zip.Inflater", "$.ZipEntry", "$.ZipException"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.entry = null;\r
5 this.flag = 0;\r
6 this.crc = null;\r
7 this.remaining = 0;\r
8 this.tmpbuf = null;\r
9 this.$closed = false;\r
10 this.entryEOF = false;\r
11 this.zc = null;\r
12 this.byteTest = null;\r
13 this.$b = null;\r
14 Clazz.instantialize (this, arguments);\r
15 }, java.util.zip, "ZipInputStream", java.util.zip.InflaterInputStream, java.util.zip.ZipConstants);\r
16 Clazz.prepareFields (c$, function () {\r
17 this.crc =  new java.util.zip.CRC32 ();\r
18 this.tmpbuf =  Clazz.newByteArray (512, 0);\r
19 this.byteTest =  Clazz.newByteArray (-1, [0x20]);\r
20 this.$b =  Clazz.newByteArray (256, 0);\r
21 });\r
22 Clazz.defineMethod (c$, "ensureOpen", \r
23  function () {\r
24 if (this.$closed) {\r
25 throw  new java.io.IOException ("Stream closed");\r
26 }});\r
27 Clazz.makeConstructor (c$, \r
28 function ($in) {\r
29 Clazz.superConstructor (this, java.util.zip.ZipInputStream, [ new java.io.PushbackInputStream ($in, 1024), java.util.zip.ZipInputStream.newInflater (), 512]);\r
30 var charset = "UTF-8";\r
31 try {\r
32  String.instantialize (this.byteTest, charset);\r
33 } catch (e) {\r
34 if (Clazz.exceptionOf (e, java.io.UnsupportedEncodingException)) {\r
35 throw  new NullPointerException ("charset is invalid");\r
36 } else {\r
37 throw e;\r
38 }\r
39 }\r
40 this.zc = charset;\r
41 }, "java.io.InputStream");\r
42 c$.newInflater = Clazz.defineMethod (c$, "newInflater", \r
43  function () {\r
44 return  new java.util.zip.Inflater ().init (0, true);\r
45 });\r
46 Clazz.defineMethod (c$, "getNextEntry", \r
47 function () {\r
48 this.ensureOpen ();\r
49 if (this.entry != null) {\r
50 this.closeEntry ();\r
51 }this.crc.reset ();\r
52 this.inflater = this.inf = java.util.zip.ZipInputStream.newInflater ();\r
53 if ((this.entry = this.readLOC ()) == null) {\r
54 return null;\r
55 }if (this.entry.method == 0) {\r
56 this.remaining = this.entry.size;\r
57 }this.entryEOF = false;\r
58 return this.entry;\r
59 });\r
60 Clazz.defineMethod (c$, "closeEntry", \r
61 function () {\r
62 this.ensureOpen ();\r
63 while (this.read (this.tmpbuf, 0, this.tmpbuf.length) != -1) {\r
64 }\r
65 this.entryEOF = true;\r
66 });\r
67 Clazz.overrideMethod (c$, "available", \r
68 function () {\r
69 this.ensureOpen ();\r
70 return (this.entryEOF ? 0 : 1);\r
71 });\r
72 Clazz.overrideMethod (c$, "read", \r
73 function (b, off, len) {\r
74 this.ensureOpen ();\r
75 if (off < 0 || len < 0 || off > b.length - len) {\r
76 throw  new IndexOutOfBoundsException ();\r
77 } else if (len == 0) {\r
78 return 0;\r
79 }if (this.entry == null) {\r
80 return -1;\r
81 }switch (this.entry.method) {\r
82 case 8:\r
83 len = this.readInf (b, off, len);\r
84 if (len == -1) {\r
85 this.readEnd (this.entry);\r
86 this.entryEOF = true;\r
87 this.entry = null;\r
88 } else {\r
89 this.crc.update (b, off, len);\r
90 }return len;\r
91 case 0:\r
92 if (this.remaining <= 0) {\r
93 this.entryEOF = true;\r
94 this.entry = null;\r
95 return -1;\r
96 }if (len > this.remaining) {\r
97 len = this.remaining;\r
98 }len = this.$in.read (b, off, len);\r
99 if (len == -1) {\r
100 throw  new java.util.zip.ZipException ("unexpected EOF");\r
101 }this.crc.update (b, off, len);\r
102 this.remaining -= len;\r
103 if (this.remaining == 0 && this.entry.crc != this.crc.getValue ()) {\r
104 throw  new java.util.zip.ZipException ("invalid entry CRC (expected 0x" + Long.toHexString (this.entry.crc) + " but got 0x" + Long.toHexString (this.crc.getValue ()) + ")");\r
105 }return len;\r
106 default:\r
107 throw  new java.util.zip.ZipException ("invalid compression method");\r
108 }\r
109 }, "~A,~N,~N");\r
110 Clazz.overrideMethod (c$, "skip", \r
111 function (n) {\r
112 if (n < 0) {\r
113 throw  new IllegalArgumentException ("negative skip length");\r
114 }this.ensureOpen ();\r
115 var max = Math.min (n, 2147483647);\r
116 var total = 0;\r
117 while (total < max) {\r
118 var len = max - total;\r
119 if (len > this.tmpbuf.length) {\r
120 len = this.tmpbuf.length;\r
121 }len = this.read (this.tmpbuf, 0, len);\r
122 if (len == -1) {\r
123 this.entryEOF = true;\r
124 break;\r
125 }total += len;\r
126 }\r
127 return total;\r
128 }, "~N");\r
129 Clazz.defineMethod (c$, "close", \r
130 function () {\r
131 if (!this.$closed) {\r
132 Clazz.superCall (this, java.util.zip.ZipInputStream, "close", []);\r
133 this.$closed = true;\r
134 }});\r
135 Clazz.defineMethod (c$, "readLOC", \r
136  function () {\r
137 try {\r
138 this.readFully (this.tmpbuf, 0, 30);\r
139 } catch (e) {\r
140 if (Clazz.exceptionOf (e, java.io.EOFException)) {\r
141 return null;\r
142 } else {\r
143 throw e;\r
144 }\r
145 }\r
146 if (java.util.zip.ZipInputStream.get32 (this.tmpbuf, 0) != 67324752) {\r
147 return null;\r
148 }this.flag = java.util.zip.ZipInputStream.get16 (this.tmpbuf, 6);\r
149 var len = java.util.zip.ZipInputStream.get16 (this.tmpbuf, 26);\r
150 var blen = this.$b.length;\r
151 if (len > blen) {\r
152 do blen = blen * 2;\r
153  while (len > blen);\r
154 this.$b =  Clazz.newByteArray (blen, 0);\r
155 }this.readFully (this.$b, 0, len);\r
156 var e = this.createZipEntry (((this.flag & 2048) != 0) ? this.toStringUTF8 (this.$b, len) : this.toStringb2 (this.$b, len));\r
157 if ((this.flag & 1) == 1) {\r
158 throw  new java.util.zip.ZipException ("encrypted ZIP entry not supported");\r
159 }e.method = java.util.zip.ZipInputStream.get16 (this.tmpbuf, 8);\r
160 e.time = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 10);\r
161 if ((this.flag & 8) == 8) {\r
162 if (e.method != 8) {\r
163 throw  new java.util.zip.ZipException ("only DEFLATED entries can have EXT descriptor");\r
164 }} else {\r
165 e.crc = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 14);\r
166 e.csize = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 18);\r
167 e.size = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 22);\r
168 }len = java.util.zip.ZipInputStream.get16 (this.tmpbuf, 28);\r
169 if (len > 0) {\r
170 var bb =  Clazz.newByteArray (len, 0);\r
171 this.readFully (bb, 0, len);\r
172 e.setExtra (bb);\r
173 if (e.csize == 4294967295 || e.size == 4294967295) {\r
174 var off = 0;\r
175 while (off + 4 < len) {\r
176 var sz = java.util.zip.ZipInputStream.get16 (bb, off + 2);\r
177 if (java.util.zip.ZipInputStream.get16 (bb, off) == 1) {\r
178 off += 4;\r
179 if (sz < 16 || (off + sz) > len) {\r
180 return e;\r
181 }e.size = java.util.zip.ZipInputStream.get64 (bb, off);\r
182 e.csize = java.util.zip.ZipInputStream.get64 (bb, off + 8);\r
183 break;\r
184 }off += (sz + 4);\r
185 }\r
186 }}return e;\r
187 });\r
188 Clazz.defineMethod (c$, "toStringUTF8", \r
189  function (b2, len) {\r
190 try {\r
191 return  String.instantialize (b2, 0, len, this.zc);\r
192 } catch (e) {\r
193 if (Clazz.exceptionOf (e, java.io.UnsupportedEncodingException)) {\r
194 return this.toStringb2 (b2, len);\r
195 } else {\r
196 throw e;\r
197 }\r
198 }\r
199 }, "~A,~N");\r
200 Clazz.defineMethod (c$, "toStringb2", \r
201  function (b2, len) {\r
202 return  String.instantialize (b2, 0, len);\r
203 }, "~A,~N");\r
204 Clazz.defineMethod (c$, "createZipEntry", \r
205 function (name) {\r
206 return  new java.util.zip.ZipEntry (name);\r
207 }, "~S");\r
208 Clazz.defineMethod (c$, "readEnd", \r
209  function (e) {\r
210 var n = this.inf.getAvailIn ();\r
211 if (n > 0) {\r
212 (this.$in).unread (this.buf, this.len - n, n);\r
213 this.eof = false;\r
214 }if ((this.flag & 8) == 8) {\r
215 if (this.inf.getTotalOut () > 4294967295 || this.inf.getTotalIn () > 4294967295) {\r
216 this.readFully (this.tmpbuf, 0, 24);\r
217 var sig = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 0);\r
218 if (sig != 134695760) {\r
219 e.crc = sig;\r
220 e.csize = java.util.zip.ZipInputStream.get64 (this.tmpbuf, 4);\r
221 e.size = java.util.zip.ZipInputStream.get64 (this.tmpbuf, 12);\r
222 (this.$in).unread (this.tmpbuf, 19, 4);\r
223 } else {\r
224 e.crc = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 4);\r
225 e.csize = java.util.zip.ZipInputStream.get64 (this.tmpbuf, 8);\r
226 e.size = java.util.zip.ZipInputStream.get64 (this.tmpbuf, 16);\r
227 }} else {\r
228 this.readFully (this.tmpbuf, 0, 16);\r
229 var sig = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 0);\r
230 if (sig != 134695760) {\r
231 e.crc = sig;\r
232 e.csize = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 4);\r
233 e.size = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 8);\r
234 (this.$in).unread (this.tmpbuf, 11, 4);\r
235 } else {\r
236 e.crc = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 4);\r
237 e.csize = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 8);\r
238 e.size = java.util.zip.ZipInputStream.get32 (this.tmpbuf, 12);\r
239 }}}if (e.size != this.inf.getTotalOut ()) {\r
240 throw  new java.util.zip.ZipException ("invalid entry size (expected " + e.size + " but got " + this.inf.getTotalOut () + " bytes)");\r
241 }if (e.csize != this.inf.getTotalIn ()) {\r
242 throw  new java.util.zip.ZipException ("invalid entry compressed size (expected " + e.csize + " but got " + this.inf.getTotalIn () + " bytes)");\r
243 }if (e.crc != this.crc.getValue ()) {\r
244 throw  new java.util.zip.ZipException ("invalid entry CRC (expected 0x" + Long.toHexString (e.crc) + " but got 0x" + Long.toHexString (this.crc.getValue ()) + ")");\r
245 }}, "java.util.zip.ZipEntry");\r
246 Clazz.defineMethod (c$, "readFully", \r
247  function (b, off, len) {\r
248 while (len > 0) {\r
249 var n = this.$in.read (b, off, len);\r
250 if (n == -1) {\r
251 throw  new java.io.EOFException ();\r
252 }off += n;\r
253 len -= n;\r
254 }\r
255 }, "~A,~N,~N");\r
256 c$.get16 = Clazz.defineMethod (c$, "get16", \r
257  function (b, off) {\r
258 return (b[off] & 0xff) | ((b[off + 1] & 0xff) << 8);\r
259 }, "~A,~N");\r
260 c$.get32 = Clazz.defineMethod (c$, "get32", \r
261  function (b, off) {\r
262 return (java.util.zip.ZipInputStream.get16 (b, off) | (java.util.zip.ZipInputStream.get16 (b, off + 2) << 16)) & 0xffffffff;\r
263 }, "~A,~N");\r
264 c$.get64 = Clazz.defineMethod (c$, "get64", \r
265  function (b, off) {\r
266 return java.util.zip.ZipInputStream.get32 (b, off) | (java.util.zip.ZipInputStream.get32 (b, off + 4) << 32);\r
267 }, "~A,~N");\r
268 Clazz.defineStatics (c$,\r
269 "STORED", 0,\r
270 "DEFLATED", 8);\r
271 });\r