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