1 28 29 package com.caucho.eswrap.com.caucho.vfs; 30 31 import com.caucho.es.Call; 32 import com.caucho.es.ESBase; 33 import com.caucho.es.ESException; 34 import com.caucho.util.Exit; 35 import com.caucho.util.ExitListener; 36 import com.caucho.vfs.Path; 37 import com.caucho.vfs.ReadStream; 38 import com.caucho.vfs.ReadWritePair; 39 import com.caucho.vfs.WriteStream; 40 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 import java.util.Iterator ; 45 46 public class PathEcmaWrap { 47 public static ReadStream openRead(Path p) 48 throws IOException 49 { 50 ReadStream s = p.openRead(); 51 Exit.addExit(exitInputStream, s); 52 return s; 53 } 54 55 public static WriteStream openWrite(Path p) 56 throws IOException 57 { 58 WriteStream s = p.openWrite(); 59 Exit.addExit(exitOutputStream, s); 60 return s; 61 } 62 63 public static ReadWritePair openReadWrite(Path p) 64 throws IOException 65 { 66 ReadWritePair s = p.openReadWrite(); 67 Exit.addExit(exitStream, s); 68 return s; 69 } 70 71 public static WriteStream openAppend(Path p) 72 throws IOException 73 { 74 WriteStream s = p.openAppend(); 75 Exit.addExit(exitOutputStream, s); 76 return s; 77 } 78 79 public static boolean remove(Path p) 80 throws IOException 81 { 82 return p.remove(); 83 } 84 85 public static boolean renameTo(Path p, ESBase dst) 86 throws IOException 87 { 88 Object value; 89 90 try { 91 value = dst.toJavaObject(); 92 } catch (ESException e) { 93 return false; 94 } 95 96 if (value == null) 97 return false; 98 99 if (value instanceof Path) 100 return p.renameTo((Path) value); 101 else { 102 Path top = p.getParent().lookup(value.toString()); 103 return p.renameTo(top); 104 } 105 } 106 107 public static Path call(Path p, String name) 109 throws IOException 110 { 111 Path path = p.lookup(name); 112 113 return path; 114 } 115 116 public static Path call(Path p) 117 throws IOException 118 { 119 return p; 120 } 121 122 public static Iterator keys(Path p) 123 { 124 try { 125 return p.iterator(); 126 } catch (IOException e) { 127 return null; 128 } 129 } 130 131 public static void write(Path path, Call call, int length) 132 throws Throwable 133 { 134 WriteStream s = path.openWrite(); 135 136 try { 137 for (int i = 0; i < length; i++) { 138 String string = call.getArgString(i, length); 139 140 s.print(string); 141 } 142 } finally { 143 s.close(); 144 } 145 } 146 147 public static void writeln(Path path, Call call, int length) 148 throws Throwable 149 { 150 WriteStream s = path.openWrite(); 151 152 try { 153 for (int i = 0; i < length; i++) { 154 String string = call.getArgString(i, length); 155 156 s.print(string); 157 } 158 159 s.print('\n'); 160 } finally { 161 s.close(); 162 } 163 } 164 165 public static void writeStream(Path path, InputStream is) 166 throws Throwable 167 { 168 WriteStream s = path.openWrite(); 169 170 try { 171 s.writeStream(is); 172 } finally { 173 s.close(); 174 } 175 } 176 177 public static void writeFile(Path path, Path file) 178 throws Throwable 179 { 180 WriteStream s = path.openWrite(); 181 182 try { 183 s.writeFile(file); 184 } finally { 185 s.close(); 186 } 187 } 188 189 public static void append(Path path, Call call, int length) 190 throws Throwable 191 { 192 WriteStream s = path.openAppend(); 193 194 try { 195 for (int i = 0; i < length; i++) { 196 String string = call.getArgString(i, length); 197 198 s.print(string); 199 } 200 } finally { 201 s.close(); 202 } 203 } 204 205 public static void appendln(Path path, Call call, int length) 206 throws Throwable 207 { 208 WriteStream s = path.openAppend(); 209 210 try { 211 for (int i = 0; i < length; i++) { 212 String string = call.getArgString(i, length); 213 214 s.print(string); 215 } 216 217 s.print('\n'); 218 } finally { 219 s.close(); 220 } 221 } 222 223 public static void appendStream(Path path, InputStream is) 224 throws Throwable 225 { 226 WriteStream s = path.openAppend(); 227 228 try { 229 s.writeStream(is); 230 } finally { 231 s.close(); 232 } 233 } 234 235 public static void appendFile(Path path, Path file) 236 throws Throwable 237 { 238 WriteStream s = path.openAppend(); 239 240 try { 241 s.writeFile(file); 242 } finally { 243 s.close(); 244 } 245 } 246 247 private static ExitListener exitStream = new ExitListener() { 248 public void handleExit(Object o) 249 { 250 ReadWritePair pair = (ReadWritePair) o; 251 252 pair.getReadStream().close(); 253 254 try { 255 pair.getWriteStream().close(); 256 } catch (IOException e) { 257 } 258 } 259 }; 260 261 private static ExitListener exitInputStream = new ExitListener() { 262 public void handleExit(Object o) 263 { 264 InputStream stream = (InputStream ) o; 265 266 try { 267 stream.close(); 268 } catch (IOException e) { 269 } 270 } 271 }; 272 273 private static ExitListener exitOutputStream = new ExitListener() { 274 public void handleExit(Object o) 275 { 276 OutputStream stream = (OutputStream ) o; 277 278 try { 279 stream.close(); 280 } catch (IOException e) { 281 } 282 } 283 }; 284 } 285 | Popular Tags |