JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / util / OC.js
1 Clazz.declarePackage ("javajs.util");\r
2 Clazz.load (["java.io.OutputStream"], "javajs.util.OC", ["java.io.BufferedWriter", "$.ByteArrayOutputStream", "$.OutputStreamWriter", "javajs.util.Base64", "$.SB"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.bytePoster = null;\r
5 this.fileName = null;\r
6 this.bw = null;\r
7 this.isLocalFile = false;\r
8 this.byteCount = 0;\r
9 this.isCanceled = false;\r
10 this.closed = false;\r
11 this.os = null;\r
12 this.sb = null;\r
13 this.type = null;\r
14 this.$isBase64 = false;\r
15 this.os0 = null;\r
16 this.bytes = null;\r
17 Clazz.instantialize (this, arguments);\r
18 }, javajs.util, "OC", java.io.OutputStream);\r
19 Clazz.defineMethod (c$, "setParams", \r
20 function (bytePoster, fileName, asWriter, os) {\r
21 this.bytePoster = bytePoster;\r
22 this.fileName = fileName;\r
23 this.$isBase64 = ";base64,".equals (fileName);\r
24 if (this.$isBase64) {\r
25 fileName = null;\r
26 this.os0 = os;\r
27 os = null;\r
28 }this.os = os;\r
29 this.isLocalFile = (fileName != null && !javajs.util.OC.isRemote (fileName));\r
30 if (asWriter && !this.$isBase64 && os != null) this.bw =  new java.io.BufferedWriter ( new java.io.OutputStreamWriter (os));\r
31 return this;\r
32 }, "javajs.api.BytePoster,~S,~B,java.io.OutputStream");\r
33 Clazz.defineMethod (c$, "setBytes", \r
34 function (b) {\r
35 this.bytes = b;\r
36 return this;\r
37 }, "~A");\r
38 Clazz.defineMethod (c$, "getFileName", \r
39 function () {\r
40 return this.fileName;\r
41 });\r
42 Clazz.defineMethod (c$, "getName", \r
43 function () {\r
44 return (this.fileName == null ? null : this.fileName.substring (this.fileName.lastIndexOf ("/") + 1));\r
45 });\r
46 Clazz.defineMethod (c$, "getByteCount", \r
47 function () {\r
48 return this.byteCount;\r
49 });\r
50 Clazz.defineMethod (c$, "setType", \r
51 function (type) {\r
52 this.type = type;\r
53 }, "~S");\r
54 Clazz.defineMethod (c$, "getType", \r
55 function () {\r
56 return this.type;\r
57 });\r
58 Clazz.defineMethod (c$, "append", \r
59 function (s) {\r
60 try {\r
61 if (this.bw != null) {\r
62 this.bw.write (s);\r
63 } else if (this.os == null) {\r
64 if (this.sb == null) this.sb =  new javajs.util.SB ();\r
65 this.sb.append (s);\r
66 } else {\r
67 var b = s.getBytes ();\r
68 this.os.write (b, 0, b.length);\r
69 this.byteCount += b.length;\r
70 return this;\r
71 }} catch (e) {\r
72 if (Clazz.exceptionOf (e, java.io.IOException)) {\r
73 } else {\r
74 throw e;\r
75 }\r
76 }\r
77 this.byteCount += s.length;\r
78 return this;\r
79 }, "~S");\r
80 Clazz.defineMethod (c$, "reset", \r
81 function () {\r
82 this.sb = null;\r
83 this.initOS ();\r
84 });\r
85 Clazz.defineMethod (c$, "initOS", \r
86 ($fz = function () {\r
87 if (this.sb != null) {\r
88 var s = this.sb.toString ();\r
89 this.reset ();\r
90 this.append (s);\r
91 return;\r
92 }try {\r
93 {\r
94 this.os = null;\r
95 }if (this.os == null) this.os =  new java.io.ByteArrayOutputStream ();\r
96 if (this.bw != null) {\r
97 this.bw.close ();\r
98 this.bw =  new java.io.BufferedWriter ( new java.io.OutputStreamWriter (this.os));\r
99 }} catch (e) {\r
100 if (Clazz.exceptionOf (e, Exception)) {\r
101 System.out.println (e.toString ());\r
102 } else {\r
103 throw e;\r
104 }\r
105 }\r
106 this.byteCount = 0;\r
107 }, $fz.isPrivate = true, $fz));\r
108 Clazz.overrideMethod (c$, "write", \r
109 function (buf, i, len) {\r
110 if (this.os == null) this.initOS ();\r
111 try {\r
112 this.os.write (buf, i, len);\r
113 } catch (e) {\r
114 if (Clazz.exceptionOf (e, java.io.IOException)) {\r
115 } else {\r
116 throw e;\r
117 }\r
118 }\r
119 this.byteCount += len;\r
120 }, "~A,~N,~N");\r
121 Clazz.defineMethod (c$, "writeByteAsInt", \r
122 function (b) {\r
123 if (this.os == null) this.initOS ();\r
124 {\r
125 this.os.writeByteAsInt(b);\r
126 }this.byteCount++;\r
127 }, "~N");\r
128 Clazz.defineMethod (c$, "cancel", \r
129 function () {\r
130 this.isCanceled = true;\r
131 this.closeChannel ();\r
132 });\r
133 Clazz.defineMethod (c$, "closeChannel", \r
134 function () {\r
135 if (this.closed) return null;\r
136 try {\r
137 if (this.bw != null) {\r
138 this.bw.flush ();\r
139 this.bw.close ();\r
140 } else if (this.os != null) {\r
141 this.os.flush ();\r
142 this.os.close ();\r
143 }if (this.os0 != null && this.isCanceled) {\r
144 this.os0.flush ();\r
145 this.os0.close ();\r
146 }} catch (e) {\r
147 if (Clazz.exceptionOf (e, Exception)) {\r
148 } else {\r
149 throw e;\r
150 }\r
151 }\r
152 if (this.isCanceled) {\r
153 this.closed = true;\r
154 return null;\r
155 }if (this.fileName == null) {\r
156 if (this.$isBase64) {\r
157 var s = this.getBase64 ();\r
158 if (this.os0 != null) {\r
159 this.os = this.os0;\r
160 this.append (s);\r
161 }this.sb =  new javajs.util.SB ();\r
162 this.sb.append (s);\r
163 this.$isBase64 = false;\r
164 return this.closeChannel ();\r
165 }return (this.sb == null ? null : this.sb.toString ());\r
166 }this.closed = true;\r
167 var jmol = null;\r
168 var _function = null;\r
169 {\r
170 jmol = Jmol; _function = (typeof this.fileName == "function" ?\r
171 this.fileName : null);\r
172 }if (jmol != null) {\r
173 var data = (this.sb == null ? this.toByteArray () : this.sb.toString ());\r
174 if (_function == null) jmol._doAjax (this.fileName, null, data);\r
175  else jmol._apply (this.fileName, data);\r
176 }return null;\r
177 });\r
178 Clazz.defineMethod (c$, "isBase64", \r
179 function () {\r
180 return this.$isBase64;\r
181 });\r
182 Clazz.defineMethod (c$, "getBase64", \r
183 function () {\r
184 return javajs.util.Base64.getBase64 (this.toByteArray ()).toString ();\r
185 });\r
186 Clazz.defineMethod (c$, "toByteArray", \r
187 function () {\r
188 return (this.bytes != null ? this.bytes : Clazz.instanceOf (this.os, java.io.ByteArrayOutputStream) ? (this.os).toByteArray () : null);\r
189 });\r
190 Clazz.defineMethod (c$, "close", \r
191 function () {\r
192 this.closeChannel ();\r
193 });\r
194 Clazz.overrideMethod (c$, "toString", \r
195 function () {\r
196 if (this.bw != null) try {\r
197 this.bw.flush ();\r
198 } catch (e) {\r
199 if (Clazz.exceptionOf (e, java.io.IOException)) {\r
200 } else {\r
201 throw e;\r
202 }\r
203 }\r
204 if (this.sb != null) return this.closeChannel ();\r
205 return this.byteCount + " bytes";\r
206 });\r
207 Clazz.defineMethod (c$, "postByteArray", \r
208 ($fz = function () {\r
209 var bytes = (this.sb == null ? this.toByteArray () : this.sb.toString ().getBytes ());\r
210 return this.bytePoster.postByteArray (this.fileName, bytes);\r
211 }, $fz.isPrivate = true, $fz));\r
212 c$.isRemote = Clazz.defineMethod (c$, "isRemote", \r
213 function (fileName) {\r
214 if (fileName == null) return false;\r
215 var itype = javajs.util.OC.urlTypeIndex (fileName);\r
216 return (itype >= 0 && itype != 4);\r
217 }, "~S");\r
218 c$.isLocal = Clazz.defineMethod (c$, "isLocal", \r
219 function (fileName) {\r
220 if (fileName == null) return false;\r
221 var itype = javajs.util.OC.urlTypeIndex (fileName);\r
222 return (itype < 0 || itype == 4);\r
223 }, "~S");\r
224 c$.urlTypeIndex = Clazz.defineMethod (c$, "urlTypeIndex", \r
225 function (name) {\r
226 if (name == null) return -2;\r
227 for (var i = 0; i < javajs.util.OC.urlPrefixes.length; ++i) {\r
228 if (name.startsWith (javajs.util.OC.urlPrefixes[i])) {\r
229 return i;\r
230 }}\r
231 return -1;\r
232 }, "~S");\r
233 Clazz.defineStatics (c$,\r
234 "urlPrefixes",  Clazz.newArray (-1, ["http:", "https:", "sftp:", "ftp:", "file:"]),\r
235 "URL_LOCAL", 4);\r
236 });\r