1 28 29 package com.caucho.eswrap.com.caucho.vfs; 30 31 import com.caucho.es.Call; 32 import com.caucho.vfs.Path; 33 import com.caucho.vfs.ReadStream; 34 import com.caucho.vfs.ReadWritePair; 35 import com.caucho.vfs.WriteStream; 36 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 40 public class WriteStreamEcmaWrap { 41 public static void writeByte(WriteStream os, int ch) 42 throws Throwable 43 { 44 os.write(ch); 45 } 46 47 public static void write(WriteStream os, Call call, int length) 48 throws Throwable 49 { 50 for (int i = 0; i < length; i++) { 51 String string = call.getArgString(i, length); 52 53 if (string == null) 54 string = "null"; 55 56 os.print(string); 57 } 58 } 59 60 public static void writeln(WriteStream os, Call call, int length) 61 throws Throwable 62 { 63 for (int i = 0; i < length; i++) { 64 String string = call.getArgString(i, length); 65 66 if (string == null) 67 string = "null"; 68 69 if (i + 1 == length) 70 os.println(string); 71 else 72 os.print(string); 73 } 74 75 if (length == 0) 76 os.println(); 77 } 78 79 public static void printf(WriteStream os, Call eval, int length) 80 throws Throwable 81 { 82 if (length == 0) 83 return; 84 85 String result = eval.printf(length); 86 87 os.print(result); 88 } 89 90 public static void writeFile(WriteStream os, Path path) 91 throws IOException 92 { 93 ReadStream stream = path.openRead(); 94 95 try { 96 os.writeStream(stream); 97 } finally { 98 stream.close(); 99 } 100 } 101 102 public static void writeStream(WriteStream os, Call call, int length) 103 throws Throwable 104 { 105 if (length < 1) 106 return; 107 108 char []buf = new char[256]; 109 int len; 110 111 Object obj = call.getArgObject(0, length); 112 if (obj instanceof ReadStream) { 113 ReadStream is = (ReadStream) obj; 114 os.writeStream(is); 115 } 116 else if (obj instanceof ReadWritePair) { 117 os.writeStream(((ReadWritePair) obj).getReadStream()); 118 } 119 else if (obj instanceof InputStream ) { 120 os.writeStream((InputStream ) obj); 121 } 122 else 123 throw new IllegalArgumentException ("expected stream at " + 124 obj.getClass().getName()); 125 } 126 } 127 128 | Popular Tags |