1 Clazz.declarePackage ("JU");
2 Clazz.load (["java.io.OutputStream"], "JU.OC", ["java.io.BufferedWriter", "$.ByteArrayOutputStream", "$.OutputStreamWriter", "JU.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 }, JU, "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 && !JU.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 JU.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 ());
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.overrideMethod (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 JU.SB ();
163 this.$isBase64 = false;
164 return this.closeChannel ();
165 }return (this.sb == null ? null : this.sb.toString ());
168 var data = (this.sb == null ? this.toByteArray() :
169 this.sb.toString()); if (typeof this.fileName == "function") {
170 this.fileName(data); } else { Jmol._doAjax(this.fileName,
174 Clazz.defineMethod (c$, "isBase64",
176 return this.$isBase64;
178 Clazz.defineMethod (c$, "getBase64",
180 return JU.Base64.getBase64 (this.toByteArray ()).toString ();
182 Clazz.defineMethod (c$, "toByteArray",
184 return (this.bytes != null ? this.bytes : Clazz.instanceOf (this.os, java.io.ByteArrayOutputStream) ? (this.os).toByteArray () : null);
186 Clazz.defineMethod (c$, "close",
188 this.closeChannel ();
190 Clazz.overrideMethod (c$, "toString",
192 if (this.bw != null) try {
195 if (Clazz.exceptionOf (e, java.io.IOException)) {
200 if (this.sb != null) return this.closeChannel ();
201 return this.byteCount + " bytes";
203 Clazz.defineMethod (c$, "postByteArray",
205 var bytes = (this.sb == null ? this.toByteArray () : this.sb.toString ().getBytes ());
206 return this.bytePoster.postByteArray (this.fileName, bytes);
208 c$.isRemote = Clazz.defineMethod (c$, "isRemote",
209 function (fileName) {
210 if (fileName == null) return false;
211 var itype = JU.OC.urlTypeIndex (fileName);
212 return (itype >= 0 && itype != 4);
214 c$.isLocal = Clazz.defineMethod (c$, "isLocal",
215 function (fileName) {
216 if (fileName == null) return false;
217 var itype = JU.OC.urlTypeIndex (fileName);
218 return (itype < 0 || itype == 4);
220 c$.urlTypeIndex = Clazz.defineMethod (c$, "urlTypeIndex",
222 if (name == null) return -2;
223 for (var i = 0; i < JU.OC.urlPrefixes.length; ++i) {
224 if (name.startsWith (JU.OC.urlPrefixes[i])) {
229 Clazz.defineStatics (c$,
230 "urlPrefixes", Clazz.newArray (-1, ["http:", "https:", "sftp:", "ftp:", "file:"]),