1 Clazz.declarePackage ("JU");
\r
2 Clazz.load (["java.io.OutputStream"], "JU.OC", ["java.io.BufferedWriter", "$.ByteArrayOutputStream", "$.OutputStreamWriter", "JU.Base64", "$.SB"], function () {
\r
3 c$ = Clazz.decorateAsClass (function () {
\r
4 this.bytePoster = null;
\r
5 this.fileName = null;
\r
7 this.isLocalFile = false;
\r
9 this.isCanceled = false;
\r
10 this.closed = false;
\r
14 this.$isBase64 = false;
\r
17 Clazz.instantialize (this, arguments);
\r
18 }, JU, "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
29 this.isLocalFile = (fileName != null && !JU.OC.isRemote (fileName));
\r
30 if (asWriter && !this.$isBase64 && os != null) this.bw = new java.io.BufferedWriter ( new java.io.OutputStreamWriter (os));
\r
32 }, "javajs.api.BytePoster,~S,~B,java.io.OutputStream");
\r
33 Clazz.defineMethod (c$, "setBytes",
\r
38 Clazz.defineMethod (c$, "getFileName",
\r
40 return this.fileName;
\r
42 Clazz.defineMethod (c$, "getName",
\r
44 return (this.fileName == null ? null : this.fileName.substring (this.fileName.lastIndexOf ("/") + 1));
\r
46 Clazz.defineMethod (c$, "getByteCount",
\r
48 return this.byteCount;
\r
50 Clazz.defineMethod (c$, "setType",
\r
54 Clazz.defineMethod (c$, "getType",
\r
58 Clazz.defineMethod (c$, "append",
\r
61 if (this.bw != null) {
\r
63 } else if (this.os == null) {
\r
64 if (this.sb == null) this.sb = new JU.SB ();
\r
67 var b = s.getBytes ();
\r
68 this.os.write (b, 0, b.length);
\r
69 this.byteCount += b.length;
\r
72 if (Clazz.exceptionOf (e, java.io.IOException)) {
\r
77 this.byteCount += s.length;
\r
80 Clazz.defineMethod (c$, "reset",
\r
85 Clazz.defineMethod (c$, "initOS",
\r
87 if (this.sb != null) {
\r
88 var s = this.sb.toString ();
\r
95 }if (this.os == null) this.os = new java.io.ByteArrayOutputStream ();
\r
96 if (this.bw != null) {
\r
98 this.bw = new java.io.BufferedWriter ( new java.io.OutputStreamWriter (this.os));
\r
100 if (Clazz.exceptionOf (e, Exception)) {
\r
101 System.out.println (e.toString ());
\r
106 this.byteCount = 0;
\r
108 Clazz.overrideMethod (c$, "write",
\r
109 function (buf, i, len) {
\r
110 if (this.os == null) this.initOS ();
\r
112 this.os.write (buf, i, len);
\r
114 if (Clazz.exceptionOf (e, java.io.IOException)) {
\r
119 this.byteCount += len;
\r
121 Clazz.overrideMethod (c$, "writeByteAsInt",
\r
123 if (this.os == null) this.initOS ();
\r
125 this.os.writeByteAsInt(b);
\r
128 Clazz.defineMethod (c$, "cancel",
\r
130 this.isCanceled = true;
\r
131 this.closeChannel ();
\r
133 Clazz.defineMethod (c$, "closeChannel",
\r
135 if (this.closed) return null;
\r
137 if (this.bw != null) {
\r
140 } else if (this.os != null) {
\r
143 }if (this.os0 != null && this.isCanceled) {
\r
147 if (Clazz.exceptionOf (e, Exception)) {
\r
152 if (this.isCanceled) {
\r
153 this.closed = true;
\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
161 }this.sb = new JU.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
168 var data = (this.sb == null ? this.toByteArray() :
\r
169 this.sb.toString()); if (typeof this.fileName == "function") {
\r
170 this.fileName(data); } else { Jmol._doAjax(this.fileName,
\r
174 Clazz.defineMethod (c$, "isBase64",
\r
176 return this.$isBase64;
\r
178 Clazz.defineMethod (c$, "getBase64",
\r
180 return JU.Base64.getBase64 (this.toByteArray ()).toString ();
\r
182 Clazz.defineMethod (c$, "toByteArray",
\r
184 return (this.bytes != null ? this.bytes : Clazz.instanceOf (this.os, java.io.ByteArrayOutputStream) ? (this.os).toByteArray () : null);
\r
186 Clazz.defineMethod (c$, "close",
\r
188 this.closeChannel ();
\r
190 Clazz.overrideMethod (c$, "toString",
\r
192 if (this.bw != null) try {
\r
195 if (Clazz.exceptionOf (e, java.io.IOException)) {
\r
200 if (this.sb != null) return this.closeChannel ();
\r
201 return this.byteCount + " bytes";
\r
203 Clazz.defineMethod (c$, "postByteArray",
\r
205 var bytes = (this.sb == null ? this.toByteArray () : this.sb.toString ().getBytes ());
\r
206 return this.bytePoster.postByteArray (this.fileName, bytes);
\r
208 c$.isRemote = Clazz.defineMethod (c$, "isRemote",
\r
209 function (fileName) {
\r
210 if (fileName == null) return false;
\r
211 var itype = JU.OC.urlTypeIndex (fileName);
\r
212 return (itype >= 0 && itype != 4);
\r
214 c$.isLocal = Clazz.defineMethod (c$, "isLocal",
\r
215 function (fileName) {
\r
216 if (fileName == null) return false;
\r
217 var itype = JU.OC.urlTypeIndex (fileName);
\r
218 return (itype < 0 || itype == 4);
\r
220 c$.urlTypeIndex = Clazz.defineMethod (c$, "urlTypeIndex",
\r
222 if (name == null) return -2;
\r
223 for (var i = 0; i < JU.OC.urlPrefixes.length; ++i) {
\r
224 if (name.startsWith (JU.OC.urlPrefixes[i])) {
\r
229 Clazz.defineStatics (c$,
\r
230 "urlPrefixes", Clazz.newArray (-1, ["http:", "https:", "sftp:", "ftp:", "file:"]),
\r