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

Print this page

        

@@ -1908,11 +1908,11 @@
             } else {
                 n = Math.abs(n);
             }
             String name = prefix + Long.toString(n) + suffix;
             File f = new File(dir, name);
-            if (!name.equals(f.getName()))
+            if (!name.equals(f.getName()) || f.isInvalid())
                 throw new IOException("Unable to create temporary file");
             return f;
         }
     }
 

@@ -1994,23 +1994,30 @@
         if (suffix == null)
             suffix = ".tmp";
 
         File tmpdir = (directory != null) ? directory
                                           : TempDirectory.location();
+        SecurityManager sm = System.getSecurityManager();
         File f;
-        try {
             do {
                 f = TempDirectory.generateFile(prefix, suffix, tmpdir);
-            } while (f.exists());
-            if (!f.createNewFile())
-                throw new IOException("Unable to create temporary file");
+
+            if (sm != null) {
+                try {
+                    sm.checkWrite(f.getPath());
         } catch (SecurityException se) {
             // don't reveal temporary directory location
             if (directory == null)
                 throw new SecurityException("Unable to create temporary file");
             throw se;
         }
+            }
+        } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0);
+
+        if (!fs.createFileExclusively(f.getPath()))
+            throw new IOException("Unable to create temporary file");
+
         return f;
     }
 
     /**
      * Creates an empty file in the default temporary-file directory, using