JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / JU / InflaterInputStream.js
1 Clazz.declarePackage ("JU");\r
2 Clazz.load (["java.io.FilterInputStream"], "JU.InflaterInputStream", ["java.io.EOFException", "$.IOException", "java.lang.IllegalArgumentException", "$.IndexOutOfBoundsException", "$.NullPointerException"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.inflater = null;\r
5 this.buf = null;\r
6 this.len = 0;\r
7 this.closed = false;\r
8 this.eof = false;\r
9 this.close_in = true;\r
10 this.myinflater = false;\r
11 this.byte1 = null;\r
12 this.b = null;\r
13 Clazz.instantialize (this, arguments);\r
14 }, JU, "InflaterInputStream", java.io.FilterInputStream);\r
15 Clazz.prepareFields (c$, function () {\r
16 this.byte1 =  Clazz.newByteArray (1, 0);\r
17 this.b =  Clazz.newByteArray (512, 0);\r
18 });\r
19 Clazz.makeConstructor (c$, \r
20 function ($in, inflater, size, close_in) {\r
21 Clazz.superConstructor (this, JU.InflaterInputStream, [$in]);\r
22 this.inflater = inflater;\r
23 this.buf =  Clazz.newByteArray (size, 0);\r
24 this.close_in = close_in;\r
25 }, "java.io.InputStream,JU.Inflater,~N,~B");\r
26 Clazz.overrideMethod (c$, "readByteAsInt", \r
27 function () {\r
28 if (this.closed) {\r
29 throw  new java.io.IOException ("Stream closed");\r
30 }return this.read (this.byte1, 0, 1) == -1 ? -1 : this.byte1[0] & 0xff;\r
31 });\r
32 Clazz.overrideMethod (c$, "read", \r
33 function (b, off, len) {\r
34 return this.readInf (b, off, len);\r
35 }, "~A,~N,~N");\r
36 Clazz.defineMethod (c$, "readInf", \r
37 function (b, off, len) {\r
38 if (this.closed) {\r
39 throw  new java.io.IOException ("Stream closed");\r
40 }if (b == null) {\r
41 throw  new NullPointerException ();\r
42 } else if (off < 0 || len < 0 || len > b.length - off) {\r
43 throw  new IndexOutOfBoundsException ();\r
44 } else if (len == 0) {\r
45 return 0;\r
46 } else if (this.eof) {\r
47 return -1;\r
48 }var n = 0;\r
49 this.inflater.setOutput (b, off, len);\r
50 while (!this.eof) {\r
51 if (this.inflater.avail_in == 0) this.fill ();\r
52 var err = this.inflater.inflate (0);\r
53 n += this.inflater.next_out_index - off;\r
54 off = this.inflater.next_out_index;\r
55 switch (err) {\r
56 case -3:\r
57 throw  new java.io.IOException (this.inflater.msg);\r
58 case 1:\r
59 case 2:\r
60 this.eof = true;\r
61 if (err == 2) return -1;\r
62 break;\r
63 default:\r
64 }\r
65 if (this.inflater.avail_out == 0) break;\r
66 }\r
67 return n;\r
68 }, "~A,~N,~N");\r
69 Clazz.overrideMethod (c$, "available", \r
70 function () {\r
71 if (this.closed) {\r
72 throw  new java.io.IOException ("Stream closed");\r
73 }return (this.eof ? 0 : 1);\r
74 });\r
75 Clazz.overrideMethod (c$, "skip", \r
76 function (n) {\r
77 if (n < 0) {\r
78 throw  new IllegalArgumentException ("negative skip length");\r
79 }if (this.closed) {\r
80 throw  new java.io.IOException ("Stream closed");\r
81 }var max = Math.min (n, 2147483647);\r
82 var total = 0;\r
83 while (total < max) {\r
84 var len = max - total;\r
85 if (len > this.b.length) {\r
86 len = this.b.length;\r
87 }len = this.read (this.b, 0, len);\r
88 if (len == -1) {\r
89 this.eof = true;\r
90 break;\r
91 }total += len;\r
92 }\r
93 return total;\r
94 }, "~N");\r
95 Clazz.overrideMethod (c$, "close", \r
96 function () {\r
97 if (!this.closed) {\r
98 if (this.myinflater) this.inflater.end ();\r
99 if (this.close_in) this.$in.close ();\r
100 this.closed = true;\r
101 }});\r
102 Clazz.defineMethod (c$, "fill", \r
103 function () {\r
104 if (this.closed) {\r
105 throw  new java.io.IOException ("Stream closed");\r
106 }this.len = this.$in.read (this.buf, 0, this.buf.length);\r
107 if (this.len == -1) {\r
108 if (this.inflater.istate.wrap == 0 && !this.inflater.finished ()) {\r
109 this.buf[0] = 0;\r
110 this.len = 1;\r
111 } else if (this.inflater.istate.was != -1) {\r
112 throw  new java.io.IOException ("footer is not found");\r
113 } else {\r
114 throw  new java.io.EOFException ("Unexpected end of ZLIB input stream");\r
115 }}this.inflater.setInput (this.buf, 0, this.len, true);\r
116 });\r
117 Clazz.overrideMethod (c$, "markSupported", \r
118 function () {\r
119 return false;\r
120 });\r
121 Clazz.overrideMethod (c$, "mark", \r
122 function (readlimit) {\r
123 }, "~N");\r
124 Clazz.overrideMethod (c$, "reset", \r
125 function () {\r
126 throw  new java.io.IOException ("mark/reset not supported");\r
127 });\r
128 Clazz.defineMethod (c$, "getTotalIn", \r
129 function () {\r
130 return this.inflater.getTotalIn ();\r
131 });\r
132 Clazz.defineMethod (c$, "getTotalOut", \r
133 function () {\r
134 return this.inflater.getTotalOut ();\r
135 });\r
136 Clazz.defineMethod (c$, "getAvailIn", \r
137 function () {\r
138 if (this.inflater.avail_in <= 0) return null;\r
139 var tmp =  Clazz.newByteArray (this.inflater.avail_in, 0);\r
140 System.arraycopy (this.inflater.next_in, this.inflater.next_in_index, tmp, 0, this.inflater.avail_in);\r
141 return tmp;\r
142 });\r
143 Clazz.defineMethod (c$, "readHeader", \r
144 function () {\r
145 var empty = "".getBytes ();\r
146 this.inflater.setInput (empty, 0, 0, false);\r
147 this.inflater.setOutput (empty, 0, 0);\r
148 var err = this.inflater.inflate (0);\r
149 if (!this.inflater.istate.inParsingHeader ()) {\r
150 return;\r
151 }var b1 =  Clazz.newByteArray (1, 0);\r
152 do {\r
153 var i = this.$in.read (b1, 0, 1);\r
154 if (i <= 0) throw  new java.io.IOException ("no input");\r
155 this.inflater.setInput (b1, 0, b1.length, false);\r
156 err = this.inflater.inflate (0);\r
157 if (err != 0) throw  new java.io.IOException (this.inflater.msg);\r
158 } while (this.inflater.istate.inParsingHeader ());\r
159 });\r
160 Clazz.defineMethod (c$, "getInflater", \r
161 function () {\r
162 return this.inflater;\r
163 });\r
164 Clazz.defineStatics (c$,\r
165 "DEFAULT_BUFSIZE", 512);\r
166 });\r