1 28 29 package com.caucho.eswrap.com.caucho.vfs; 30 31 import com.caucho.es.Call; 32 import com.caucho.util.CharBuffer; 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.util.Iterator ; 40 41 public class ReadWritePairEcmaWrap { 42 public static void writeByte(ReadWritePair s, int ch) 43 throws Throwable 44 { 45 s.getWriteStream().write(ch); 46 } 47 48 public static void write(ReadWritePair s, Call call, int length) 49 throws Throwable 50 { 51 WriteStream os = s.getWriteStream(); 52 for (int i = 0; i < length; i++) { 53 String string = call.getArgString(i, length); 54 55 os.print(string); 56 } 57 } 58 59 public static void writeln(ReadWritePair s, Call call, int length) 60 throws Throwable 61 { 62 WriteStream os = s.getWriteStream(); 63 for (int i = 0; i < length; i++) { 64 String string = call.getArgString(i, length); 65 66 os.print(string); 67 } 68 69 os.println(); 70 } 71 72 public static void writeStream(ReadWritePair s, Call call, int length) 73 throws Throwable 74 { 75 if (length < 1) 76 return; 77 78 WriteStream os = s.getWriteStream(); 79 Object obj = call.getArgObject(0, length); 80 81 if (obj instanceof InputStream ) 82 os.writeStream((InputStream ) obj); 83 else if (obj instanceof ReadWritePair) 84 os.writeStream(((ReadWritePair) obj).getReadStream()); 85 else 86 throw new IllegalArgumentException ("expected read stream at " + 87 obj.getClass().getName()); 88 } 89 90 public static WriteStream getOutputStream(ReadWritePair s) 91 { 92 return s.getWriteStream(); 93 } 94 95 public static void printf(ReadWritePair s, Call eval, int length) 96 throws Throwable 97 { 98 if (length == 0) 99 return; 100 101 WriteStream os = s.getWriteStream(); 102 String result = eval.printf(length); 103 104 os.print(result); 105 } 106 107 public static int readByte(ReadWritePair s) 108 throws IOException 109 { 110 ReadStream is = s.getReadStream(); 111 return is.read(); 112 } 113 114 public static String read(ReadWritePair s) 115 throws IOException 116 { 117 ReadStream is = s.getReadStream(); 118 int ch = is.readChar(); 119 120 if (ch < 0) 121 return null; 122 123 return String.valueOf((char) ch); 124 } 125 126 public static String read(ReadWritePair s, int length) 127 throws IOException 128 { 129 ReadStream is = s.getReadStream(); 130 CharBuffer cb = new CharBuffer(); 131 132 for (; length > 0; length--) { 133 int ch = is.readChar(); 134 135 if (ch < 0) 136 break; 137 138 cb.append((char) ch); 139 } 140 141 if (cb.length() == 0) 142 return null; 143 144 return cb.toString(); 145 } 146 147 public static int getAvailable(ReadWritePair s) 148 throws IOException 149 { 150 return s.getReadStream().available(); 151 } 152 153 public static int available(ReadWritePair s) 154 throws IOException 155 { 156 return s.getReadStream().available(); 157 } 158 159 public static String readAvailable(ReadWritePair s, int length) 160 throws IOException 161 { 162 ReadStream is = s.getReadStream(); 163 CharBuffer cb = new CharBuffer(); 164 165 while (is.getAvailable() > 0) { 166 int ch = is.readChar(); 167 168 if (ch < 0) 169 break; 170 171 cb.append((char) ch); 172 } 173 174 if (cb.length() == 0) 175 return null; 176 177 return cb.toString(); 178 } 179 180 public static String readln(ReadWritePair s) 181 throws IOException 182 { 183 ReadStream is = s.getReadStream(); 184 CharBuffer cb = new CharBuffer(); 185 186 if (! is.readln(cb)) 187 return null; 188 189 return cb.toString(); 190 } 191 192 public static ReadStream getInputStream(ReadWritePair s) 193 { 194 return s.getReadStream(); 195 } 196 197 public static void setAttribute(ReadWritePair s, String key, Object value) 198 throws IOException 199 { 200 s.getWriteStream().setAttribute(key, value); 201 } 202 203 public static void removeAttribute(ReadWritePair s, String key) 204 throws IOException 205 { 206 s.getWriteStream().removeAttribute(key); 207 } 208 209 public static Object getAttribute(ReadWritePair s, String key) 210 throws IOException 211 { 212 return s.getReadStream().getAttribute(key); 213 } 214 215 public static Iterator getAttributeNames(ReadWritePair s) 216 throws IOException 217 { 218 return s.getReadStream().getAttributeNames(); 219 } 220 221 public static void flush(ReadWritePair s) 222 throws IOException 223 { 224 s.getWriteStream().flush(); 225 } 226 227 public static void close(ReadWritePair s) 228 throws IOException 229 { 230 s.getWriteStream().close(); 231 s.getReadStream().close(); 232 } 233 } 234 235 | Popular Tags |