]>
gcc.gnu.org Git - gcc.git/blob - libjava/java/io/File.java
c4a4e39a19df25d03553f491e04c67fa67cdd404
1 // File.java - File name
3 /* Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
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 ();
82 if (dirPath
!= null && dirPath
.length() > 0)
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 static synchronized String
nextValue ()
220 return Long
.toString(counter
++, Character
.MAX_RADIX
);
223 public static File
createTempFile (String prefix
, String suffix
,
227 FileDescriptor desc
= new FileDescriptor ();
229 SecurityManager s
= System
.getSecurityManager();
233 if (prefix
.length () < 3)
234 throw new IllegalArgumentException ();
238 // FIXME: filename length varies by architecture and filesystem.
239 int max_length
= 255;
242 // `6' is the number of characters we generate.
243 if (prefix
.length () + 6 + suffix
.length () > max_length
)
246 if (suffix
.charAt(0) == '.')
248 suffix
= suffix
.substring(0, suf_len
);
249 if (prefix
.length () + 6 + suf_len
> max_length
)
250 prefix
= prefix
.substring(0, max_length
- 6 - suf_len
);
253 // We don't care about the name because we set it later.
254 File ret
= new File ("");
255 // How many times should we try? We choose 100.
256 for (int i
= 0; i
< 100; ++i
)
259 String t
= "ZZZZZZ" + nextValue ();
260 String l
= prefix
+ t
.substring(t
.length() - 6) + suffix
;
263 desc
.open (l
, FileDescriptor
.WRITE
| FileDescriptor
.EXCL
);
267 catch (IOException _
)
272 throw new IOException ("couldn't make temp file");
275 public static File
createTempFile (String prefix
, String suffix
)
278 return createTempFile (prefix
, suffix
, null);
281 private final native boolean performRenameTo (File dest
);
282 public boolean renameTo (File dest
)
284 SecurityManager s
= System
.getSecurityManager();
287 // FIXME: JCL doesn't specify which path to check. We check the
288 // source since we can canonicalize it.
289 s
.checkWrite(safeCanonicalPath());
291 return performRenameTo (dest
);
294 public static final String pathSeparator
295 = System
.getProperty("path.separator");
296 public static final char pathSeparatorChar
= pathSeparator
.charAt(0);
297 public static final String separator
= System
.getProperty("file.separator");
298 public static final char separatorChar
= separator
.charAt(0);
300 private static final String tmpdir
= System
.getProperty("java.io.tmpdir");
305 // We keep a counter for use by createTempFile. We choose the first
306 // value randomly to try to avoid clashes with other VMs.
307 private static long counter
= Double
.doubleToLongBits (Math
.random ());
309 // mkdirs() uses this to avoid repeated allocations.
310 private final void setPath (String n
)
316 private final String
checkRead ()
318 SecurityManager s
= System
.getSecurityManager();
319 String p
= safeCanonicalPath ();
327 // Return canonical path, or null.
328 private final String
safeCanonicalPath ()
333 p
= getCanonicalPath ();
335 catch (IOException x
)
342 // QUERY arguments to access function.
343 private final static int READ
= 0;
344 private final static int WRITE
= 1;
345 private final static int EXISTS
= 2;
347 // QUERY arguments to stat function.
348 private final static int DIRECTORY
= 0;
349 private final static int ISFILE
= 1;
351 // QUERY arguments to attr function.
352 private final static int MODIFIED
= 0;
353 private final static int LENGTH
= 1;
355 private final native long attr (String p
, int query
);
356 private final native boolean access (String p
, int query
);
357 private final native boolean stat (String p
, int query
);
This page took 0.0507 seconds and 4 git commands to generate.