< prev index next >

src/java.base/share/classes/java/io/File.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.net.URI;
  29 import java.net.URL;
  30 import java.net.MalformedURLException;
  31 import java.net.URISyntaxException;
  32 import java.util.List;
  33 import java.util.ArrayList;
  34 import java.security.AccessController;
  35 import java.security.SecureRandom;
  36 import java.nio.file.Path;
  37 import java.nio.file.FileSystems;
  38 import sun.security.action.GetPropertyAction;
  39 
  40 /**
  41  * An abstract representation of file and directory pathnames.
  42  *
  43  * <p> User interfaces and operating systems use system-dependent <em>pathname
  44  * strings</em> to name files and directories.  This class presents an
  45  * abstract, system-independent view of hierarchical pathnames.  An
  46  * <em>abstract pathname</em> has two components:
  47  *
  48  * <ol>
  49  * <li> An optional system-dependent <em>prefix</em> string,
  50  *      such as a disk-drive specifier, <code>"/"</code>&nbsp;for the UNIX root
  51  *      directory, or <code>"\\\\"</code>&nbsp;for a Microsoft Windows UNC pathname, and
  52  * <li> A sequence of zero or more string <em>names</em>.
  53  * </ol>
  54  *


1879      * @since  1.6
1880      */
1881     public long getUsableSpace() {
1882         SecurityManager sm = System.getSecurityManager();
1883         if (sm != null) {
1884             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1885             sm.checkRead(path);
1886         }
1887         if (isInvalid()) {
1888             return 0L;
1889         }
1890         return fs.getSpace(this, FileSystem.SPACE_USABLE);
1891     }
1892 
1893     /* -- Temporary files -- */
1894 
1895     private static class TempDirectory {
1896         private TempDirectory() { }
1897 
1898         // temporary directory location
1899         private static final File tmpdir = new File(AccessController
1900             .doPrivileged(new GetPropertyAction("java.io.tmpdir")));
1901         static File location() {
1902             return tmpdir;
1903         }
1904 
1905         // file name generation
1906         private static final SecureRandom random = new SecureRandom();
1907         static File generateFile(String prefix, String suffix, File dir)
1908             throws IOException
1909         {
1910             long n = random.nextLong();
1911             if (n == Long.MIN_VALUE) {
1912                 n = 0;      // corner case
1913             } else {
1914                 n = Math.abs(n);
1915             }
1916 
1917             // Use only the file name from the supplied prefix
1918             prefix = (new File(prefix)).getName();
1919 
1920             String name = prefix + Long.toString(n) + suffix;




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.net.URI;
  29 import java.net.URL;
  30 import java.net.MalformedURLException;
  31 import java.net.URISyntaxException;
  32 import java.util.List;
  33 import java.util.ArrayList;

  34 import java.security.SecureRandom;
  35 import java.nio.file.Path;
  36 import java.nio.file.FileSystems;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 /**
  40  * An abstract representation of file and directory pathnames.
  41  *
  42  * <p> User interfaces and operating systems use system-dependent <em>pathname
  43  * strings</em> to name files and directories.  This class presents an
  44  * abstract, system-independent view of hierarchical pathnames.  An
  45  * <em>abstract pathname</em> has two components:
  46  *
  47  * <ol>
  48  * <li> An optional system-dependent <em>prefix</em> string,
  49  *      such as a disk-drive specifier, <code>"/"</code>&nbsp;for the UNIX root
  50  *      directory, or <code>"\\\\"</code>&nbsp;for a Microsoft Windows UNC pathname, and
  51  * <li> A sequence of zero or more string <em>names</em>.
  52  * </ol>
  53  *


1878      * @since  1.6
1879      */
1880     public long getUsableSpace() {
1881         SecurityManager sm = System.getSecurityManager();
1882         if (sm != null) {
1883             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
1884             sm.checkRead(path);
1885         }
1886         if (isInvalid()) {
1887             return 0L;
1888         }
1889         return fs.getSpace(this, FileSystem.SPACE_USABLE);
1890     }
1891 
1892     /* -- Temporary files -- */
1893 
1894     private static class TempDirectory {
1895         private TempDirectory() { }
1896 
1897         // temporary directory location
1898         private static final File tmpdir = new File(
1899                 GetPropertyAction.getProperty("java.io.tmpdir"));
1900         static File location() {
1901             return tmpdir;
1902         }
1903 
1904         // file name generation
1905         private static final SecureRandom random = new SecureRandom();
1906         static File generateFile(String prefix, String suffix, File dir)
1907             throws IOException
1908         {
1909             long n = random.nextLong();
1910             if (n == Long.MIN_VALUE) {
1911                 n = 0;      // corner case
1912             } else {
1913                 n = Math.abs(n);
1914             }
1915 
1916             // Use only the file name from the supplied prefix
1917             prefix = (new File(prefix)).getName();
1918 
1919             String name = prefix + Long.toString(n) + suffix;


< prev index next >