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