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;
7 this.isLocalFile = false;
9 this.isCanceled = false;
14 this.$isBase64 = false;
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);
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));
32 }, "javajs.api.BytePoster,~S,~B,java.io.OutputStream");
33 Clazz.defineMethod (c$, "setBytes",
38 Clazz.defineMethod (c$, "getFileName",
42 Clazz.defineMethod (c$, "getName",
44 return (this.fileName == null ? null : this.fileName.substring (this.fileName.lastIndexOf ("/") + 1));
46 Clazz.defineMethod (c$, "getByteCount",
48 return this.byteCount;
50 Clazz.defineMethod (c$, "setType",
54 Clazz.defineMethod (c$, "getType",
58 Clazz.defineMethod (c$, "append",
61 if (this.bw != null) {
63 } else if (this.os == null) {
64 if (this.sb == null) this.sb = new javajs.util.SB ();
67 var b = s.getBytes ();
68 this.os.write (b, 0, b.length);
69 this.byteCount += b.length;
72 if (Clazz.exceptionOf (e, java.io.IOException)) {
77 this.byteCount += s.length;
80 Clazz.defineMethod (c$, "reset",
85 Clazz.defineMethod (c$, "initOS",
87 if (this.sb != null) {
88 var s = this.sb.toString ();
95 }if (this.os == null) this.os = new java.io.ByteArrayOutputStream ();
96 if (this.bw != null) {
98 this.bw = new java.io.BufferedWriter ( new java.io.OutputStreamWriter (this.os));
100 if (Clazz.exceptionOf (e, Exception)) {
101 System.out.println (e.toString ());
107 }, $fz.isPrivate = true, $fz));
108 Clazz.overrideMethod (c$, "write",
109 function (buf, i, len) {
110 if (this.os == null) this.initOS ();
112 this.os.write (buf, i, len);
114 if (Clazz.exceptionOf (e, java.io.IOException)) {
119 this.byteCount += len;
121 Clazz.defineMethod (c$, "writeByteAsInt",
123 if (this.os == null) this.initOS ();
125 this.os.writeByteAsInt(b);
128 Clazz.defineMethod (c$, "cancel",
130 this.isCanceled = true;
131 this.closeChannel ();
133 Clazz.defineMethod (c$, "closeChannel",
135 if (this.closed) return null;
137 if (this.bw != null) {
140 } else if (this.os != null) {
143 }if (this.os0 != null && this.isCanceled) {
147 if (Clazz.exceptionOf (e, Exception)) {
152 if (this.isCanceled) {
155 }if (this.fileName == null) {
156 if (this.$isBase64) {
157 var s = this.getBase64 ();
158 if (this.os0 != null) {
161 }this.sb = new javajs.util.SB ();
163 this.$isBase64 = false;
164 return this.closeChannel ();
165 }return (this.sb == null ? null : this.sb.toString ());
168 var _function = null;
170 jmol = Jmol; _function = (typeof this.fileName == "function" ?
171 this.fileName : 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);
178 Clazz.defineMethod (c$, "isBase64",
180 return this.$isBase64;
182 Clazz.defineMethod (c$, "getBase64",
184 return javajs.util.Base64.getBase64 (this.toByteArray ()).toString ();
186 Clazz.defineMethod (c$, "toByteArray",
188 return (this.bytes != null ? this.bytes : Clazz.instanceOf (this.os, java.io.ByteArrayOutputStream) ? (this.os).toByteArray () : null);
190 Clazz.defineMethod (c$, "close",
192 this.closeChannel ();
194 Clazz.overrideMethod (c$, "toString",
196 if (this.bw != null) try {
199 if (Clazz.exceptionOf (e, java.io.IOException)) {
204 if (this.sb != null) return this.closeChannel ();
205 return this.byteCount + " bytes";
207 Clazz.defineMethod (c$, "postByteArray",
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);
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);
224 c$.urlTypeIndex = Clazz.defineMethod (c$, "urlTypeIndex",
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])) {
233 Clazz.defineStatics (c$,
234 "urlPrefixes", Clazz.newArray (-1, ["http:", "https:", "sftp:", "ftp:", "file:"]),