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