JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / java / util / zip / ZipOutputStream.js
1 Clazz.declarePackage ("java.util.zip");\r
2 Clazz.load (["java.util.zip.DeflaterOutputStream", "$.ZipConstants", "java.util.Hashtable", "java.util.zip.CRC32", "JU.Lst"], "java.util.zip.ZipOutputStream", ["JU.ZStream", "java.io.IOException", "java.lang.Boolean", "$.IllegalArgumentException", "$.IndexOutOfBoundsException", "$.Long", "java.util.zip.Deflater", "$.ZipException"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.current = null;\r
5 this.xentries = null;\r
6 this.names = null;\r
7 this.crc = null;\r
8 this.written = 0;\r
9 this.locoff = 0;\r
10 this.comment = null;\r
11 this.method = 8;\r
12 this.finished = false;\r
13 this.$closed = false;\r
14 Clazz.instantialize (this, arguments);\r
15 }, java.util.zip, "ZipOutputStream", java.util.zip.DeflaterOutputStream, java.util.zip.ZipConstants);\r
16 Clazz.prepareFields (c$, function () {\r
17 this.xentries =  new JU.Lst ();\r
18 this.names =  new java.util.Hashtable ();\r
19 this.crc =  new java.util.zip.CRC32 ();\r
20 });\r
21 c$.version = Clazz.defineMethod (c$, "version", \r
22  function (e) {\r
23 switch (e.method) {\r
24 case 8:\r
25 return 20;\r
26 case 0:\r
27 return 10;\r
28 default:\r
29 throw  new java.util.zip.ZipException ("unsupported compression method");\r
30 }\r
31 }, "java.util.zip.ZipEntry");\r
32 Clazz.defineMethod (c$, "ensureOpen", \r
33  function () {\r
34 if (this.$closed) {\r
35 throw  new java.io.IOException ("Stream closed");\r
36 }});\r
37 Clazz.makeConstructor (c$, \r
38 function () {\r
39 Clazz.superConstructor (this, java.util.zip.ZipOutputStream, []);\r
40 });\r
41 Clazz.defineMethod (c$, "setZOS", \r
42 function (out) {\r
43 this.setDOS (out, java.util.zip.ZipOutputStream.newDeflater ());\r
44 return this;\r
45 }, "java.io.OutputStream");\r
46 c$.newDeflater = Clazz.defineMethod (c$, "newDeflater", \r
47  function () {\r
48 return ( new java.util.zip.Deflater (2147483647)).init (-1, 0, true);\r
49 });\r
50 Clazz.defineMethod (c$, "setComment", \r
51 function (comment) {\r
52 if (comment != null) {\r
53 this.comment = JU.ZStream.getBytes (comment);\r
54 if (this.comment.length > 0xffff) throw  new IllegalArgumentException ("ZIP file comment too long.");\r
55 }}, "~S");\r
56 Clazz.defineMethod (c$, "putNextEntry", \r
57 function (e) {\r
58 this.ensureOpen ();\r
59 if (this.current != null) {\r
60 this.closeEntry ();\r
61 }if (e.time == -1) {\r
62 e.setTime (System.currentTimeMillis ());\r
63 }if (e.method == -1) {\r
64 e.method = this.method;\r
65 }e.flag = 0;\r
66 switch (e.method) {\r
67 case 8:\r
68 if (e.size == -1 || e.csize == -1 || e.crc == -1) e.flag = 8;\r
69 break;\r
70 case 0:\r
71 if (e.size == -1) {\r
72 e.size = e.csize;\r
73 } else if (e.csize == -1) {\r
74 e.csize = e.size;\r
75 } else if (e.size != e.csize) {\r
76 throw  new java.util.zip.ZipException ("STORED entry where compressed != uncompressed size");\r
77 }if (e.size == -1 || e.crc == -1) {\r
78 throw  new java.util.zip.ZipException ("STORED entry missing size, compressed size, or crc-32");\r
79 }break;\r
80 default:\r
81 throw  new java.util.zip.ZipException ("unsupported compression method");\r
82 }\r
83 if (this.names.containsKey (e.name)) {\r
84 throw  new java.util.zip.ZipException ("duplicate entry: " + e.name);\r
85 }this.names.put (e.name, Boolean.TRUE);\r
86 e.flag |= 2048;\r
87 this.current = e;\r
88 this.current.offset = this.written;\r
89 this.xentries.addLast (this.current);\r
90 this.writeLOC (this.current);\r
91 }, "java.util.zip.ZipEntry");\r
92 Clazz.defineMethod (c$, "closeEntry", \r
93 function () {\r
94 this.ensureOpen ();\r
95 if (this.current != null) {\r
96 var e = this.current;\r
97 switch (e.method) {\r
98 case 8:\r
99 this.deflater.finish ();\r
100 Clazz.superCall (this, java.util.zip.ZipOutputStream, "finish", []);\r
101 if ((e.flag & 8) == 0) {\r
102 if (e.size != this.deflater.getBytesRead ()) {\r
103 throw  new java.util.zip.ZipException ("invalid entry size (expected " + e.size + " but got " + this.deflater.getBytesRead () + " bytes)");\r
104 }if (e.csize != this.deflater.getBytesWritten ()) {\r
105 throw  new java.util.zip.ZipException ("invalid entry compressed size (expected " + e.csize + " but got " + this.deflater.getBytesWritten () + " bytes)");\r
106 }if (e.crc != this.crc.getValue ()) {\r
107 throw  new java.util.zip.ZipException ("invalid entry CRC-32 (expected 0x" + Long.toHexString (e.crc) + " but got 0x" + Long.toHexString (this.crc.getValue ()) + ")");\r
108 }} else {\r
109 e.size = this.deflater.getBytesRead ();\r
110 e.csize = this.deflater.getBytesWritten ();\r
111 e.crc = this.crc.getValue ();\r
112 this.writeEXT (e);\r
113 }this.deflater = java.util.zip.ZipOutputStream.newDeflater ();\r
114 this.written += e.csize;\r
115 break;\r
116 case 0:\r
117 if (e.size != this.written - this.locoff) {\r
118 throw  new java.util.zip.ZipException ("invalid entry size (expected " + e.size + " but got " + (this.written - this.locoff) + " bytes)");\r
119 }if (e.crc != this.crc.getValue ()) {\r
120 throw  new java.util.zip.ZipException ("invalid entry crc-32 (expected 0x" + Long.toHexString (e.crc) + " but got 0x" + Long.toHexString (this.crc.getValue ()) + ")");\r
121 }break;\r
122 default:\r
123 throw  new java.util.zip.ZipException ("invalid compression method");\r
124 }\r
125 this.crc.reset ();\r
126 this.current = null;\r
127 }});\r
128 Clazz.defineMethod (c$, "write", \r
129 function (b, off, len) {\r
130 this.ensureOpen ();\r
131 if (off < 0 || len < 0 || off > b.length - len) {\r
132 throw  new IndexOutOfBoundsException ();\r
133 } else if (len == 0) {\r
134 return;\r
135 }if (this.current == null) {\r
136 throw  new java.util.zip.ZipException ("no current ZIP entry");\r
137 }var entry = this.current;\r
138 switch (entry.method) {\r
139 case 8:\r
140 Clazz.superCall (this, java.util.zip.ZipOutputStream, "write", [b, off, len]);\r
141 break;\r
142 case 0:\r
143 this.written += len;\r
144 if (this.written - this.locoff > entry.size) {\r
145 throw  new java.util.zip.ZipException ("attempt to write past end of STORED entry");\r
146 }this.out.write (this.buffer, 0, len);\r
147 break;\r
148 default:\r
149 throw  new java.util.zip.ZipException ("invalid compression method");\r
150 }\r
151 this.crc.update (b, off, len);\r
152 }, "~A,~N,~N");\r
153 Clazz.defineMethod (c$, "finish", \r
154 function () {\r
155 this.ensureOpen ();\r
156 if (this.finished) {\r
157 return;\r
158 }if (this.current != null) {\r
159 this.closeEntry ();\r
160 }var off = this.written;\r
161 for (var xentry, $xentry = this.xentries.iterator (); $xentry.hasNext () && ((xentry = $xentry.next ()) || true);) this.writeCEN (xentry);\r
162 \r
163 this.writeEND (off, this.written - off);\r
164 this.finished = true;\r
165 });\r
166 Clazz.defineMethod (c$, "close", \r
167 function () {\r
168 if (!this.$closed) {\r
169 Clazz.superCall (this, java.util.zip.ZipOutputStream, "close", []);\r
170 this.$closed = true;\r
171 }});\r
172 Clazz.defineMethod (c$, "writeLOC", \r
173  function (entry) {\r
174 var e = entry;\r
175 var flag = e.flag;\r
176 var elen = (e.extra != null) ? e.extra.length : 0;\r
177 var hasZip64 = false;\r
178 this.writeInt (67324752);\r
179 if ((flag & 8) == 8) {\r
180 this.writeShort (java.util.zip.ZipOutputStream.version (e));\r
181 this.writeShort (flag);\r
182 this.writeShort (e.method);\r
183 this.writeInt (e.time);\r
184 this.writeInt (0);\r
185 this.writeInt (0);\r
186 this.writeInt (0);\r
187 } else {\r
188 if (e.csize >= 4294967295 || e.size >= 4294967295) {\r
189 hasZip64 = true;\r
190 this.writeShort (45);\r
191 } else {\r
192 this.writeShort (java.util.zip.ZipOutputStream.version (e));\r
193 }this.writeShort (flag);\r
194 this.writeShort (e.method);\r
195 this.writeInt (e.time);\r
196 this.writeInt (e.crc);\r
197 if (hasZip64) {\r
198 this.writeInt (4294967295);\r
199 this.writeInt (4294967295);\r
200 elen += 20;\r
201 } else {\r
202 this.writeInt (e.csize);\r
203 this.writeInt (e.size);\r
204 }}var nameBytes = JU.ZStream.getBytes (e.name);\r
205 this.writeShort (nameBytes.length);\r
206 this.writeShort (elen);\r
207 this.writeBytes (nameBytes, 0, nameBytes.length);\r
208 if (hasZip64) {\r
209 this.writeShort (1);\r
210 this.writeShort (16);\r
211 this.writeLong (e.size);\r
212 this.writeLong (e.csize);\r
213 }if (e.extra != null) {\r
214 this.writeBytes (e.extra, 0, e.extra.length);\r
215 }this.locoff = this.written;\r
216 }, "java.util.zip.ZipEntry");\r
217 Clazz.defineMethod (c$, "writeEXT", \r
218  function (e) {\r
219 this.writeInt (134695760);\r
220 this.writeInt (e.crc);\r
221 if (e.csize >= 4294967295 || e.size >= 4294967295) {\r
222 this.writeLong (e.csize);\r
223 this.writeLong (e.size);\r
224 } else {\r
225 this.writeInt (e.csize);\r
226 this.writeInt (e.size);\r
227 }}, "java.util.zip.ZipEntry");\r
228 Clazz.defineMethod (c$, "writeCEN", \r
229  function (entry) {\r
230 var e = entry;\r
231 var flag = e.flag;\r
232 var version = java.util.zip.ZipOutputStream.version (e);\r
233 var csize = e.csize;\r
234 var size = e.size;\r
235 var offset = entry.offset;\r
236 var e64len = 0;\r
237 var hasZip64 = false;\r
238 if (e.csize >= 4294967295) {\r
239 csize = 4294967295;\r
240 e64len += 8;\r
241 hasZip64 = true;\r
242 }if (e.size >= 4294967295) {\r
243 size = 4294967295;\r
244 e64len += 8;\r
245 hasZip64 = true;\r
246 }if (entry.offset >= 4294967295) {\r
247 offset = 4294967295;\r
248 e64len += 8;\r
249 hasZip64 = true;\r
250 }this.writeInt (33639248);\r
251 if (hasZip64) {\r
252 this.writeShort (45);\r
253 this.writeShort (45);\r
254 } else {\r
255 this.writeShort (version);\r
256 this.writeShort (version);\r
257 }this.writeShort (flag);\r
258 this.writeShort (e.method);\r
259 this.writeInt (e.time);\r
260 this.writeInt (e.crc);\r
261 this.writeInt (csize);\r
262 this.writeInt (size);\r
263 var nameBytes = JU.ZStream.getBytes (e.name);\r
264 this.writeShort (nameBytes.length);\r
265 if (hasZip64) {\r
266 this.writeShort (e64len + 4 + (e.extra != null ? e.extra.length : 0));\r
267 } else {\r
268 this.writeShort (e.extra != null ? e.extra.length : 0);\r
269 }var commentBytes;\r
270 if (e.comment != null) {\r
271 commentBytes = JU.ZStream.getBytes (e.comment);\r
272 this.writeShort (Math.min (commentBytes.length, 0xffff));\r
273 } else {\r
274 commentBytes = null;\r
275 this.writeShort (0);\r
276 }this.writeShort (0);\r
277 this.writeShort (0);\r
278 this.writeInt (0);\r
279 this.writeInt (offset);\r
280 this.writeBytes (nameBytes, 0, nameBytes.length);\r
281 if (hasZip64) {\r
282 this.writeShort (1);\r
283 this.writeShort (e64len);\r
284 if (size == 4294967295) this.writeLong (e.size);\r
285 if (csize == 4294967295) this.writeLong (e.csize);\r
286 if (offset == 4294967295) this.writeLong (entry.offset);\r
287 }if (e.extra != null) {\r
288 this.writeBytes (e.extra, 0, e.extra.length);\r
289 }if (commentBytes != null) {\r
290 this.writeBytes (commentBytes, 0, Math.min (commentBytes.length, 0xffff));\r
291 }}, "java.util.zip.ZipEntry");\r
292 Clazz.defineMethod (c$, "writeEND", \r
293  function (off, len) {\r
294 var hasZip64 = false;\r
295 var xlen = len;\r
296 var xoff = off;\r
297 if (xlen >= 4294967295) {\r
298 xlen = 4294967295;\r
299 hasZip64 = true;\r
300 }if (xoff >= 4294967295) {\r
301 xoff = 4294967295;\r
302 hasZip64 = true;\r
303 }var count = this.xentries.size ();\r
304 if (count >= 65535) {\r
305 count = 65535;\r
306 hasZip64 = true;\r
307 }if (hasZip64) {\r
308 var off64 = this.written;\r
309 this.writeInt (101075792);\r
310 this.writeLong (44);\r
311 this.writeShort (45);\r
312 this.writeShort (45);\r
313 this.writeInt (0);\r
314 this.writeInt (0);\r
315 this.writeLong (this.xentries.size ());\r
316 this.writeLong (this.xentries.size ());\r
317 this.writeLong (len);\r
318 this.writeLong (off);\r
319 this.writeInt (117853008);\r
320 this.writeInt (0);\r
321 this.writeLong (off64);\r
322 this.writeInt (1);\r
323 }this.writeInt (101010256);\r
324 this.writeShort (0);\r
325 this.writeShort (0);\r
326 this.writeShort (count);\r
327 this.writeShort (count);\r
328 this.writeInt (xlen);\r
329 this.writeInt (xoff);\r
330 if (this.comment != null) {\r
331 this.writeShort (this.comment.length);\r
332 this.writeBytes (this.comment, 0, this.comment.length);\r
333 } else {\r
334 this.writeShort (0);\r
335 }}, "~N,~N");\r
336 Clazz.defineMethod (c$, "writeShort", \r
337  function (v) {\r
338 var out = this.out;\r
339 {\r
340 out.writeByteAsInt((v >>> 0) & 0xff);\r
341 out.writeByteAsInt((v >>> 8) & 0xff);\r
342 }this.written += 2;\r
343 }, "~N");\r
344 Clazz.defineMethod (c$, "writeInt", \r
345  function (v) {\r
346 var out = this.out;\r
347 {\r
348 out.writeByteAsInt((v >>> 0) & 0xff);\r
349 out.writeByteAsInt((v >>> 8) & 0xff);\r
350 out.writeByteAsInt((v >>> 16) & 0xff);\r
351 out.writeByteAsInt((v >>> 24) & 0xff);\r
352 }this.written += 4;\r
353 }, "~N");\r
354 Clazz.defineMethod (c$, "writeLong", \r
355  function (v) {\r
356 var out = this.out;\r
357 {\r
358 out.writeByteAsInt((v >>> 0) & 0xff);\r
359 out.writeByteAsInt((v >>> 8) & 0xff);\r
360 out.writeByteAsInt((v >>> 16) & 0xff);\r
361 out.writeByteAsInt((v >>> 24) & 0xff);\r
362 out.writeByteAsInt(0);\r
363 out.writeByteAsInt(0);\r
364 out.writeByteAsInt(0);\r
365 out.writeByteAsInt(0);\r
366 }this.written += 8;\r
367 }, "~N");\r
368 Clazz.defineMethod (c$, "writeBytes", \r
369  function (b, off, len) {\r
370 this.out.write (b, off, len);\r
371 this.written += len;\r
372 }, "~A,~N,~N");\r
373 Clazz.defineStatics (c$,\r
374 "STORED", 0,\r
375 "DEFLATED", 8);\r
376 });\r