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