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