src/java.base/share/classes/java/io/File.java
changeset 59263 f34ad283fcd6
parent 58288 48e480e56aad
equal deleted inserted replaced
59262:06970ab040d4 59263:f34ad283fcd6
    27 
    27 
    28 import java.net.URI;
    28 import java.net.URI;
    29 import java.net.URL;
    29 import java.net.URL;
    30 import java.net.MalformedURLException;
    30 import java.net.MalformedURLException;
    31 import java.net.URISyntaxException;
    31 import java.net.URISyntaxException;
       
    32 import java.nio.file.FileStore;
    32 import java.nio.file.FileSystems;
    33 import java.nio.file.FileSystems;
    33 import java.nio.file.Path;
    34 import java.nio.file.Path;
    34 import java.security.SecureRandom;
    35 import java.security.SecureRandom;
    35 import java.util.ArrayList;
    36 import java.util.ArrayList;
    36 import java.util.List;
    37 import java.util.List;
  1796 
  1797 
  1797     /* -- Disk usage -- */
  1798     /* -- Disk usage -- */
  1798 
  1799 
  1799     /**
  1800     /**
  1800      * Returns the size of the partition <a href="#partName">named</a> by this
  1801      * Returns the size of the partition <a href="#partName">named</a> by this
  1801      * abstract pathname.
  1802      * abstract pathname. If the total number of bytes in the partition is
       
  1803      * greater than {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be
       
  1804      * returned.
  1802      *
  1805      *
  1803      * @return  The size, in bytes, of the partition or {@code 0L} if this
  1806      * @return  The size, in bytes, of the partition or {@code 0L} if this
  1804      *          abstract pathname does not name a partition
  1807      *          abstract pathname does not name a partition or if the size
       
  1808      *          cannot be obtained
  1805      *
  1809      *
  1806      * @throws  SecurityException
  1810      * @throws  SecurityException
  1807      *          If a security manager has been installed and it denies
  1811      *          If a security manager has been installed and it denies
  1808      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
  1812      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
  1809      *          or its {@link SecurityManager#checkRead(String)} method denies
  1813      *          or its {@link SecurityManager#checkRead(String)} method denies
  1810      *          read access to the file named by this abstract pathname
  1814      *          read access to the file named by this abstract pathname
  1811      *
  1815      *
  1812      * @since  1.6
  1816      * @since  1.6
       
  1817      * @see FileStore#getTotalSpace
  1813      */
  1818      */
  1814     public long getTotalSpace() {
  1819     public long getTotalSpace() {
  1815         SecurityManager sm = System.getSecurityManager();
  1820         SecurityManager sm = System.getSecurityManager();
  1816         if (sm != null) {
  1821         if (sm != null) {
  1817             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1822             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1818             sm.checkRead(path);
  1823             sm.checkRead(path);
  1819         }
  1824         }
  1820         if (isInvalid()) {
  1825         if (isInvalid()) {
  1821             return 0L;
  1826             return 0L;
  1822         }
  1827         }
  1823         return fs.getSpace(this, FileSystem.SPACE_TOTAL);
  1828         long space = fs.getSpace(this, FileSystem.SPACE_TOTAL);
       
  1829         return space >= 0L ? space : Long.MAX_VALUE;
  1824     }
  1830     }
  1825 
  1831 
  1826     /**
  1832     /**
  1827      * Returns the number of unallocated bytes in the partition <a
  1833      * Returns the number of unallocated bytes in the partition <a
  1828      * href="#partName">named</a> by this abstract path name.
  1834      * href="#partName">named</a> by this abstract path name.  If the
       
  1835      * number of unallocated bytes in the partition is greater than
       
  1836      * {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
  1829      *
  1837      *
  1830      * <p> The returned number of unallocated bytes is a hint, but not
  1838      * <p> The returned number of unallocated bytes is a hint, but not
  1831      * a guarantee, that it is possible to use most or any of these
  1839      * a guarantee, that it is possible to use most or any of these
  1832      * bytes.  The number of unallocated bytes is most likely to be
  1840      * bytes.  The number of unallocated bytes is most likely to be
  1833      * accurate immediately after this call.  It is likely to be made
  1841      * accurate immediately after this call.  It is likely to be made
  1835      * on the system outside of this virtual machine.  This method
  1843      * on the system outside of this virtual machine.  This method
  1836      * makes no guarantee that write operations to this file system
  1844      * makes no guarantee that write operations to this file system
  1837      * will succeed.
  1845      * will succeed.
  1838      *
  1846      *
  1839      * @return  The number of unallocated bytes on the partition or {@code 0L}
  1847      * @return  The number of unallocated bytes on the partition or {@code 0L}
  1840      *          if the abstract pathname does not name a partition.  This
  1848      *          if the abstract pathname does not name a partition or if this
  1841      *          value will be less than or equal to the total file system size
  1849      *          number cannot be obtained.  This value will be less than or
  1842      *          returned by {@link #getTotalSpace}.
  1850      *          equal to the total file system size returned by
       
  1851      *          {@link #getTotalSpace}.
  1843      *
  1852      *
  1844      * @throws  SecurityException
  1853      * @throws  SecurityException
  1845      *          If a security manager has been installed and it denies
  1854      *          If a security manager has been installed and it denies
  1846      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
  1855      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
  1847      *          or its {@link SecurityManager#checkRead(String)} method denies
  1856      *          or its {@link SecurityManager#checkRead(String)} method denies
  1848      *          read access to the file named by this abstract pathname
  1857      *          read access to the file named by this abstract pathname
  1849      *
  1858      *
  1850      * @since  1.6
  1859      * @since  1.6
       
  1860      * @see FileStore#getUnallocatedSpace
  1851      */
  1861      */
  1852     public long getFreeSpace() {
  1862     public long getFreeSpace() {
  1853         SecurityManager sm = System.getSecurityManager();
  1863         SecurityManager sm = System.getSecurityManager();
  1854         if (sm != null) {
  1864         if (sm != null) {
  1855             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1865             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1856             sm.checkRead(path);
  1866             sm.checkRead(path);
  1857         }
  1867         }
  1858         if (isInvalid()) {
  1868         if (isInvalid()) {
  1859             return 0L;
  1869             return 0L;
  1860         }
  1870         }
  1861         return fs.getSpace(this, FileSystem.SPACE_FREE);
  1871         long space = fs.getSpace(this, FileSystem.SPACE_FREE);
       
  1872         return space >= 0L ? space : Long.MAX_VALUE;
  1862     }
  1873     }
  1863 
  1874 
  1864     /**
  1875     /**
  1865      * Returns the number of bytes available to this virtual machine on the
  1876      * Returns the number of bytes available to this virtual machine on the
  1866      * partition <a href="#partName">named</a> by this abstract pathname.  When
  1877      * partition <a href="#partName">named</a> by this abstract pathname.  If
  1867      * possible, this method checks for write permissions and other operating
  1878      * the number of available bytes in the partition is greater than
  1868      * system restrictions and will therefore usually provide a more accurate
  1879      * {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
  1869      * estimate of how much new data can actually be written than {@link
  1880      * When possible, this method checks for write permissions and other
  1870      * #getFreeSpace}.
  1881      * operating system restrictions and will therefore usually provide a more
       
  1882      * accurate estimate of how much new data can actually be written than
       
  1883      * {@link #getFreeSpace}.
  1871      *
  1884      *
  1872      * <p> The returned number of available bytes is a hint, but not a
  1885      * <p> The returned number of available bytes is a hint, but not a
  1873      * guarantee, that it is possible to use most or any of these bytes.  The
  1886      * guarantee, that it is possible to use most or any of these bytes.  The
  1874      * number of available bytes is most likely to be accurate immediately
  1887      * number of available bytes is most likely to be accurate immediately
  1875      * after this call.  It is likely to be made inaccurate by any external
  1888      * after this call.  It is likely to be made inaccurate by any external
  1876      * I/O operations including those made on the system outside of this
  1889      * I/O operations including those made on the system outside of this
  1877      * virtual machine.  This method makes no guarantee that write operations
  1890      * virtual machine.  This method makes no guarantee that write operations
  1878      * to this file system will succeed.
  1891      * to this file system will succeed.
  1879      *
  1892      *
  1880      * @return  The number of available bytes on the partition or {@code 0L}
  1893      * @return  The number of available bytes on the partition or {@code 0L}
  1881      *          if the abstract pathname does not name a partition.  On
  1894      *          if the abstract pathname does not name a partition or if this
  1882      *          systems where this information is not available, this method
  1895      *          number cannot be obtained.  On systems where this information
  1883      *          will be equivalent to a call to {@link #getFreeSpace}.
  1896      *          is not available, this method will be equivalent to a call to
       
  1897      *          {@link #getFreeSpace}.
  1884      *
  1898      *
  1885      * @throws  SecurityException
  1899      * @throws  SecurityException
  1886      *          If a security manager has been installed and it denies
  1900      *          If a security manager has been installed and it denies
  1887      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
  1901      *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
  1888      *          or its {@link SecurityManager#checkRead(String)} method denies
  1902      *          or its {@link SecurityManager#checkRead(String)} method denies
  1889      *          read access to the file named by this abstract pathname
  1903      *          read access to the file named by this abstract pathname
  1890      *
  1904      *
  1891      * @since  1.6
  1905      * @since  1.6
       
  1906      * @see FileStore#getUsableSpace
  1892      */
  1907      */
  1893     public long getUsableSpace() {
  1908     public long getUsableSpace() {
  1894         SecurityManager sm = System.getSecurityManager();
  1909         SecurityManager sm = System.getSecurityManager();
  1895         if (sm != null) {
  1910         if (sm != null) {
  1896             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1911             sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
  1897             sm.checkRead(path);
  1912             sm.checkRead(path);
  1898         }
  1913         }
  1899         if (isInvalid()) {
  1914         if (isInvalid()) {
  1900             return 0L;
  1915             return 0L;
  1901         }
  1916         }
  1902         return fs.getSpace(this, FileSystem.SPACE_USABLE);
  1917         long space = fs.getSpace(this, FileSystem.SPACE_USABLE);
       
  1918         return space >= 0L ? space : Long.MAX_VALUE;
  1903     }
  1919     }
  1904 
  1920 
  1905     /* -- Temporary files -- */
  1921     /* -- Temporary files -- */
  1906 
  1922 
  1907     private static class TempDirectory {
  1923     private static class TempDirectory {