OSDN Git Service

25b6fcd674da6cea778dddad8bd36bda76ac2966
[pf3gnuchains/gcc-fork.git] / libjava / java / io / File.java
1 // File.java - File name
2
3 /* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.io;
12
13 import java.util.*;
14 import java.net.*;
15 import gnu.gcj.runtime.FileDeleter;
16
17 /**
18  * @author Tom Tromey <tromey@cygnus.com>
19  * @date September 24, 1998 
20  */
21
22 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
23  * "The Java Language Specification", ISBN 0-201-63451-1
24  * Status:  Complete to version 1.3.
25  */
26
27 public class File implements Serializable, Comparable
28 {
29   public boolean canRead ()
30   {
31     checkRead();
32     return _access (READ);
33   }
34
35   public boolean canWrite ()
36   {
37     checkWrite();
38     return _access (WRITE);
39   }
40   
41   private native boolean performCreate() throws IOException;
42
43   /** @since 1.2 */
44   public boolean createNewFile() throws IOException
45   {
46     checkWrite();
47     return performCreate();
48   }
49   
50   private native boolean performDelete ();
51   public boolean delete ()
52   {
53     SecurityManager s = System.getSecurityManager();
54     String name = path;
55     if (s != null)
56       s.checkDelete(path);
57     return performDelete ();
58   }
59
60   public boolean equals (Object obj)
61   {
62     if (! (obj instanceof File))
63       return false;
64     File other = (File) obj;
65     if (caseSensitive)
66       return (path.equals(other.path));
67     else
68       return (path.equalsIgnoreCase(other.path));      
69   }
70
71   public boolean exists ()
72   {
73     checkRead();
74     return _access (EXISTS);
75   }
76
77   public File (String p)
78   {
79     path = normalizePath(p);
80   }
81
82   // Remove duplicate and redundant separator characters.
83   private String normalizePath(String p)
84   {
85     int dupIndex = p.indexOf(dupSeparator);
86     int plen = p.length();
87     
88     // Special case: permit Windows UNC path prefix.
89     if (dupSeparator == "\\" && dupIndex == 0)
90       dupIndex = p.indexOf(dupSeparator, 1);
91
92     if (dupIndex == -1)
93       {
94         // Ignore trailing separator.
95         if (plen > 1 && p.charAt(plen - 1) == separatorChar)
96           return p.substring(0, plen - 1);
97         else
98           return p;
99       }
100     
101     StringBuffer newpath = new StringBuffer(plen);
102     int last = 0;
103     while (dupIndex != -1)
104       {
105         newpath.append(p.substring(last, dupIndex));
106         // Ignore the duplicate path characters.
107         while (p.charAt(dupIndex) == separatorChar)
108           {
109             dupIndex++;
110             if (dupIndex == plen)
111               return newpath.toString();
112           }
113         newpath.append(separatorChar);
114         last = dupIndex;
115         dupIndex = p.indexOf(dupSeparator, last);
116       }
117     
118     // Again, ignore possible trailing separator.
119     int end;
120     if (plen > 1 && p.charAt(plen - 1) == separatorChar)
121       end = plen - 1;
122     else
123       end = plen;
124     newpath.append(p.substring(last, end));
125     
126     return newpath.toString();
127   }
128   
129   public File (String dirPath, String name)
130   {
131     if (name == null)
132       throw new NullPointerException ();
133     if (dirPath != null && dirPath.length() > 0)
134       {
135         // Try to be smart about the number of separator characters.
136         if (dirPath.charAt(dirPath.length() - 1) == separatorChar
137             || name.length() == 0)
138           path = normalizePath(dirPath + name);
139         else
140           path = normalizePath(dirPath + separatorChar + name);
141       }
142     else
143       path = normalizePath(name);
144   }
145
146   public File (File dir, String name)
147   {
148     this (dir == null ? null : dir.path, name);
149   }
150
151   // FIXME  ???
152   public String getAbsolutePath ()
153   {
154     if (isAbsolute ())
155       return path;
156     return System.getProperty("user.dir") + separatorChar + path;
157   }
158
159   /** @since 1.2 */
160   public File getAbsoluteFile () throws IOException
161   {
162     return new File (getAbsolutePath());
163   }
164
165   public native String getCanonicalPath () throws IOException;
166
167   /** @since 1.2 */
168   public File getCanonicalFile () throws IOException
169   {
170     return new File (getCanonicalPath());
171   }
172
173   public String getName ()
174   {
175     int last = path.lastIndexOf(separatorChar);
176     return path.substring(last + 1);
177   }
178
179   public String getParent ()
180   {
181     int last = path.lastIndexOf(separatorChar);
182     if (last == -1)
183       return null;
184     return path.substring(0, last);
185   }
186
187   /** @since 1.2 */
188   public File getParentFile ()
189   {
190     String parent = getParent ();
191     return (parent == null ? null : new File (parent));
192   }
193
194   public String getPath ()
195   {
196     return path;
197   }
198
199   public int hashCode ()
200   {
201     if (caseSensitive)
202       return (path.hashCode() ^ 1234321);
203     else
204       return (path.toLowerCase().hashCode() ^ 1234321);
205   }
206
207   public native boolean isAbsolute ();
208
209   public boolean isDirectory ()
210   {
211     checkRead();
212     return _stat (DIRECTORY);
213   }
214
215   public boolean isFile ()
216   {
217     checkRead();
218     return _stat (ISFILE);
219   }
220
221   /** @since 1.2 */
222   public boolean isHidden()
223   {
224     checkRead();
225     return _stat (ISHIDDEN);
226   }
227
228   public long lastModified ()
229   {
230     checkRead();
231     return attr (MODIFIED);
232   }
233
234   public long length ()
235   {
236     checkRead();
237     return attr (LENGTH);
238   }
239     
240   private final native Object[] performList (FilenameFilter filter,
241                                              FileFilter fileFilter,
242                                              Class result_type);
243
244   public String[] list (FilenameFilter filter)
245   {
246     checkRead();
247     return (String[]) performList (filter, null, String.class);
248   }
249
250   public String[] list ()
251   {
252     checkRead();
253     return (String[]) performList (null, null, String.class);
254   }
255
256   /** @since 1.2 */
257   public File[] listFiles()
258   {
259     checkRead();
260     return (File[]) performList (null, null, File.class);
261   }
262   
263   /** @since 1.2 */
264   public File[] listFiles(FilenameFilter filter)
265   {
266     checkRead();
267     return (File[]) performList (filter, null, File.class);
268   }
269   
270   /** @since 1.2 */
271   public File[] listFiles(FileFilter filter)
272   {
273     checkRead();
274     return (File[]) performList (null, filter, File.class);
275   }
276
277   public String toString ()
278   {
279     return path;
280   }
281
282   public URL toURL () throws MalformedURLException
283   {
284     return new URL ("file:" + path + (isDirectory() ? "/" : ""));
285   }
286
287   private final native boolean performMkdir ();
288
289   public boolean mkdir ()
290   {
291     checkWrite();
292     return performMkdir ();
293   }
294
295   private static boolean mkdirs (File x)
296   {
297     if (x.isDirectory())
298       return true;
299     String p = x.getPath();
300     String parent = x.getParent();
301     if (parent != null)
302       {
303         x.path = parent;
304         if (! mkdirs (x))
305           return false;
306         x.path = p;
307       }
308     return x.mkdir();
309   }
310
311   public boolean mkdirs ()
312   {
313     checkWrite();
314     if (isDirectory ())
315       return false;
316     return mkdirs (new File (path));
317   }
318
319   private static synchronized String nextValue ()
320   {
321     return Long.toString(counter++, Character.MAX_RADIX);
322   }
323
324   /** @since 1.2 */
325   public static File createTempFile (String prefix, String suffix,
326                                      File directory)
327     throws IOException
328   {
329     // Grab the system temp directory if necessary
330     if (directory == null)
331       {
332         String dirname = tmpdir;
333         if (dirname == null)
334           throw 
335             new IOException("Cannot determine system temporary directory"); 
336         
337         directory = new File(dirname);
338         if (!directory.exists())
339           throw new IOException("System temporary directory " 
340                                 + directory.getName() + " does not exist.");
341         if (!directory.isDirectory())
342           throw new IOException("System temporary directory " 
343                                 + directory.getName() 
344                                 + " is not really a directory.");
345       }
346
347     if (prefix.length () < 3)
348       throw new IllegalArgumentException ("Prefix too short: " + prefix);
349     if (suffix == null)
350       suffix = ".tmp";
351
352     // Truncation rules.
353     // `6' is the number of characters we generate.
354     if (prefix.length () + 6 + suffix.length () > maxPathLen)
355       {
356         int suf_len = 0;
357         if (suffix.charAt(0) == '.')
358           suf_len = 4;
359         suffix = suffix.substring(0, suf_len);
360         if (prefix.length () + 6 + suf_len > maxPathLen)
361           prefix = prefix.substring(0, maxPathLen - 6 - suf_len);
362       }
363
364     File f;
365
366     // How many times should we try?  We choose 100.
367     for (int i = 0; i < 100; ++i)
368       {
369         // This is ugly.
370         String t = "ZZZZZZ" + nextValue ();
371         String l = prefix + t.substring(t.length() - 6) + suffix;
372         try
373           {
374             f = new File(directory, l);
375             if (f.createNewFile())
376               return f;
377           }
378         catch (IOException ignored)
379           {
380           }
381       }
382
383     throw new IOException ("cannot create temporary file");
384   }
385
386   private native boolean performSetReadOnly();
387
388   /** @since 1.2 */
389   public boolean setReadOnly()
390   {
391     checkWrite();
392     return performSetReadOnly();
393   }
394
395   private static native File[] performListRoots();
396
397   /** @since 1.2 */
398   public static File[] listRoots()
399   {
400     File[] roots = performListRoots();
401     
402     SecurityManager s = System.getSecurityManager();
403     if (s != null)
404       {
405         // Only return roots to which the security manager permits read access.
406         int count = roots.length;
407         for (int i = 0; i < roots.length; i++)
408           {
409             try
410               {
411                 s.checkRead(roots[i].path);             
412               }
413             catch (SecurityException sx)
414               {
415                 roots[i] = null;
416                 count--;
417               }
418           }
419         if (count != roots.length)
420           {
421             File[] newRoots = new File[count];
422             int k = 0;
423             for (int i=0; i < roots.length; i++)
424               {
425                 if (roots[i] != null)
426                   newRoots[k++] = roots[i];
427               }
428             roots = newRoots;
429           }
430       }
431     return roots;
432   }
433
434   public static File createTempFile (String prefix, String suffix)
435     throws IOException
436   {
437     return createTempFile (prefix, suffix, null);
438   }
439
440   /** @since 1.2 */
441   public int compareTo(File other)
442   {
443     if (caseSensitive)
444       return path.compareTo (other.path);
445     else
446       return path.compareToIgnoreCase (other.path);
447   }
448
449   /** @since 1.2 */
450   public int compareTo(Object o)
451   {
452     File other = (File) o;
453     return compareTo (other);
454   }
455
456   private native boolean performRenameTo (File dest);
457   public boolean renameTo (File dest)
458   {
459     SecurityManager s = System.getSecurityManager();
460     String sname = getName();
461     String dname = dest.getName();
462     if (s != null)
463       {
464         s.checkWrite(sname);
465         s.checkWrite(dname);
466       }
467     return performRenameTo (dest);
468   }
469
470   private native boolean performSetLastModified(long time);
471   
472   /** @since 1.2 */
473   public boolean setLastModified(long time)
474   {
475     checkWrite();
476     return performSetLastModified(time);
477   }
478
479   public static final String pathSeparator
480     = System.getProperty("path.separator");
481   public static final char pathSeparatorChar = pathSeparator.charAt(0);
482   public static final String separator = System.getProperty("file.separator");
483   public static final char separatorChar = separator.charAt(0);
484
485   static final String tmpdir = System.getProperty("java.io.tmpdir");
486   static int maxPathLen;
487   static boolean caseSensitive;
488   static String dupSeparator = separator + separator;
489   
490   static
491   {
492     init_native();
493   }
494   
495   // Native function called at class initialization. This should should
496   // set the maxPathLen and caseSensitive variables.
497   private static native void init_native();
498
499   // The path.
500   private String path;
501
502   // We keep a counter for use by createTempFile.  We choose the first
503   // value randomly to try to avoid clashes with other VMs.
504   private static long counter = Double.doubleToLongBits (Math.random ());
505
506   private void checkWrite ()
507   {
508     SecurityManager s = System.getSecurityManager();
509     if (s != null)
510       s.checkWrite(path);
511   }
512
513   private void checkRead ()
514   {
515     SecurityManager s = System.getSecurityManager();
516     if (s != null)
517       s.checkRead(path);
518   }
519
520   /** 
521     * Add this File to the set of files to be deleted upon normal
522     * termination.
523     *
524     * @since 1.2 
525     */
526   // FIXME: This should use the ShutdownHook API once we implement that.
527   public void deleteOnExit ()
528   {
529     SecurityManager sm = System.getSecurityManager ();
530     if (sm != null)
531       sm.checkDelete (getName ());
532
533     FileDeleter.add (this);
534   }
535
536   private void writeObject (ObjectOutputStream oos) throws IOException
537   {
538     oos.defaultWriteObject ();
539     oos.writeChar (separatorChar);
540   }
541
542   private void readObject (ObjectInputStream ois)
543     throws ClassNotFoundException, IOException
544   {
545     ois.defaultReadObject ();
546
547     // If the file was from an OS with a different dir separator,
548     // fixup the path to use the separator on this OS.
549     char oldSeparatorChar = ois.readChar ();
550     if (oldSeparatorChar != separatorChar)
551       path = path.replace (oldSeparatorChar, separatorChar);
552   }
553
554   // QUERY arguments to access function.
555   private final static int READ = 0;
556   private final static int WRITE = 1;
557   private final static int EXISTS = 2;
558
559   // QUERY arguments to stat function.
560   private final static int DIRECTORY = 0;
561   private final static int ISFILE = 1;
562   private final static int ISHIDDEN = 2;
563
564   // QUERY arguments to attr function.
565   private final static int MODIFIED = 0;
566   private final static int LENGTH = 1;
567   
568   private final native long attr (int query);
569   // On OSF1 V5.0, `stat' is a macro.  It is easiest to use the name
570   // `_stat' instead.  We do the same thing for `_access' just in
571   // case.
572   private final native boolean _access (int query);
573   private final native boolean _stat (int query);
574
575   private static final long serialVersionUID = 301077366599181567L;
576 }