1 // File.java - File name
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
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
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date September 24, 1998
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19 * "The Java Language Specification", ISBN 0-201-63451-1
20 * Status: Complete to version 1.1; 1.2 functionality missing.
21 * A known bug: most calls to the security manager can generate
22 * IOException since we use the canonical path.
25 public class File implements Serializable
27 public boolean canRead ()
29 return access (checkRead (), READ);
32 public boolean canWrite ()
34 SecurityManager s = System.getSecurityManager();
35 String p = safeCanonicalPath ();
36 // FIXME: it isn't entirely clear what to do if we can't find the
42 return access (p, WRITE);
45 private final native boolean performDelete (String canon);
46 public boolean delete ()
48 SecurityManager s = System.getSecurityManager();
49 String p = safeCanonicalPath ();
50 // FIXME: what is right?
55 return performDelete (p);
58 public boolean equals (Object obj)
60 if (! (obj instanceof File))
62 File other = (File) obj;
63 return path.compareTo(other.path) == 0;
66 public boolean exists ()
68 return access (checkRead (), EXISTS);
71 public File (String p)
74 throw new NullPointerException ();
78 public File (String dirPath, String name)
81 throw new NullPointerException ();
84 // Try to be smart about the number of separator characters.
85 if (dirPath.charAt(dirPath.length() - 1) == separatorChar)
86 path = dirPath + name;
88 path = dirPath + separatorChar + name;
94 public File (File dir, String name)
96 this (dir == null ? null : dir.path, name);
99 public String getAbsolutePath ()
103 return System.getProperty("user.dir") + separatorChar + path;
106 public native String getCanonicalPath () throws IOException;
108 public String getName ()
110 int last = path.lastIndexOf(separatorChar);
111 return path.substring(last + 1);
114 public String getParent ()
116 int last = path.lastIndexOf(separatorChar);
119 return path.substring(0, last);
122 public String getPath ()
127 public int hashCode ()
130 return path.hashCode();
133 public native boolean isAbsolute ();
135 public boolean isDirectory ()
137 return stat (checkRead (), DIRECTORY);
140 public boolean isFile ()
142 return stat (checkRead (), ISFILE);
145 public long lastModified ()
147 return attr (checkRead (), MODIFIED);
150 public long length ()
152 return attr (checkRead (), LENGTH);
155 private final native String[] performList (String canon,
156 FilenameFilter filter);
157 public String[] list (FilenameFilter filter)
159 return performList (checkRead (), filter);
162 public String[] list ()
164 return performList (checkRead (), null);
167 public String toString ()
172 private final native boolean performMkdir ();
173 public boolean mkdir ()
175 SecurityManager s = System.getSecurityManager();
178 // NOTE: in theory we should use the canonical path. In
179 // practice, we can't compute the canonical path until we've
180 // made this completely. Lame.
183 return performMkdir ();
186 private static boolean mkdirs (File x)
190 String p = x.getPath();
191 String parent = x.getParent();
202 public boolean mkdirs ()
204 SecurityManager s = System.getSecurityManager();
207 // NOTE: in theory we should use the canonical path. In
208 // practice, we can't compute the canonical path until we've
209 // made this completely. Lame.
215 return mkdirs (new File (path));
218 private final native boolean performRenameTo (File dest);
219 public boolean renameTo (File dest)
221 SecurityManager s = System.getSecurityManager();
224 // FIXME: JCL doesn't specify which path to check. We check the
225 // source since we can canonicalize it.
226 s.checkWrite(safeCanonicalPath());
228 return performRenameTo (dest);
231 public static final String pathSeparator
232 = System.getProperty("path.separator");
233 public static final char pathSeparatorChar = pathSeparator.charAt(0);
234 public static final String separator = System.getProperty("file.separator");
235 public static final char separatorChar = separator.charAt(0);
241 // mkdirs() uses this to avoid repeated allocations.
242 private final void setPath (String n)
248 private final String checkRead ()
250 SecurityManager s = System.getSecurityManager();
251 String p = safeCanonicalPath ();
259 // Return canonical path, or null.
260 private final String safeCanonicalPath ()
265 p = getCanonicalPath ();
267 catch (IOException x)
274 // QUERY arguments to access function.
275 private final static int READ = 0;
276 private final static int WRITE = 1;
277 private final static int EXISTS = 2;
279 // QUERY arguments to stat function.
280 private final static int DIRECTORY = 0;
281 private final static int ISFILE = 1;
283 // QUERY arguments to attr function.
284 private final static int MODIFIED = 0;
285 private final static int LENGTH = 1;
287 private final native long attr (String p, int query);
288 private final native boolean access (String p, int query);
289 private final native boolean stat (String p, int query);