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 import com.caucho.vfs.ReadWritePair; 35 import com.caucho.vfs.WriteStream; 36 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 41 public class OutputStreamEcmaWrap { 42 public static void writeByte(OutputStream os, int ch) 43 throws Throwable 44 { 45 os.write(ch); 46 } 47 48 public static void write(OutputStream os, Call call, int length) 49 throws Throwable 50 { 51 for (int i = 0; i < length; i++) { 52 String string = call.getArgString(i, length); 53 54 if (string == null) 55 string = "null"; 56 57 byte []bytes = string.getBytes(); 58 59 os.write(bytes, 0, bytes.length); 60 } 61 } 62 63 public static void writeBytes(OutputStream os, byte []buffer, int offset, int length) 64 throws Throwable 65 { 66 os.write(buffer, offset, length); 67 } 68 69 public static void writeln(OutputStream os, Call call, int length) 70 throws Throwable 71 { 72 for (int i = 0; i < length; i++) { 73 String string = call.getArgString(i, length); 74 75 if (string == null) 76 string = "null"; 77 78 byte []bytes = string.getBytes(); 79 80 os.write(bytes, 0, bytes.length); 81 } 82 83 os.write('\n'); 84 } 85 86 public static void printf(OutputStream os, Call eval, int length) 87 throws Throwable 88 { 89 if (length == 0) 90 return; 91 92 String result = eval.printf(length); 93 byte []bytes = result.getBytes(); 94 95 os.write(bytes, 0, bytes.length); 96 } 97 98 public static void writeFile(OutputStream os, Path path) 99 throws IOException 100 { 101 ReadStream stream = path.openRead(); 102 103 try { 104 stream.writeToStream(os); 105 } finally { 106 stream.close(); 107 } 108 } 109 110 public static void writeStream(OutputStream os, Call call, int length) 111 throws Throwable 112 { 113 if (length < 1) 114 return; 115 116 char []buf = new char[256]; 117 int len; 118 119 Object obj = call.getArgObject(0, length); 120 if (obj instanceof ReadStream) { 121 ReadStream is = (ReadStream) obj; 122 is.writeToStream(os); 123 } 124 else if (obj instanceof ReadWritePair) { 125 ((ReadWritePair) obj).getReadStream().writeToStream(os); 126 } 127 else if (obj instanceof InputStream ) { 128 if (os instanceof WriteStream) { 129 ((WriteStream) os).writeStream((InputStream ) obj); 130 } 131 else { 132 int ch; 133 InputStream is = (InputStream ) obj; 134 while ((ch = is.read()) >= 0) 135 os.write(ch); 136 } 137 } 138 else 139 throw new IllegalArgumentException ("expected stream at " + 140 obj.getClass().getName()); 141 } 142 } 143 144 | Popular Tags |