< prev index next >

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

Print this page




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.privilegedGetProperty("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 
1911             // Use only the file name from the supplied prefix
1912             prefix = (new File(prefix)).getName();
1913             String name = prefix + Long.toUnsignedString(n) + suffix;
























1914             File f = new File(dir, name);
1915             if (!name.equals(f.getName()) || f.isInvalid()) {
1916                 if (System.getSecurityManager() != null)
1917                     throw new IOException("Unable to create temporary file");
1918                 else
1919                     throw new IOException("Unable to create temporary file, " + f);

1920             }
1921             return f;
1922         }
1923     }
1924 
1925     /**
1926      * <p> Creates a new empty file in the specified directory, using the
1927      * given prefix and suffix strings to generate its name.  If this method
1928      * returns successfully then it is guaranteed that:
1929      *
1930      * <ol>
1931      * <li> The file denoted by the returned abstract pathname did not exist
1932      *      before this method was invoked, and
1933      * <li> Neither this method nor any of its variants will return the same
1934      *      abstract pathname again in the current invocation of the virtual
1935      *      machine.
1936      * </ol>
1937      *
1938      * This method provides only part of a temporary-file facility.  To arrange
1939      * for a file created by this method to be deleted automatically, use the




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.privilegedGetProperty("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         private static String shortenSubName(String subName, int excess,
1907             int nameMin) {
1908             long newLength = Math.max(nameMin, subName.length() - excess);
1909             if (newLength < subName.length()) {
1910                 subName = subName.substring(0, (int)newLength);
1911             }
1912             return subName;
1913         }
1914         static File generateFile(String prefix, String suffix, File dir)
1915             throws IOException
1916         {
1917             long n = random.nextLong();
1918             String nus = Long.toUnsignedString(n);
1919 
1920             // Use only the file name from the supplied prefix
1921             prefix = (new File(prefix)).getName();
1922             String name = prefix + nus + suffix;
1923 
1924             // Shorten the name if it exceeds the maximum path component length
1925             long nameMax = fs.getNameMax(dir.getPath());
1926             int excess = name.length() - (int)nameMax;
1927             if (excess > 0) {
1928                 // Attempt to shorten the prefix length to no less then 3
1929                 prefix = shortenSubName(prefix, excess, 3);
1930                 name = prefix + nus + suffix;
1931                 excess = name.length() - (int)nameMax;
1932             }
1933             if (excess > 0) {
1934                 // Attempt to shorten the suffix length to no less than
1935                 // 0 or 4 depending on whether it begins with a dot ('.')
1936                 suffix = shortenSubName(suffix, excess,
1937                     suffix.indexOf(".") == 0 ? 4 : 0);
1938                 name = prefix + nus + suffix;
1939                 excess = name.length() - (int)nameMax;
1940             }
1941             if (excess > 0 && excess <= nus.length() - 5) {
1942                 // Attempt to shorten the random string length to no less than 5
1943                 nus = shortenSubName(nus, excess, 5);
1944                 name = prefix + nus + suffix;
1945             }
1946 
1947             File f = new File(dir, name);
1948             if (!name.equals(f.getName()) || f.isInvalid()) {
1949                 if (System.getSecurityManager() != null)
1950                     throw new IOException("Unable to create temporary file");
1951                 else
1952                     throw new IOException("Unable to create temporary file, "
1953                         + f);
1954             }
1955             return f;
1956         }
1957     }
1958 
1959     /**
1960      * <p> Creates a new empty file in the specified directory, using the
1961      * given prefix and suffix strings to generate its name.  If this method
1962      * returns successfully then it is guaranteed that:
1963      *
1964      * <ol>
1965      * <li> The file denoted by the returned abstract pathname did not exist
1966      *      before this method was invoked, and
1967      * <li> Neither this method nor any of its variants will return the same
1968      *      abstract pathname again in the current invocation of the virtual
1969      *      machine.
1970      * </ol>
1971      *
1972      * This method provides only part of a temporary-file facility.  To arrange
1973      * for a file created by this method to be deleted automatically, use the


< prev index next >