Source code: com/memoire/fu/FuFileUrl.java
1 /**
2 * @modification $Date: 2002/12/16 09:09:11 $
3 * @statut unstable
4 * @file FuFileUrl.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.fu;
13
14 import com.memoire.fu.*;
15
16 import java.io.*;
17 import java.net.*;
18 import java.util.*;
19
20 /**
21 * Remote file.
22 */
23 public class FuFileUrl
24 extends FuFile
25 {
26 protected URL url_;
27 protected String name_;
28
29 protected boolean init_;
30 protected boolean exists_;
31 protected long lastModified_;
32 protected long length_;
33 protected String[] list_;
34
35 protected boolean canRead_;
36 protected boolean canWrite_;
37
38 /*
39 public FuFileUrl(FuFileUrl _a, String _n) throws MalformedURLException
40 {
41 this(new URL(_a.url_,_n));
42 }
43 */
44
45 public FuFileUrl(URL _a, String _n) throws MalformedURLException
46 {
47 this(new URL(_a,_n));
48 }
49
50 public FuFileUrl(URL _url)
51 {
52 super(_url.getFile());
53 url_ =_url;
54 list_=new String[0];
55 init_=false;
56 canRead_ =true;
57 canWrite_=true;
58 }
59
60 public final void init()
61 {
62 if(!init_)
63 {
64 init_=true;
65
66 final boolean[] done=new boolean[] { false };
67
68 Runnable runnable=new Runnable()
69 {
70 public void run()
71 {
72 try
73 {
74 exists_=true;
75 URLConnection cnx=getContentURL().openConnection();
76 //System.err.println("CONTENT="+getContentURL());
77 lastModified_=cnx.getLastModified();
78 length_ =cnx.getContentLength();
79
80 if(isDirectory())
81 {
82 InputStream in=cnx.getInputStream();
83 BufferedReader nr=new BufferedReader(new InputStreamReader(in));
84 Vector v=readList(nr);
85 nr.close();
86
87 int l=v.size();
88 list_=new String[l];
89 for(int i=0;i<l;i++)
90 list_[i]=(String)v.elementAt(i);
91 }
92 }
93 catch(Exception ex)
94 {
95 exists_=false;
96 //ex.printStackTrace();
97 }
98 done[0]=true;
99 }
100 };
101
102 Thread t=new Thread(runnable,"Connecting "+getContentURL());
103 t.start();
104 for(int i=0;i<300;i++)
105 {
106 if(done[0]) break;
107 try { Thread.sleep(100l); }
108 catch(InterruptedException ex) { }
109 System.err.print("~");
110 }
111 if(!done[0]) t.interrupt();
112 }
113 }
114
115 protected Vector readList(BufferedReader _nr)
116 throws IOException
117 {
118 return new Vector(0,1);
119 }
120
121 // FuFile API
122
123 // redefine in FuFileHttp
124 public URL getContentURL()
125 {
126 return url_;
127 }
128
129 public final FuFile createChild(String _name)
130 {
131 FuFile r=null;
132 try
133 { r=createFile(new URL(url_,_name)); }
134 catch(MalformedURLException ex)
135 { }
136 return r;
137 }
138
139 public final InputStream getInputStream() throws IOException
140 {
141 InputStream r=null;
142
143 if(canRead())
144 {
145 try
146 {
147 URLConnection cnx=getContentURL().openConnection();
148 cnx.setDoInput(true);
149 r=cnx.getInputStream();
150 }
151 catch(IOException ex) { }
152 }
153
154 if(r==null)
155 {
156 canRead_=false;
157 throw new IOException("Can not read "+getContentURL());
158 }
159
160 return r;
161 }
162
163 public final OutputStream getOutputStream() throws IOException
164 {
165 OutputStream r=null;
166
167 if(canWrite())
168 {
169 try
170 {
171 URLConnection cnx=getContentURL().openConnection();
172 cnx.setDoOutput(true);
173 r=cnx.getOutputStream();
174 }
175 catch(IOException ex) { }
176 }
177
178 if(r==null)
179 {
180 canWrite_=false;
181 throw new IOException("Can not write "+getContentURL());
182 }
183
184 return r;
185 }
186
187 public final String getViewText()
188 {
189 return "["+url_.getHost()+"]"+url_.getFile();
190 }
191
192 // Standard API
193
194 public final boolean canRead()
195 {
196 return canRead_;
197 }
198
199 public final boolean canWrite()
200 {
201 return canWrite_;
202 }
203
204 //int compareTo(File pathname)
205 //int compareTo(Object o)
206
207 public final boolean createNewFile()
208 {
209 return false;
210 }
211
212 public final boolean delete()
213 {
214 return false;
215 }
216
217 public final void deleteOnExit()
218 {
219 }
220
221 public final boolean equals(Object _o)
222 {
223 if((_o==null)||!(_o instanceof File)) return false;
224
225 String a=getAbsolutePath();
226 String b=((File)_o).getAbsolutePath();
227 if(a.endsWith("/")) a=a.substring(0,a.length()-1);
228 if(b.endsWith("/")) b=b.substring(0,b.length()-1);
229 return a.equals(b);
230 }
231
232 public final boolean exists()
233 {
234 init();
235 return exists_;
236 //return !init_ ? true : exists_;
237 }
238
239 public /*final*/ String getAbsolutePath()
240 {
241 return url_.toString();
242 }
243
244 public final String getCanonicalPath()
245 {
246 return url_.toString();
247 }
248
249 // use for '..'
250 public final void setName(String _name)
251 {
252 name_=_name;
253 }
254
255 public final String getName()
256 {
257 if(name_!=null) return name_;
258
259 String r;
260
261 try
262 {
263 r=url_.getFile();//Path();
264 if((r!=null)&&r.endsWith("/"))
265 r=r.substring(0,r.length()-1);
266 int i=r.lastIndexOf('/');
267 r=r.substring(i+1);
268 }
269 catch(Exception ex) { r=null; }
270
271 return r;
272 }
273
274 public final String getParent()
275 {
276 String r;
277
278 try
279 {
280 r=url_.getFile();
281 if((r!=null)&&r.endsWith("/"))
282 r=r.substring(0,r.length()-1);
283 int i=r.lastIndexOf('/');
284 if(i>=0)
285 {
286 r=r.substring(0,i+1);
287 r=new URL(url_,r).toString();
288 }
289 else r=null;
290 }
291 catch(Exception ex) { r=null; }
292
293 return r;
294 }
295
296 public final String getPath()
297 {
298 return url_.toString();
299 }
300
301 public final int hashCode()
302 {
303 return url_.hashCode();
304 }
305
306 public final boolean isAbsolute()
307 {
308 return true;
309 }
310
311 public final boolean isDirectory()
312 {
313 String n=url_.getFile();
314 return (n==null)||n.endsWith("/");
315 }
316
317 public final boolean isFile()
318 {
319 return !isDirectory();
320 }
321
322 public final boolean isHidden()
323 {
324 String n=getName();
325 return (n!=null)&&n.startsWith(".");
326 }
327
328 public final long lastModified()
329 {
330 init();
331 return lastModified_;
332 }
333
334 public final long length()
335 {
336 init();
337 return length_;
338 }
339
340 public final String[] list()
341 {
342 init();
343 return list_;
344 }
345
346 //String[] list(FilenameFilter filter)
347
348 public final File[] listFiles()
349 {
350 init();
351
352 int l=list_.length;
353 File[] r=new File[l];
354
355 //try
356 //{
357 for(int i=0;i<l;i++)
358 r[i]=createChild(list_[i]);
359 //new URLAdapter(new URL(url_,list_[i]));
360 //}
361 //catch(MalformedURLException ex) { r=new File[0]; }
362
363 return r;
364 }
365
366 //File[] listFiles(FileFilter filter)
367 //File[] listFiles(FilenameFilter filter)
368
369 public boolean mkdir()
370 {
371 return false;
372 }
373
374 public final boolean mkdirs()
375 {
376 return false;
377 }
378
379 public final boolean renameTo(File _dest)
380 {
381 return false;
382 }
383
384 public final boolean setLastModified(long _time)
385 {
386 return false;
387 }
388
389 public final boolean setReadOnly()
390 {
391 canWrite_=false;
392 return true;
393 }
394
395 public final String toString()
396 {
397 return url_.toString();
398 }
399
400 public final URL toURL()
401 {
402 return url_;
403 }
404 }
405