KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > eswrap > com > caucho > vfs > PathEcmaWrap


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 public class PathEcmaWrap {
47   public static ReadStream openRead(Path p)
48     throws IOException JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
81   {
82     return p.remove();
83   }
84
85   public static boolean renameTo(Path p, ESBase dst)
86     throws IOException JavaDoc
87   {
88     Object JavaDoc 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   // XXX: bogus for javascript
108
public static Path call(Path p, String JavaDoc name)
109     throws IOException JavaDoc
110   {
111     Path path = p.lookup(name);
112
113     return path;
114   }
115
116   public static Path call(Path p)
117     throws IOException JavaDoc
118   {
119     return p;
120   }
121
122   public static Iterator JavaDoc keys(Path p)
123   {
124     try {
125       return p.iterator();
126     } catch (IOException JavaDoc e) {
127       return null;
128     }
129   }
130
131   public static void write(Path path, Call call, int length)
132     throws Throwable JavaDoc
133   {
134     WriteStream s = path.openWrite();
135
136     try {
137       for (int i = 0; i < length; i++) {
138     String JavaDoc 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 JavaDoc
149   {
150     WriteStream s = path.openWrite();
151
152     try {
153       for (int i = 0; i < length; i++) {
154     String JavaDoc 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 JavaDoc is)
166   throws Throwable JavaDoc
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 JavaDoc
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 JavaDoc
191   {
192     WriteStream s = path.openAppend();
193
194     try {
195       for (int i = 0; i < length; i++) {
196     String JavaDoc 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 JavaDoc
207   {
208     WriteStream s = path.openAppend();
209
210     try {
211       for (int i = 0; i < length; i++) {
212     String JavaDoc 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 JavaDoc is)
224   throws Throwable JavaDoc
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 JavaDoc
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 JavaDoc o)
249     {
250       ReadWritePair pair = (ReadWritePair) o;
251
252       pair.getReadStream().close();
253
254       try {
255     pair.getWriteStream().close();
256       } catch (IOException JavaDoc e) {
257       }
258     }
259   };
260
261   private static ExitListener exitInputStream = new ExitListener() {
262     public void handleExit(Object JavaDoc o)
263     {
264       InputStream JavaDoc stream = (InputStream JavaDoc) o;
265
266       try {
267     stream.close();
268       } catch (IOException JavaDoc e) {
269       }
270     }
271   };
272
273   private static ExitListener exitOutputStream = new ExitListener() {
274     public void handleExit(Object JavaDoc o)
275     {
276       OutputStream JavaDoc stream = (OutputStream JavaDoc) o;
277
278       try {
279     stream.close();
280       } catch (IOException JavaDoc e) {
281       }
282     }
283   };
284 }
285
Popular Tags