< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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


1945                     excess = prefixLength + nusLength + suffixLength - nameMax;
1946                 }
1947 
1948                 if (excess > 0 && excess <= nusLength - 5) {
1949                     // Attempt to shorten the random character string length
1950                     // to no less than 5
1951                     nusLength = shortenSubName(nusLength, excess, 5);
1952                 }
1953 
1954                 StringBuilder sb =
1955                     new StringBuilder(prefixLength + nusLength + suffixLength);
1956                 sb.append(prefixLength < prefix.length() ?
1957                     prefix.substring(0, prefixLength) : prefix);
1958                 sb.append(nusLength < nus.length() ?
1959                     nus.substring(0, nusLength) : nus);
1960                 sb.append(suffixLength < suffix.length() ?
1961                     suffix.substring(0, suffixLength) : suffix);
1962                 name = sb.toString();
1963             }
1964 



1965             File f = new File(dir, name);
1966             if (!name.equals(f.getName()) || f.isInvalid()) {
1967                 if (System.getSecurityManager() != null)
1968                     throw new IOException("Unable to create temporary file");
1969                 else
1970                     throw new IOException("Unable to create temporary file, "
1971                         + name);
1972             }
1973             return f;
1974         }
1975     }
1976 
1977     /**
1978      * <p> Creates a new empty file in the specified directory, using the
1979      * given prefix and suffix strings to generate its name.  If this method
1980      * returns successfully then it is guaranteed that:
1981      *
1982      * <ol>
1983      * <li> The file denoted by the returned abstract pathname did not exist
1984      *      before this method was invoked, and


   1 /*
   2  * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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


1945                     excess = prefixLength + nusLength + suffixLength - nameMax;
1946                 }
1947 
1948                 if (excess > 0 && excess <= nusLength - 5) {
1949                     // Attempt to shorten the random character string length
1950                     // to no less than 5
1951                     nusLength = shortenSubName(nusLength, excess, 5);
1952                 }
1953 
1954                 StringBuilder sb =
1955                     new StringBuilder(prefixLength + nusLength + suffixLength);
1956                 sb.append(prefixLength < prefix.length() ?
1957                     prefix.substring(0, prefixLength) : prefix);
1958                 sb.append(nusLength < nus.length() ?
1959                     nus.substring(0, nusLength) : nus);
1960                 sb.append(suffixLength < suffix.length() ?
1961                     suffix.substring(0, suffixLength) : suffix);
1962                 name = sb.toString();
1963             }
1964 
1965             // Normalize the path component
1966             name = fs.normalize(name);
1967 
1968             File f = new File(dir, name);
1969             if (!name.equals(f.getName()) || f.isInvalid()) {
1970                 if (System.getSecurityManager() != null)
1971                     throw new IOException("Unable to create temporary file");
1972                 else
1973                     throw new IOException("Unable to create temporary file, "
1974                         + name);
1975             }
1976             return f;
1977         }
1978     }
1979 
1980     /**
1981      * <p> Creates a new empty file in the specified directory, using the
1982      * given prefix and suffix strings to generate its name.  If this method
1983      * returns successfully then it is guaranteed that:
1984      *
1985      * <ol>
1986      * <li> The file denoted by the returned abstract pathname did not exist
1987      *      before this method was invoked, and


< prev index next >