File.java 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. /* File.java -- Class representing a file on disk
  2. Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.io;
  33. import gnu.classpath.SystemProperties;
  34. import gnu.java.lang.CPStringBuilder;
  35. import java.net.MalformedURLException;
  36. import java.net.URI;
  37. import java.net.URISyntaxException;
  38. import java.net.URL;
  39. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  40. * "The Java Language Specification", ISBN 0-201-63451-1
  41. * Status: Complete to version 1.3.
  42. */
  43. /**
  44. * This class represents a file or directory on a local disk. It provides
  45. * facilities for dealing with a variety of systems that use various
  46. * types of path separators ("/" versus "\", for example). It also
  47. * contains method useful for creating and deleting files and directories.
  48. *
  49. * @author Aaron M. Renn (arenn@urbanophile.com)
  50. * @author Tom Tromey (tromey@cygnus.com)
  51. */
  52. public class File implements Serializable, Comparable<File>
  53. {
  54. private static final long serialVersionUID = 301077366599181567L;
  55. /**
  56. * This is the path separator string for the current host. This field
  57. * contains the value of the <code>file.separator</code> system property.
  58. * An example separator string would be "/" on the GNU system.
  59. */
  60. public static final String separator = SystemProperties.getProperty("file.separator");
  61. private static final String dupSeparator = separator + separator;
  62. /**
  63. * This is the first character of the file separator string. On many
  64. * hosts (for example, on the GNU system), this represents the entire
  65. * separator string. The complete separator string is obtained from the
  66. * <code>file.separator</code>system property.
  67. */
  68. public static final char separatorChar = separator.charAt(0);
  69. /**
  70. * This is the string that is used to separate the host name from the
  71. * path name in paths that include the host name. It is the value of
  72. * the <code>path.separator</code> system property.
  73. */
  74. public static final String pathSeparator
  75. = SystemProperties.getProperty("path.separator");
  76. /**
  77. * This is the first character of the string used to separate the host name
  78. * from the path name in paths that include a host. The separator string
  79. * is taken from the <code>path.separator</code> system property.
  80. */
  81. public static final char pathSeparatorChar = pathSeparator.charAt(0);
  82. /**
  83. * This is the path to the file set when the object is created. It
  84. * may be an absolute or relative path name.
  85. */
  86. private String path;
  87. /**
  88. * The time (millisecond), when the last temporary file was created.
  89. */
  90. private static long last_tmp;
  91. /**
  92. * The number of files, created during the current millisecond.
  93. */
  94. private static int n_created;
  95. /**
  96. * This method tests whether or not the current thread is allowed to
  97. * to read the file pointed to by this object. This will be true if and
  98. * and only if 1) the file exists and 2) the <code>SecurityManager</code>
  99. * (if any) allows access to the file via it's <code>checkRead</code>
  100. * method 3) the file is readable.
  101. *
  102. * @return <code>true</code> if reading is allowed,
  103. * <code>false</code> otherwise
  104. *
  105. * @exception SecurityException If the <code>SecurityManager</code>
  106. * does not allow access to the file
  107. */
  108. public boolean canRead()
  109. {
  110. // Test for existence. This also does the SecurityManager check
  111. if (!exists())
  112. return false;
  113. return VMFile.canRead(path);
  114. }
  115. /**
  116. * This method test whether or not the current thread is allowed to
  117. * write to this object. This will be true if and only if 1) The
  118. * <code>SecurityManager</code> (if any) allows write access to the
  119. * file and 2) The file exists and 3) The file is writable. To determine
  120. * whether or not a non-existent file can be created, check the parent
  121. * directory for write access.
  122. *
  123. * @return <code>true</code> if writing is allowed, <code>false</code>
  124. * otherwise
  125. *
  126. * @exception SecurityException If the <code>SecurityManager</code>
  127. * does not allow access to the file
  128. */
  129. public boolean canWrite()
  130. {
  131. // First do a SecurityCheck before doing anything else.
  132. checkWrite();
  133. // Test for existence. This is required by the spec
  134. if (! VMFile.exists(path))
  135. return false;
  136. if (VMFile.isDirectory(path))
  137. return VMFile.canWriteDirectory(path);
  138. else
  139. return VMFile.canWrite(path);
  140. }
  141. /**
  142. * This method tests whether or not the current thread is allowed to
  143. * to execute the file pointed to by this object. This will be true if and
  144. * and only if 1) the file exists and 2) the <code>SecurityManager</code>
  145. * (if any) allows access to the file via it's <code>checkExec</code>
  146. * method 3) the file is executable.
  147. *
  148. * @return <code>true</code> if execution is allowed,
  149. * <code>false</code> otherwise
  150. *
  151. * @exception SecurityException If the <code>SecurityManager</code>
  152. * does not allow access to the file
  153. */
  154. public boolean canExecute()
  155. {
  156. if (!VMFile.exists(path))
  157. return false;
  158. checkExec();
  159. return VMFile.canExecute(path);
  160. }
  161. /**
  162. * This method creates a new file of zero length with the same name as
  163. * the path of this <code>File</code> object if an only if that file
  164. * does not already exist.
  165. * <p>
  166. * A <code>SecurityManager.checkWrite</code> check is done prior
  167. * to performing this action.
  168. *
  169. * @return <code>true</code> if the file was created, <code>false</code> if
  170. * the file alread existed.
  171. *
  172. * @exception IOException If an I/O error occurs
  173. * @exception SecurityException If the <code>SecurityManager</code> will
  174. * not allow this operation to be performed.
  175. *
  176. * @since 1.2
  177. */
  178. public boolean createNewFile() throws IOException
  179. {
  180. checkWrite();
  181. return VMFile.create(path);
  182. }
  183. /**
  184. * This method deletes the file represented by this object. If this file
  185. * is a directory, it must be empty in order for the delete to succeed.
  186. *
  187. * @return <code>true</code> if the file was deleted, <code>false</code>
  188. * otherwise
  189. *
  190. * @exception SecurityException If deleting of the file is not allowed
  191. */
  192. public synchronized boolean delete()
  193. {
  194. SecurityManager s = System.getSecurityManager();
  195. if (s != null)
  196. s.checkDelete(path);
  197. return VMFile.delete(path);
  198. }
  199. /**
  200. * This method tests two <code>File</code> objects for equality by
  201. * comparing the path of the specified <code>File</code> against the path
  202. * of this object. The two objects are equal if an only if 1) The
  203. * argument is not null 2) The argument is a <code>File</code> object and
  204. * 3) The path of the <code>File</code>argument is equal to the path
  205. * of this object.
  206. * <p>
  207. * The paths of the files are determined by calling the
  208. * <code>getPath()</code>
  209. * method on each object.
  210. *
  211. * @return <code>true</code> if the two objects are equal,
  212. * <code>false</code> otherwise.
  213. */
  214. public boolean equals(Object obj)
  215. {
  216. if (! (obj instanceof File))
  217. return false;
  218. File other = (File) obj;
  219. if (VMFile.IS_CASE_SENSITIVE)
  220. return path.equals(other.path);
  221. else
  222. return path.equalsIgnoreCase(other.path);
  223. }
  224. /**
  225. * This method tests whether or not the file represented by the object
  226. * actually exists on the filesystem.
  227. *
  228. * @return <code>true</code> if the file exists, <code>false</code>otherwise.
  229. *
  230. * @exception SecurityException If reading of the file is not permitted
  231. */
  232. public boolean exists()
  233. {
  234. checkRead();
  235. return VMFile.exists(path);
  236. }
  237. /**
  238. * This method initializes a new <code>File</code> object to represent
  239. * a file with the specified path.
  240. *
  241. * @param name The path name of the file
  242. */
  243. public File(String name)
  244. {
  245. path = normalizePath (name);
  246. }
  247. // Remove duplicate and redundant separator characters.
  248. private String normalizePath(String p)
  249. {
  250. // On Windows, convert any '/' to '\'. This appears to be the same logic
  251. // that Sun's Win32 Java performs.
  252. if (separatorChar == '\\')
  253. {
  254. p = p.replace ('/', '\\');
  255. // We have to special case the "\c:" prefix.
  256. if (p.length() > 2 && p.charAt(0) == '\\' &&
  257. ((p.charAt(1) >= 'a' && p.charAt(1) <= 'z') ||
  258. (p.charAt(1) >= 'A' && p.charAt(1) <= 'Z')) &&
  259. p.charAt(2) == ':')
  260. p = p.substring(1);
  261. }
  262. int dupIndex = p.indexOf(dupSeparator);
  263. int plen = p.length();
  264. // Special case: permit Windows UNC path prefix.
  265. if (dupSeparator.equals("\\\\") && dupIndex == 0)
  266. dupIndex = p.indexOf(dupSeparator, 1);
  267. if (dupIndex == -1)
  268. {
  269. // Ignore trailing separator (though on Windows "a:\", for
  270. // example, is a valid and minimal path).
  271. if (plen > 1 && p.charAt (plen - 1) == separatorChar)
  272. {
  273. if (! (separatorChar == '\\' && ((plen == 3 && p.charAt(1) == ':')
  274. || (plen == 2 && p.charAt(0) == separatorChar))))
  275. return p.substring (0, plen - 1);
  276. }
  277. else
  278. return p;
  279. }
  280. CPStringBuilder newpath = new CPStringBuilder(plen);
  281. int last = 0;
  282. while (dupIndex != -1)
  283. {
  284. newpath.append(p.substring(last, dupIndex));
  285. // Ignore the duplicate path characters.
  286. while (p.charAt(dupIndex) == separatorChar)
  287. {
  288. dupIndex++;
  289. if (dupIndex == plen)
  290. {
  291. if ((separatorChar == '\\'
  292. && newpath.length() == 2
  293. && newpath.charAt(1) == ':')
  294. || (separatorChar != '\\' && newpath.length() == 0))
  295. {
  296. newpath.append(separatorChar);
  297. }
  298. return newpath.toString();
  299. }
  300. }
  301. newpath.append(separatorChar);
  302. last = dupIndex;
  303. dupIndex = p.indexOf(dupSeparator, last);
  304. }
  305. // Again, ignore possible trailing separator (except special cases
  306. // like "a:\" on Windows).
  307. int end;
  308. if (plen > 1 && p.charAt (plen - 1) == separatorChar)
  309. {
  310. if (separatorChar == '\\'
  311. && ((plen == 3 && p.charAt(1) == ':')
  312. || (plen == 2 && p.charAt(0) == separatorChar)))
  313. end = plen;
  314. else
  315. end = plen - 1;
  316. }
  317. else
  318. end = plen;
  319. newpath.append(p.substring(last, end));
  320. return newpath.toString();
  321. }
  322. /**
  323. * This method initializes a new <code>File</code> object to represent
  324. * a file in the specified named directory. The path name to the file
  325. * will be the directory name plus the separator string plus the file
  326. * name. If the directory path name ends in the separator string, another
  327. * separator string will still be appended.
  328. *
  329. * @param dirPath The path to the directory the file resides in
  330. * @param name The name of the file
  331. */
  332. public File(String dirPath, String name)
  333. {
  334. if (name == null)
  335. throw new NullPointerException();
  336. if (dirPath != null)
  337. {
  338. if (dirPath.length() > 0)
  339. {
  340. // Try to be smart about the number of separator characters.
  341. if (dirPath.charAt(dirPath.length() - 1) == separatorChar
  342. || name.length() == 0)
  343. path = normalizePath(dirPath + name);
  344. else
  345. path = normalizePath(dirPath + separatorChar + name);
  346. }
  347. else
  348. {
  349. // If dirPath is empty, use a system dependant
  350. // default prefix.
  351. // Note that the leading separators in name have
  352. // to be chopped off, to prevent them forming
  353. // a UNC prefix on Windows.
  354. if (separatorChar == '\\' /* TODO use ON_WINDOWS */)
  355. {
  356. int skip = 0;
  357. while(name.length() > skip
  358. && (name.charAt(skip) == separatorChar
  359. || name.charAt(skip) == '/'))
  360. {
  361. skip++;
  362. }
  363. name = name.substring(skip);
  364. }
  365. path = normalizePath(separatorChar + name);
  366. }
  367. }
  368. else
  369. path = normalizePath(name);
  370. }
  371. /**
  372. * This method initializes a new <code>File</code> object to represent
  373. * a file in the specified directory. If the <code>directory</code>
  374. * argument is <code>null</code>, the file is assumed to be in the
  375. * current directory as specified by the <code>user.dir</code> system
  376. * property
  377. *
  378. * @param directory The directory this file resides in
  379. * @param name The name of the file
  380. */
  381. public File(File directory, String name)
  382. {
  383. this (directory == null ? null : directory.path, name);
  384. }
  385. /**
  386. * This method initializes a new <code>File</code> object to represent
  387. * a file corresponding to the specified <code>file:</code> protocol URI.
  388. *
  389. * @param uri The URI
  390. * @throws IllegalArgumentException if the URI is not hierarchical
  391. */
  392. public File(URI uri)
  393. {
  394. if (uri == null)
  395. throw new NullPointerException("uri is null");
  396. if (!uri.getScheme().equals("file"))
  397. throw new IllegalArgumentException("invalid uri protocol");
  398. String name = uri.getPath();
  399. if (name == null)
  400. throw new IllegalArgumentException("URI \"" + uri
  401. + "\" is not hierarchical");
  402. path = normalizePath(name);
  403. }
  404. /**
  405. * This method returns the path of this file as an absolute path name.
  406. * If the path name is already absolute, then it is returned. Otherwise
  407. * the value returned is the current directory plus the separatory
  408. * string plus the path of the file. The current directory is determined
  409. * from the <code>user.dir</code> system property.
  410. *
  411. * @return The absolute path of this file
  412. */
  413. public String getAbsolutePath()
  414. {
  415. if (isAbsolute())
  416. return path;
  417. else
  418. return VMFile.getAbsolutePath(path);
  419. }
  420. /**
  421. * This method returns a <code>File</code> object representing the
  422. * absolute path of this object.
  423. *
  424. * @return A <code>File</code> with the absolute path of the object.
  425. *
  426. * @since 1.2
  427. */
  428. public File getAbsoluteFile()
  429. {
  430. return new File(getAbsolutePath());
  431. }
  432. /**
  433. * This method returns a canonical representation of the pathname of
  434. * this file. The actual form of the canonical representation is
  435. * system-dependent. On the GNU system, conversion to canonical
  436. * form involves the removal of redundant separators, references to
  437. * "." and "..", and symbolic links.
  438. * <p>
  439. * Note that this method, unlike the other methods which return path
  440. * names, can throw an IOException. This is because native method
  441. * might be required in order to resolve the canonical path
  442. *
  443. * @exception IOException If an error occurs
  444. */
  445. public String getCanonicalPath() throws IOException
  446. {
  447. // On Windows, getAbsolutePath might end up calling us, so we
  448. // have to special case that call to avoid infinite recursion.
  449. if (separatorChar == '\\' && path.length() == 2 &&
  450. ((path.charAt(0) >= 'a' && path.charAt(0) <= 'z') ||
  451. (path.charAt(0) >= 'A' && path.charAt(0) <= 'Z')) &&
  452. path.charAt(1) == ':')
  453. {
  454. return VMFile.toCanonicalForm(path);
  455. }
  456. // Call getAbsolutePath first to make sure that we do the
  457. // current directory handling, because the native code
  458. // may have a different idea of the current directory.
  459. return VMFile.toCanonicalForm(getAbsolutePath());
  460. }
  461. /**
  462. * This method returns a <code>File</code> object representing the
  463. * canonical path of this object.
  464. *
  465. * @return A <code>File</code> instance representing the canonical path of
  466. * this object.
  467. *
  468. * @exception IOException If an error occurs.
  469. *
  470. * @since 1.2
  471. */
  472. public File getCanonicalFile() throws IOException
  473. {
  474. return new File(getCanonicalPath());
  475. }
  476. /**
  477. * This method returns the name of the file. This is everything in the
  478. * complete path of the file after the last instance of the separator
  479. * string.
  480. *
  481. * @return The file name
  482. */
  483. public String getName()
  484. {
  485. return VMFile.getName(path);
  486. }
  487. /**
  488. * This method returns a <code>String</code> the represents this file's
  489. * parent. <code>null</code> is returned if the file has no parent. The
  490. * parent is determined via a simple operation which removes the name
  491. * after the last file separator character, as determined by the platform.
  492. *
  493. * @return The parent directory of this file
  494. */
  495. public String getParent()
  496. {
  497. String prefix = null;
  498. int nameSeqIndex = 0;
  499. if (path.equals(""))
  500. return null;
  501. // The "prefix", if present, is the leading "/" on UNIX and
  502. // either the drive specifier (e.g. "C:") or the leading "\\"
  503. // of a UNC network path on Windows.
  504. if (separatorChar == '/' && path.charAt (0) == '/')
  505. {
  506. prefix = "/";
  507. nameSeqIndex = 1;
  508. }
  509. else if (separatorChar == '\\' && path.length() > 1)
  510. {
  511. if ((path.charAt (0) == '\\' && path.charAt (1) == '\\')
  512. || (((path.charAt (0) >= 'a' && path.charAt (0) <= 'z')
  513. || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z'))
  514. && path.charAt (1) == ':'))
  515. {
  516. prefix = path.substring (0, 2);
  517. nameSeqIndex = 2;
  518. }
  519. }
  520. // According to the JDK docs, the returned parent path is the
  521. // portion of the name sequence before the last separator
  522. // character, if found, prefixed by the prefix, otherwise null.
  523. if (nameSeqIndex < path.length())
  524. {
  525. String nameSeq = path.substring (nameSeqIndex, path.length());
  526. int last = nameSeq.lastIndexOf (separatorChar);
  527. if (last == -1)
  528. return prefix;
  529. else if (last == (nameSeq.length() - 1))
  530. // Note: The path would not have a trailing separator
  531. // except for cases like "C:\" on Windows (see
  532. // normalizePath( )), where Sun's JRE 1.4 returns null.
  533. return null;
  534. else if (last == 0)
  535. last++;
  536. if (prefix != null)
  537. return prefix + nameSeq.substring (0, last);
  538. else
  539. return nameSeq.substring (0, last);
  540. }
  541. else
  542. // Sun's JRE 1.4 returns null if the prefix is the only
  543. // component of the path - so "/" gives null on UNIX and
  544. // "C:", "\\", etc. return null on Windows.
  545. return null;
  546. }
  547. /**
  548. * This method returns a <code>File</code> object representing the parent
  549. * file of this one.
  550. *
  551. * @return a <code>File</code> for the parent of this object.
  552. * <code>null</code>
  553. * will be returned if this object does not have a parent.
  554. *
  555. * @since 1.2
  556. */
  557. public File getParentFile()
  558. {
  559. String parent = getParent();
  560. return parent != null ? new File(parent) : null;
  561. }
  562. /**
  563. * Returns the path name that represents this file. May be a relative
  564. * or an absolute path name
  565. *
  566. * @return The pathname of this file
  567. */
  568. public String getPath()
  569. {
  570. return path;
  571. }
  572. /**
  573. * This method returns a hash code representing this file. It is the
  574. * hash code of the path of this file (as returned by <code>getPath()</code>)
  575. * exclusived or-ed with the value 1234321.
  576. *
  577. * @return The hash code for this object
  578. */
  579. public int hashCode()
  580. {
  581. if (VMFile.IS_CASE_SENSITIVE)
  582. return path.hashCode() ^ 1234321;
  583. else
  584. return path.toLowerCase().hashCode() ^ 1234321;
  585. }
  586. /**
  587. * This method returns true if this object represents an absolute file
  588. * path and false if it does not. The definition of an absolute path varies
  589. * by system. As an example, on GNU systems, a path is absolute if it starts
  590. * with a "/".
  591. *
  592. * @return <code>true</code> if this object represents an absolute
  593. * file name, <code>false</code> otherwise.
  594. */
  595. public boolean isAbsolute()
  596. {
  597. return VMFile.isAbsolute(path);
  598. }
  599. /**
  600. * This method tests whether or not the file represented by this object
  601. * is a directory. In order for this method to return <code>true</code>,
  602. * the file represented by this object must exist and be a directory.
  603. *
  604. * @return <code>true</code> if this file is a directory, <code>false</code>
  605. * otherwise
  606. *
  607. * @exception SecurityException If reading of the file is not permitted
  608. */
  609. public boolean isDirectory()
  610. {
  611. checkRead();
  612. return VMFile.isDirectory(path);
  613. }
  614. /**
  615. * This method tests whether or not the file represented by this object
  616. * is a "plain" file. A file is a plain file if and only if it 1) Exists,
  617. * 2) Is not a directory or other type of special file.
  618. *
  619. * @return <code>true</code> if this is a plain file, <code>false</code>
  620. * otherwise
  621. *
  622. * @exception SecurityException If reading of the file is not permitted
  623. */
  624. public boolean isFile()
  625. {
  626. checkRead();
  627. return VMFile.isFile(path);
  628. }
  629. /**
  630. * This method tests whether or not this file represents a "hidden" file.
  631. * On GNU systems, a file is hidden if its name begins with a "."
  632. * character. Files with these names are traditionally not shown with
  633. * directory listing tools.
  634. *
  635. * @return <code>true</code> if the file is hidden, <code>false</code>
  636. * otherwise.
  637. * @throws SecurityException if a security manager exists and denies
  638. * read access to this file.
  639. * @since 1.2
  640. */
  641. public boolean isHidden()
  642. {
  643. checkRead();
  644. return VMFile.isHidden(path);
  645. }
  646. /**
  647. * This method returns the last modification time of this file. The
  648. * time value returned is an abstract value that should not be interpreted
  649. * as a specified time value. It is only useful for comparing to other
  650. * such time values returned on the same system. In that case, the larger
  651. * value indicates a more recent modification time.
  652. * <p>
  653. * If the file does not exist, then a value of 0 is returned.
  654. *
  655. * @return The last modification time of the file
  656. *
  657. * @exception SecurityException If reading of the file is not permitted
  658. */
  659. public long lastModified()
  660. {
  661. checkRead();
  662. return VMFile.lastModified(path);
  663. }
  664. /**
  665. * This method returns the length of the file represented by this object,
  666. * or 0 if the specified file does not exist.
  667. *
  668. * @return The length of the file
  669. *
  670. * @exception SecurityException If reading of the file is not permitted
  671. */
  672. public long length()
  673. {
  674. checkRead();
  675. return VMFile.length(path);
  676. }
  677. /**
  678. * This method returns a array of <code>String</code>'s representing the
  679. * list of files is then directory represented by this object. If this
  680. * object represents a non-directory file or a non-existent file, then
  681. * <code>null</code> is returned. The list of files will not contain
  682. * any names such as "." or ".." which indicate the current or parent
  683. * directory. Also, the names are not guaranteed to be sorted.
  684. * <p>
  685. * In this form of the <code>list()</code> method, a filter is specified
  686. * that allows the caller to control which files are returned in the
  687. * list. The <code>FilenameFilter</code> specified is called for each
  688. * file returned to determine whether or not that file should be included
  689. * in the list.
  690. * <p>
  691. * A <code>SecurityManager</code> check is made prior to reading the
  692. * directory. If read access to the directory is denied, an exception
  693. * will be thrown.
  694. *
  695. * @param filter An object which will identify files to exclude from
  696. * the directory listing.
  697. *
  698. * @return An array of files in the directory, or <code>null</code>
  699. * if this object does not represent a valid directory.
  700. *
  701. * @exception SecurityException If read access is not allowed to the
  702. * directory by the <code>SecurityManager</code>
  703. */
  704. public String[] list(FilenameFilter filter)
  705. {
  706. checkRead();
  707. if (!exists() || !isDirectory())
  708. return null;
  709. // Get the list of files
  710. String files[] = VMFile.list(path);
  711. // Check if an error occured in listInternal().
  712. // This is an unreadable directory, pretend there is nothing inside.
  713. if (files == null)
  714. return new String[0];
  715. if (filter == null)
  716. return files;
  717. // Apply the filter
  718. int count = 0;
  719. for (int i = 0; i < files.length; i++)
  720. {
  721. if (filter.accept(this, files[i]))
  722. ++count;
  723. else
  724. files[i] = null;
  725. }
  726. String[] retfiles = new String[count];
  727. count = 0;
  728. for (int i = 0; i < files.length; i++)
  729. if (files[i] != null)
  730. retfiles[count++] = files[i];
  731. return retfiles;
  732. }
  733. /**
  734. * This method returns a array of <code>String</code>'s representing the
  735. * list of files is then directory represented by this object. If this
  736. * object represents a non-directory file or a non-existent file, then
  737. * <code>null</code> is returned. The list of files will not contain
  738. * any names such as "." or ".." which indicate the current or parent
  739. * directory. Also, the names are not guaranteed to be sorted.
  740. * <p>
  741. * A <code>SecurityManager</code> check is made prior to reading the
  742. * directory. If read access to the directory is denied, an exception
  743. * will be thrown.
  744. *
  745. * @return An array of files in the directory, or <code>null</code> if
  746. * this object does not represent a valid directory.
  747. *
  748. * @exception SecurityException If read access is not allowed to the
  749. * directory by the <code>SecurityManager</code>
  750. */
  751. public String[] list()
  752. {
  753. return list(null);
  754. }
  755. /**
  756. * This method returns an array of <code>File</code> objects representing
  757. * all the files in the directory represented by this object. If this
  758. * object does not represent a directory, <code>null</code> is returned.
  759. * Each of the returned <code>File</code> object is constructed with this
  760. * object as its parent.
  761. * <p>
  762. * A <code>SecurityManager</code> check is made prior to reading the
  763. * directory. If read access to the directory is denied, an exception
  764. * will be thrown.
  765. *
  766. * @return An array of <code>File</code> objects for this directory.
  767. *
  768. * @exception SecurityException If the <code>SecurityManager</code> denies
  769. * access to this directory.
  770. *
  771. * @since 1.2
  772. */
  773. public File[] listFiles()
  774. {
  775. return listFiles((FilenameFilter) null);
  776. }
  777. /**
  778. * This method returns an array of <code>File</code> objects representing
  779. * all the files in the directory represented by this object. If this
  780. * object does not represent a directory, <code>null</code> is returned.
  781. * Each of the returned <code>File</code> object is constructed with this
  782. * object as its parent.
  783. * <p>
  784. * In this form of the <code>listFiles()</code> method, a filter is specified
  785. * that allows the caller to control which files are returned in the
  786. * list. The <code>FilenameFilter</code> specified is called for each
  787. * file returned to determine whether or not that file should be included
  788. * in the list.
  789. * <p>
  790. * A <code>SecurityManager</code> check is made prior to reading the
  791. * directory. If read access to the directory is denied, an exception
  792. * will be thrown.
  793. *
  794. * @return An array of <code>File</code> objects for this directory.
  795. *
  796. * @exception SecurityException If the <code>SecurityManager</code> denies
  797. * access to this directory.
  798. *
  799. * @since 1.2
  800. */
  801. public File[] listFiles(FilenameFilter filter)
  802. {
  803. String[] filelist = list(filter);
  804. if (filelist == null)
  805. return null;
  806. File[] fobjlist = new File [filelist.length];
  807. for (int i = 0; i < filelist.length; i++)
  808. fobjlist [i] = new File(this, filelist [i]);
  809. return fobjlist;
  810. }
  811. /**
  812. * This method returns an array of <code>File</code> objects representing
  813. * all the files in the directory represented by this object. If this
  814. * object does not represent a directory, <code>null</code> is returned.
  815. * Each of the returned <code>File</code> object is constructed with this
  816. * object as its parent.
  817. * <p>
  818. * In this form of the <code>listFiles()</code> method, a filter is specified
  819. * that allows the caller to control which files are returned in the
  820. * list. The <code>FileFilter</code> specified is called for each
  821. * file returned to determine whether or not that file should be included
  822. * in the list.
  823. * <p>
  824. * A <code>SecurityManager</code> check is made prior to reading the
  825. * directory. If read access to the directory is denied, an exception
  826. * will be thrown.
  827. *
  828. * @return An array of <code>File</code> objects for this directory.
  829. *
  830. * @exception SecurityException If the <code>SecurityManager</code> denies
  831. * access to this directory.
  832. *
  833. * @since 1.2
  834. */
  835. public File[] listFiles(FileFilter filter)
  836. {
  837. File[] fobjlist = listFiles((FilenameFilter) null);
  838. if (fobjlist == null)
  839. return null;
  840. if (filter == null)
  841. return fobjlist;
  842. int count = 0;
  843. for (int i = 0; i < fobjlist.length; i++)
  844. if (filter.accept(fobjlist[i]) == true)
  845. ++count;
  846. File[] final_list = new File[count];
  847. count = 0;
  848. for (int i = 0; i < fobjlist.length; i++)
  849. if (filter.accept(fobjlist[i]) == true)
  850. {
  851. final_list[count] = fobjlist[i];
  852. ++count;
  853. }
  854. return final_list;
  855. }
  856. /**
  857. * This method returns a <code>String</code> that is the path name of the
  858. * file as returned by <code>getPath</code>.
  859. *
  860. * @return A <code>String</code> representation of this file
  861. */
  862. public String toString()
  863. {
  864. return path;
  865. }
  866. /**
  867. * @return A <code>URI</code> for this object.
  868. */
  869. public URI toURI()
  870. {
  871. String abspath = getAbsolutePath();
  872. if (isDirectory() || path.equals(""))
  873. abspath = abspath + separatorChar;
  874. if (separatorChar == '\\')
  875. abspath = separatorChar + abspath;
  876. try
  877. {
  878. return new URI("file", null, null, -1,
  879. abspath.replace(separatorChar, '/'),
  880. null, null);
  881. }
  882. catch (URISyntaxException use)
  883. {
  884. // Can't happen.
  885. throw (InternalError) new InternalError("Unconvertible file: "
  886. + this).initCause(use);
  887. }
  888. }
  889. /**
  890. * This method returns a <code>URL</code> with the <code>file:</code>
  891. * protocol that represents this file. The exact form of this URL is
  892. * system dependent.
  893. *
  894. * @return A <code>URL</code> for this object.
  895. *
  896. * @exception MalformedURLException If the URL cannot be created
  897. * successfully.
  898. */
  899. public URL toURL() throws MalformedURLException
  900. {
  901. return VMFile.toURL(this);
  902. }
  903. /**
  904. * This method creates a directory for the path represented by this object.
  905. *
  906. * @return <code>true</code> if the directory was created,
  907. * <code>false</code> otherwise
  908. *
  909. * @exception SecurityException If write access is not allowed to this file
  910. */
  911. public boolean mkdir()
  912. {
  913. checkWrite();
  914. return VMFile.mkdir(path);
  915. }
  916. /**
  917. * This method creates a directory for the path represented by this file.
  918. * It will also create any intervening parent directories if necessary.
  919. *
  920. * @return <code>true</code> if the directory was created,
  921. * <code>false</code> otherwise
  922. *
  923. * @exception SecurityException If write access is not allowed to this file
  924. */
  925. public boolean mkdirs()
  926. {
  927. String parent = getParent();
  928. if (parent == null)
  929. {
  930. return mkdir();
  931. }
  932. File f = new File(parent);
  933. if (!f.exists())
  934. {
  935. boolean rc = f.mkdirs();
  936. if (rc == false)
  937. return false;
  938. }
  939. return mkdir();
  940. }
  941. /**
  942. * This method creates a temporary file in the specified directory. If
  943. * the directory name is null, then this method uses the system temporary
  944. * directory. The files created are guaranteed not to currently exist and
  945. * the same file name will never be used twice in the same virtual
  946. * machine instance.
  947. * The system temporary directory is determined by examinging the
  948. * <code>java.io.tmpdir</code> system property.
  949. * <p>
  950. * The <code>prefix</code> parameter is a sequence of at least three
  951. * characters that are used as the start of the generated filename. The
  952. * <code>suffix</code> parameter is a sequence of characters that is used
  953. * to terminate the file name. This parameter may be <code>null</code>
  954. * and if it is, the suffix defaults to ".tmp".
  955. * <p>
  956. * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code>
  957. * method is used to verify that this operation is permitted.
  958. *
  959. * @param prefix The character prefix to use in generating the path name.
  960. * @param suffix The character suffix to use in generating the path name.
  961. * @param directory The directory to create the file in, or
  962. * <code>null</code> for the default temporary directory
  963. *
  964. * @exception IllegalArgumentException If the patterns is not valid
  965. * @exception SecurityException If there is no permission to perform
  966. * this operation
  967. * @exception IOException If an error occurs
  968. *
  969. * @since 1.2
  970. */
  971. public static synchronized File createTempFile(String prefix, String suffix,
  972. File directory)
  973. throws IOException
  974. {
  975. // Grab the system temp directory if necessary
  976. if (directory == null)
  977. {
  978. String dirname = System.getProperty("java.io.tmpdir");
  979. if (dirname == null)
  980. throw new IOException("Cannot determine system temporary directory");
  981. directory = new File(dirname);
  982. if (! VMFile.exists(directory.path))
  983. throw new IOException("System temporary directory "
  984. + directory.getName() + " does not exist.");
  985. if (! VMFile.isDirectory(directory.path))
  986. throw new IOException("System temporary directory "
  987. + directory.getName()
  988. + " is not really a directory.");
  989. }
  990. // Check if prefix is at least 3 characters long
  991. if (prefix.length() < 3)
  992. throw new IllegalArgumentException("Prefix too short: " + prefix);
  993. // Set default value of suffix
  994. if (suffix == null)
  995. suffix = ".tmp";
  996. // Now identify a file name and make sure it doesn't exist.
  997. File file;
  998. if (!VMFile.IS_DOS_8_3)
  999. {
  1000. do
  1001. {
  1002. long now = System.currentTimeMillis();
  1003. if (now > last_tmp)
  1004. {
  1005. // The last temporary file was created more than 1 ms ago.
  1006. last_tmp = now;
  1007. n_created = 0;
  1008. }
  1009. else
  1010. n_created++;
  1011. String name = Long.toHexString(now);
  1012. if (n_created > 0)
  1013. name += '_'+Integer.toHexString(n_created);
  1014. String filename = prefix + name + suffix;
  1015. file = new File(directory, filename);
  1016. }
  1017. while (VMFile.exists(file.path));
  1018. }
  1019. else
  1020. {
  1021. // make sure prefix is not longer than 7 characters
  1022. if (prefix.length() >= 8)
  1023. throw new IllegalArgumentException("Prefix too long: " + prefix + "(valid length 3..7)");
  1024. long mask = 0x000000ffffFFFFL >> (prefix.length() * 4);
  1025. do
  1026. {
  1027. int n = (int) (System.currentTimeMillis() & mask);
  1028. String filename = prefix + java.lang.Integer.toHexString(n) + suffix;
  1029. file = new File(directory, filename);
  1030. }
  1031. while (VMFile.exists(file.path));
  1032. }
  1033. // Verify that we are allowed to create this file
  1034. SecurityManager sm = System.getSecurityManager();
  1035. if (sm != null)
  1036. sm.checkWrite(file.getAbsolutePath());
  1037. // Now create the file and return our file object
  1038. // XXX - FIXME race condition.
  1039. VMFile.create(file.getAbsolutePath());
  1040. return file;
  1041. }
  1042. /**
  1043. * This method sets the owner's read permission for the File represented by
  1044. * this object.
  1045. *
  1046. * It is the same as calling <code>setReadable(readable, true)</code>.
  1047. *
  1048. * @param <code>readable</code> <code>true</code> to set read permission,
  1049. * <code>false</code> to unset the read permission.
  1050. * @return <code>true</code> if the file permissions are changed,
  1051. * <code>false</code> otherwise.
  1052. * @exception SecurityException If write access of the file is not permitted.
  1053. * @see #setReadable(boolean, boolean)
  1054. * @since 1.6
  1055. */
  1056. public boolean setReadable(boolean readable)
  1057. {
  1058. return setReadable(readable, true);
  1059. }
  1060. /**
  1061. * This method sets the read permissions for the File represented by
  1062. * this object.
  1063. *
  1064. * If <code>ownerOnly</code> is set to <code>true</code> then only the
  1065. * read permission bit for the owner of the file is changed.
  1066. *
  1067. * If <code>ownerOnly</code> is set to <code>false</code>, the file
  1068. * permissions are changed so that the file can be read by everyone.
  1069. *
  1070. * On unix like systems this sets the <code>user</code>, <code>group</code>
  1071. * and <code>other</code> read bits and is equal to call
  1072. * <code>chmod a+r</code> on the file.
  1073. *
  1074. * @param <code>readable</code> <code>true</code> to set read permission,
  1075. * <code>false</code> to unset the read permission.
  1076. * @param <code>ownerOnly</code> <code>true</code> to set read permission
  1077. * for owner only, <code>false</code> for all.
  1078. * @return <code>true</code> if the file permissions are changed,
  1079. * <code>false</code> otherwise.
  1080. * @exception SecurityException If write access of the file is not permitted.
  1081. * @see #setReadable(boolean)
  1082. * @since 1.6
  1083. */
  1084. public boolean setReadable(boolean readable, boolean ownerOnly)
  1085. {
  1086. checkWrite();
  1087. return VMFile.setReadable(path, readable, ownerOnly);
  1088. }
  1089. /**
  1090. * This method sets the owner's write permission for the File represented by
  1091. * this object.
  1092. *
  1093. * It is the same as calling <code>setWritable(readable, true)</code>.
  1094. *
  1095. * @param <code>writable</code> <code>true</code> to set write permission,
  1096. * <code>false</code> to unset write permission.
  1097. * @return <code>true</code> if the file permissions are changed,
  1098. * <code>false</code> otherwise.
  1099. * @exception SecurityException If write access of the file is not permitted.
  1100. * @see #setWritable(boolean, boolean)
  1101. * @since 1.6
  1102. */
  1103. public boolean setWritable(boolean writable)
  1104. {
  1105. return setWritable(writable, true);
  1106. }
  1107. /**
  1108. * This method sets the write permissions for the File represented by
  1109. * this object.
  1110. *
  1111. * If <code>ownerOnly</code> is set to <code>true</code> then only the
  1112. * write permission bit for the owner of the file is changed.
  1113. *
  1114. * If <code>ownerOnly</code> is set to <code>false</code>, the file
  1115. * permissions are changed so that the file can be written by everyone.
  1116. *
  1117. * On unix like systems this set the <code>user</code>, <code>group</code>
  1118. * and <code>other</code> write bits and is equal to call
  1119. * <code>chmod a+w</code> on the file.
  1120. *
  1121. * @param <code>writable</code> <code>true</code> to set write permission,
  1122. * <code>false</code> to unset write permission.
  1123. * @param <code>ownerOnly</code> <code>true</code> to set write permission
  1124. * for owner only, <code>false</code> for all.
  1125. * @return <code>true</code> if the file permissions are changed,
  1126. * <code>false</code> otherwise.
  1127. * @exception SecurityException If write access of the file is not permitted.
  1128. * @see #setWritable(boolean)
  1129. * @since 1.6
  1130. */
  1131. public boolean setWritable(boolean writable, boolean ownerOnly)
  1132. {
  1133. checkWrite();
  1134. return VMFile.setWritable(path, writable, ownerOnly);
  1135. }
  1136. /**
  1137. * This method sets the owner's execute permission for the File represented
  1138. * by this object.
  1139. *
  1140. * It is the same as calling <code>setExecutable(readable, true)</code>.
  1141. *
  1142. * @param <code>executable</code> <code>true</code> to set execute permission,
  1143. * <code>false</code> to unset execute permission.
  1144. * @return <code>true</code> if the file permissions are changed,
  1145. * <code>false</code> otherwise.
  1146. * @exception SecurityException If write access of the file is not permitted.
  1147. * @see #setExecutable(boolean, boolean)
  1148. * @since 1.6
  1149. */
  1150. public boolean setExecutable(boolean executable)
  1151. {
  1152. return setExecutable(executable, true);
  1153. }
  1154. /**
  1155. * This method sets the execute permissions for the File represented by
  1156. * this object.
  1157. *
  1158. * If <code>ownerOnly</code> is set to <code>true</code> then only the
  1159. * execute permission bit for the owner of the file is changed.
  1160. *
  1161. * If <code>ownerOnly</code> is set to <code>false</code>, the file
  1162. * permissions are changed so that the file can be executed by everyone.
  1163. *
  1164. * On unix like systems this set the <code>user</code>, <code>group</code>
  1165. * and <code>other</code> write bits and is equal to call
  1166. * <code>chmod a+x</code> on the file.
  1167. *
  1168. * @param <code>executable</code> <code>true</code> to set write permission,
  1169. * <code>false</code> to unset write permission.
  1170. * @param <code>ownerOnly</code> <code>true</code> to set write permission
  1171. * for owner only, <code>false</code> for all.
  1172. * @return <code>true</code> if the file permissions are changed,
  1173. * <code>false</code> otherwise.
  1174. * @exception SecurityException If write access of the file is not permitted.
  1175. * @see #setExecutable(boolean)
  1176. * @since 1.6
  1177. */
  1178. public boolean setExecutable(boolean executable, boolean ownerOnly)
  1179. {
  1180. checkWrite();
  1181. return VMFile.setExecutable(path, executable, ownerOnly);
  1182. }
  1183. /**
  1184. * Get the total space for the partition pointed by this file path, in bytes.
  1185. *
  1186. * @return the total number of bytes in this partition.
  1187. * @since 1.6
  1188. */
  1189. public long getTotalSpace()
  1190. {
  1191. // check security manager.
  1192. SecurityManager s = System.getSecurityManager();
  1193. if (s != null)
  1194. s.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1195. checkRead();
  1196. return VMFile.getTotalSpace(path);
  1197. }
  1198. /**
  1199. * Get the free space in the partition pointed by this file path, in bytes.
  1200. *
  1201. * @return the number of free bytes in this partition.
  1202. * @since 1.6
  1203. */
  1204. public long getFreeSpace()
  1205. {
  1206. // check security manager.
  1207. SecurityManager s = System.getSecurityManager();
  1208. if (s != null)
  1209. s.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1210. checkRead();
  1211. return VMFile.getFreeSpace(path);
  1212. }
  1213. /**
  1214. * Get the usable space in the partition pointed by this file path, in bytes.
  1215. * This is not necessarily the same as the number returned by
  1216. * {@link #getFreeSpace()}.
  1217. *
  1218. * <strong>Implementation note</strong>: Unlike the RI, on Linux and UNIX
  1219. * like systems this methods take into account the reserved space for the
  1220. * "root" user. This means that the returned results will be a little
  1221. * different if a normal user or root perform the query.
  1222. *
  1223. * Also, the bytes returned should be interpreted as an hint, and may be
  1224. * different at each call of this method or even right after the method
  1225. * returns.
  1226. *
  1227. * @return the number of usable bytes in this partition.
  1228. * @since 1.6
  1229. */
  1230. public long getUsableSpace()
  1231. {
  1232. // check security manager.
  1233. SecurityManager s = System.getSecurityManager();
  1234. if (s != null)
  1235. s.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1236. checkRead();
  1237. // root users can use the reserved extra space
  1238. String user = System.getProperty("user.name");
  1239. if (user != null && user.equals("root"))
  1240. return VMFile.getFreeSpace(path);
  1241. return VMFile.getUsableSpace(path);
  1242. }
  1243. /**
  1244. * This method sets the file represented by this object to be read only.
  1245. * A read only file or directory cannot be modified. Please note that
  1246. * GNU systems allow read only files to be deleted if the directory it
  1247. * is contained in is writable.
  1248. *
  1249. * @return <code>true</code> if the operation succeeded, <code>false</code>
  1250. * otherwise.
  1251. *
  1252. * @exception SecurityException If the <code>SecurityManager</code> does
  1253. * not allow this operation.
  1254. *
  1255. * @since 1.2
  1256. */
  1257. public boolean setReadOnly()
  1258. {
  1259. // Do a security check before trying to do anything else.
  1260. checkWrite();
  1261. // Test for existence.
  1262. if (! VMFile.exists(path))
  1263. return false;
  1264. return VMFile.setReadOnly(path);
  1265. }
  1266. /**
  1267. * This method returns an array of filesystem roots. Some operating systems
  1268. * have volume oriented filesystem. This method provides a mechanism for
  1269. * determining which volumes exist. GNU systems use a single hierarchical
  1270. * filesystem, so will have only one "/" filesystem root.
  1271. *
  1272. * @return An array of <code>File</code> objects for each filesystem root
  1273. * available.
  1274. *
  1275. * @since 1.2
  1276. */
  1277. public static File[] listRoots()
  1278. {
  1279. File[] roots = VMFile.listRoots();
  1280. SecurityManager s = System.getSecurityManager();
  1281. if (s != null)
  1282. {
  1283. // Only return roots to which the security manager permits read access.
  1284. int count = roots.length;
  1285. for (int i = 0; i < roots.length; i++)
  1286. {
  1287. try
  1288. {
  1289. s.checkRead (roots[i].path);
  1290. }
  1291. catch (SecurityException sx)
  1292. {
  1293. roots[i] = null;
  1294. count--;
  1295. }
  1296. }
  1297. if (count != roots.length)
  1298. {
  1299. File[] newRoots = new File[count];
  1300. int k = 0;
  1301. for (int i = 0; i < roots.length; i++)
  1302. {
  1303. if (roots[i] != null)
  1304. newRoots[k++] = roots[i];
  1305. }
  1306. roots = newRoots;
  1307. }
  1308. }
  1309. return roots;
  1310. }
  1311. /**
  1312. * This method creates a temporary file in the system temporary directory.
  1313. * The files created are guaranteed not to currently exist and the same file
  1314. * name will never be used twice in the same virtual machine instance. The
  1315. * system temporary directory is determined by examinging the
  1316. * <code>java.io.tmpdir</code> system property.
  1317. * <p>
  1318. * The <code>prefix</code> parameter is a sequence of at least three
  1319. * characters that are used as the start of the generated filename. The
  1320. * <code>suffix</code> parameter is a sequence of characters that is used
  1321. * to terminate the file name. This parameter may be <code>null</code>
  1322. * and if it is, the suffix defaults to ".tmp".
  1323. * <p>
  1324. * If a <code>SecurityManager</code> exists, then its <code>checkWrite</code>
  1325. * method is used to verify that this operation is permitted.
  1326. * <p>
  1327. * This method is identical to calling
  1328. * <code>createTempFile(prefix, suffix, null)</code>.
  1329. *
  1330. * @param prefix The character prefix to use in generating the path name.
  1331. * @param suffix The character suffix to use in generating the path name.
  1332. *
  1333. * @exception IllegalArgumentException If the prefix or suffix are not valid.
  1334. * @exception SecurityException If there is no permission to perform
  1335. * this operation
  1336. * @exception IOException If an error occurs
  1337. */
  1338. public static File createTempFile(String prefix, String suffix)
  1339. throws IOException
  1340. {
  1341. return createTempFile(prefix, suffix, null);
  1342. }
  1343. /**
  1344. * This method compares the specified <code>File</code> to this one
  1345. * to test for equality. It does this by comparing the canonical path names
  1346. * of the files.
  1347. * <p>
  1348. * The canonical paths of the files are determined by calling the
  1349. * <code>getCanonicalPath</code> method on each object.
  1350. * <p>
  1351. * This method returns a 0 if the specified <code>Object</code> is equal
  1352. * to this one, a negative value if it is less than this one
  1353. * a positive value if it is greater than this one.
  1354. *
  1355. * @return An integer as described above
  1356. *
  1357. * @since 1.2
  1358. */
  1359. public int compareTo(File other)
  1360. {
  1361. if (VMFile.IS_CASE_SENSITIVE)
  1362. return path.compareTo (other.path);
  1363. else
  1364. return path.compareToIgnoreCase (other.path);
  1365. }
  1366. /**
  1367. * This method renames the file represented by this object to the path
  1368. * of the file represented by the argument <code>File</code>.
  1369. *
  1370. * @param dest The <code>File</code> object representing the target name
  1371. *
  1372. * @return <code>true</code> if the rename succeeds, <code>false</code>
  1373. * otherwise.
  1374. *
  1375. * @exception SecurityException If write access is not allowed to the
  1376. * file by the <code>SecurityMananger</code>.
  1377. */
  1378. public synchronized boolean renameTo(File dest)
  1379. {
  1380. checkWrite();
  1381. dest.checkWrite();
  1382. // Call our native rename method
  1383. return VMFile.renameTo(path, dest.path);
  1384. }
  1385. /**
  1386. * This method sets the modification time on the file to the specified
  1387. * value. This is specified as the number of seconds since midnight
  1388. * on January 1, 1970 GMT.
  1389. *
  1390. * @param time The desired modification time.
  1391. *
  1392. * @return <code>true</code> if the operation succeeded, <code>false</code>
  1393. * otherwise.
  1394. *
  1395. * @exception IllegalArgumentException If the specified time is negative.
  1396. * @exception SecurityException If the <code>SecurityManager</code> will
  1397. * not allow this operation.
  1398. *
  1399. * @since 1.2
  1400. */
  1401. public boolean setLastModified(long time)
  1402. {
  1403. if (time < 0)
  1404. throw new IllegalArgumentException("Negative modification time: " + time);
  1405. checkWrite();
  1406. return VMFile.setLastModified(path, time);
  1407. }
  1408. private void checkWrite()
  1409. {
  1410. // Check the SecurityManager
  1411. SecurityManager s = System.getSecurityManager();
  1412. if (s != null)
  1413. s.checkWrite(path);
  1414. }
  1415. private void checkRead()
  1416. {
  1417. // Check the SecurityManager
  1418. SecurityManager s = System.getSecurityManager();
  1419. if (s != null)
  1420. s.checkRead(path);
  1421. }
  1422. private void checkExec()
  1423. {
  1424. // Check the SecurityManager
  1425. SecurityManager s = System.getSecurityManager();
  1426. if (s != null)
  1427. s.checkExec(path);
  1428. }
  1429. /**
  1430. * Calling this method requests that the file represented by this object
  1431. * be deleted when the virtual machine exits. Note that this request cannot
  1432. * be cancelled. Also, it will only be carried out if the virtual machine
  1433. * exits normally.
  1434. *
  1435. * @exception SecurityException If deleting of the file is not allowed
  1436. *
  1437. * @since 1.2
  1438. */
  1439. public void deleteOnExit()
  1440. {
  1441. // Check the SecurityManager
  1442. SecurityManager sm = System.getSecurityManager();
  1443. if (sm != null)
  1444. sm.checkDelete(path);
  1445. DeleteFileHelper.add(this);
  1446. }
  1447. private void writeObject(ObjectOutputStream oos) throws IOException
  1448. {
  1449. oos.defaultWriteObject();
  1450. oos.writeChar(separatorChar);
  1451. }
  1452. private void readObject(ObjectInputStream ois)
  1453. throws ClassNotFoundException, IOException
  1454. {
  1455. ois.defaultReadObject();
  1456. // If the file was from an OS with a different dir separator,
  1457. // fixup the path to use the separator on this OS.
  1458. char oldSeparatorChar = ois.readChar();
  1459. if (oldSeparatorChar != separatorChar)
  1460. path = path.replace(oldSeparatorChar, separatorChar);
  1461. }
  1462. } // class File