1 // File.java - File name
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
15 import gnu.gcj.runtime.FileDeleter;
18 * @author Tom Tromey <tromey@cygnus.com>
19 * @date September 24, 1998
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.
27 public class File implements Serializable, Comparable
29 public boolean canRead ()
32 return _access (READ);
35 public boolean canWrite ()
38 return _access (WRITE);
41 private native boolean performCreate() throws IOException;
44 public boolean createNewFile() throws IOException
47 return performCreate();
50 private native boolean performDelete ();
51 public boolean delete ()
53 SecurityManager s = System.getSecurityManager();
57 return performDelete ();
60 public boolean equals (Object obj)
62 if (! (obj instanceof File))
64 File other = (File) obj;
66 return (path.equals(other.path));
68 return (path.equalsIgnoreCase(other.path));
71 public boolean exists ()
74 return _access (EXISTS);
77 public File (String p)
79 path = normalizePath(p);
82 // Remove duplicate and redundant separator characters.
83 private String normalizePath(String p)
85 int dupIndex = p.indexOf(dupSeparator);
86 int plen = p.length();
88 // Special case: permit Windows UNC path prefix.
89 if (dupSeparator == "\\" && dupIndex == 0)
90 dupIndex = p.indexOf(dupSeparator, 1);
94 // Ignore trailing separator.
95 if (plen > 1 && p.charAt(plen - 1) == separatorChar)
96 return p.substring(0, plen - 1);
101 StringBuffer newpath = new StringBuffer(plen);
103 while (dupIndex != -1)
105 newpath.append(p.substring(last, dupIndex));
106 // Ignore the duplicate path characters.
107 while (p.charAt(dupIndex) == separatorChar)
110 if (dupIndex == plen)
111 return newpath.toString();
113 newpath.append(separatorChar);
115 dupIndex = p.indexOf(dupSeparator, last);
118 // Again, ignore possible trailing separator.
120 if (plen > 1 && p.charAt(plen - 1) == separatorChar)
124 newpath.append(p.substring(last, end));
126 return newpath.toString();
129 public File (String dirPath, String name)
132 throw new NullPointerException ();
133 if (dirPath != null && dirPath.length() > 0)
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);
140 path = normalizePath(dirPath + separatorChar + name);
143 path = normalizePath(name);
146 public File (File dir, String name)
148 this (dir == null ? null : dir.path, name);
152 public String getAbsolutePath ()
156 return System.getProperty("user.dir") + separatorChar + path;
160 public File getAbsoluteFile () throws IOException
162 return new File (getAbsolutePath());
165 public native String getCanonicalPath () throws IOException;
168 public File getCanonicalFile () throws IOException
170 return new File (getCanonicalPath());
173 public String getName ()
175 int last = path.lastIndexOf(separatorChar);
176 return path.substring(last + 1);
179 public String getParent ()
181 int last = path.lastIndexOf(separatorChar);
184 return path.substring(0, last);
188 public File getParentFile ()
190 String parent = getParent ();
191 return (parent == null ? null : new File (parent));
194 public String getPath ()
199 public int hashCode ()
202 return (path.hashCode() ^ 1234321);
204 return (path.toLowerCase().hashCode() ^ 1234321);
207 public native boolean isAbsolute ();
209 public boolean isDirectory ()
212 return _stat (DIRECTORY);
215 public boolean isFile ()
218 return _stat (ISFILE);
222 public boolean isHidden()
225 return _stat (ISHIDDEN);
228 public long lastModified ()
231 return attr (MODIFIED);
234 public long length ()
237 return attr (LENGTH);
240 private final native Object[] performList (FilenameFilter filter,
241 FileFilter fileFilter,
244 public String[] list (FilenameFilter filter)
247 return (String[]) performList (filter, null, String.class);
250 public String[] list ()
253 return (String[]) performList (null, null, String.class);
257 public File[] listFiles()
260 return (File[]) performList (null, null, File.class);
264 public File[] listFiles(FilenameFilter filter)
267 return (File[]) performList (filter, null, File.class);
271 public File[] listFiles(FileFilter filter)
274 return (File[]) performList (null, filter, File.class);
277 public String toString ()
282 public URL toURL () throws MalformedURLException
284 return new URL ("file:" + path + (isDirectory() ? "/" : ""));
287 private final native boolean performMkdir ();
289 public boolean mkdir ()
292 return performMkdir ();
295 private static boolean mkdirs (File x)
299 String p = x.getPath();
300 String parent = x.getParent();
311 public boolean mkdirs ()
316 return mkdirs (new File (path));
319 private static synchronized String nextValue ()
321 return Long.toString(counter++, Character.MAX_RADIX);
325 public static File createTempFile (String prefix, String suffix,
329 // Grab the system temp directory if necessary
330 if (directory == null)
332 String dirname = tmpdir;
335 new IOException("Cannot determine system temporary directory");
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.");
347 if (prefix.length () < 3)
348 throw new IllegalArgumentException ("Prefix too short: " + prefix);
353 // `6' is the number of characters we generate.
354 if (prefix.length () + 6 + suffix.length () > maxPathLen)
357 if (suffix.charAt(0) == '.')
359 suffix = suffix.substring(0, suf_len);
360 if (prefix.length () + 6 + suf_len > maxPathLen)
361 prefix = prefix.substring(0, maxPathLen - 6 - suf_len);
366 // How many times should we try? We choose 100.
367 for (int i = 0; i < 100; ++i)
370 String t = "ZZZZZZ" + nextValue ();
371 String l = prefix + t.substring(t.length() - 6) + suffix;
374 f = new File(directory, l);
375 if (f.createNewFile())
378 catch (IOException ignored)
383 throw new IOException ("cannot create temporary file");
386 private native boolean performSetReadOnly();
389 public boolean setReadOnly()
392 return performSetReadOnly();
395 private static native File[] performListRoots();
398 public static File[] listRoots()
400 File[] roots = performListRoots();
402 SecurityManager s = System.getSecurityManager();
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++)
411 s.checkRead(roots[i].path);
413 catch (SecurityException sx)
419 if (count != roots.length)
421 File[] newRoots = new File[count];
423 for (int i=0; i < roots.length; i++)
425 if (roots[i] != null)
426 newRoots[k++] = roots[i];
434 public static File createTempFile (String prefix, String suffix)
437 return createTempFile (prefix, suffix, null);
441 public int compareTo(File other)
444 return path.compareTo (other.path);
446 return path.compareToIgnoreCase (other.path);
450 public int compareTo(Object o)
452 File other = (File) o;
453 return compareTo (other);
456 private native boolean performRenameTo (File dest);
457 public boolean renameTo (File dest)
459 SecurityManager s = System.getSecurityManager();
460 String sname = getName();
461 String dname = dest.getName();
467 return performRenameTo (dest);
470 private native boolean performSetLastModified(long time);
473 public boolean setLastModified(long time)
476 return performSetLastModified(time);
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);
485 static final String tmpdir = System.getProperty("java.io.tmpdir");
486 static int maxPathLen;
487 static boolean caseSensitive;
488 static String dupSeparator = separator + separator;
495 // Native function called at class initialization. This should should
496 // set the maxPathLen and caseSensitive variables.
497 private static native void init_native();
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 ());
506 private void checkWrite ()
508 SecurityManager s = System.getSecurityManager();
513 private void checkRead ()
515 SecurityManager s = System.getSecurityManager();
521 * Add this File to the set of files to be deleted upon normal
526 // FIXME: This should use the ShutdownHook API once we implement that.
527 public void deleteOnExit ()
529 SecurityManager sm = System.getSecurityManager ();
531 sm.checkDelete (getName ());
533 FileDeleter.add (this);
536 private void writeObject (ObjectOutputStream oos) throws IOException
538 oos.defaultWriteObject ();
539 oos.writeChar (separatorChar);
542 private void readObject (ObjectInputStream ois)
543 throws ClassNotFoundException, IOException
545 ois.defaultReadObject ();
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);
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;
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;
564 // QUERY arguments to attr function.
565 private final static int MODIFIED = 0;
566 private final static int LENGTH = 1;
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
572 private final native boolean _access (int query);
573 private final native boolean _stat (int query);
575 private static final long serialVersionUID = 301077366599181567L;