Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / DeflaterOutputStream.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["java.io.FilterOutputStream"], "JU.DeflaterOutputStream", ["java.io.IOException", "java.lang.IndexOutOfBoundsException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.deflater = null;
5 this.buffer = null;
6 this.closed = false;
7 this.syncFlush = false;
8 this.buf1 = null;
9 this.mydeflater = false;
10 this.close_out = true;
11 Clazz.instantialize (this, arguments);
12 }, JU, "DeflaterOutputStream", java.io.FilterOutputStream);
13 Clazz.prepareFields (c$, function () {
14 this.buf1 =  Clazz.newByteArray (1, 0);
15 });
16 Clazz.defineMethod (c$, "jzSetDOS", 
17 function (out, deflater, size, close_out) {
18 this.jzSetFOS (out);
19 if (size == 0) size = 512;
20 this.deflater = deflater;
21 this.buffer =  Clazz.newByteArray (size, 0);
22 this.close_out = close_out;
23 }, "java.io.OutputStream,JU.Deflater,~N,~B");
24 Clazz.overrideMethod (c$, "writeByteAsInt", 
25 function (b) {
26 this.buf1[0] = (b & 0xff);
27 this.write (this.buf1, 0, 1);
28 }, "~N");
29 Clazz.defineMethod (c$, "write", 
30 function (b, off, len) {
31 if (this.deflater.finished ()) throw  new java.io.IOException ("finished");
32 if ( new Boolean ( new Boolean (off < 0 | len < 0).valueOf () | off + len > b.length).valueOf ()) throw  new IndexOutOfBoundsException ();
33 if (len == 0) return;
34 var flush = this.syncFlush ? 2 : 0;
35 this.deflater.setInput (b, off, len, true);
36 while (this.deflater.avail_in > 0) {
37 var err = this.deflate (flush);
38 if (err == 1) break;
39 }
40 }, "~A,~N,~N");
41 Clazz.defineMethod (c$, "finish", 
42 function () {
43 while (!this.deflater.finished ()) {
44 this.deflate (4);
45 }
46 });
47 Clazz.overrideMethod (c$, "close", 
48 function () {
49 if (!this.closed) {
50 this.finish ();
51 if (this.mydeflater) {
52 this.deflater.end ();
53 }if (this.close_out) this.out.close ();
54 this.closed = true;
55 }});
56 Clazz.defineMethod (c$, "deflate", 
57 function (flush) {
58 this.deflater.setOutput (this.buffer, 0, this.buffer.length);
59 var err = this.deflater.deflate (flush);
60 switch (err) {
61 case 0:
62 case 1:
63 break;
64 case -5:
65 if (this.deflater.avail_in <= 0 && flush != 4) {
66 break;
67 }default:
68 throw  new java.io.IOException ("failed to deflate");
69 }
70 var len = this.deflater.next_out_index;
71 if (len > 0) {
72 this.out.write (this.buffer, 0, len);
73 }return err;
74 }, "~N");
75 Clazz.overrideMethod (c$, "flush", 
76 function () {
77 if (this.syncFlush && !this.deflater.finished ()) {
78 while (true) {
79 var err = this.deflate (2);
80 if (this.deflater.next_out_index < this.buffer.length) break;
81 if (err == 1) break;
82 }
83 }this.out.flush ();
84 });
85 Clazz.defineMethod (c$, "getTotalIn", 
86 function () {
87 return this.deflater.getTotalIn ();
88 });
89 Clazz.defineMethod (c$, "getTotalOut", 
90 function () {
91 return this.deflater.getTotalOut ();
92 });
93 Clazz.defineMethod (c$, "setSyncFlush", 
94 function (syncFlush) {
95 this.syncFlush = syncFlush;
96 }, "~B");
97 Clazz.defineMethod (c$, "getSyncFlush", 
98 function () {
99 return this.syncFlush;
100 });
101 Clazz.defineMethod (c$, "getDeflater", 
102 function () {
103 return this.deflater;
104 });
105 Clazz.defineStatics (c$,
106 "DEFAULT_BUFSIZE", 512);
107 });