changeset 5117:21aac3eb7c40

7143606: File.createTempFile should be improved for temporary files created by the platform. Summary: To use java.nio.file.Files.createTempFile() Reviewed-by: alanb, ksrini, prr
author sherman
date Tue, 28 Feb 2012 15:44:47 -0800
parents 988706e33b26
children 5fc4da84eff8
files src/macosx/classes/apple/applescript/AppleScriptEngine.java src/share/classes/com/sun/java/util/jar/pack/Driver.java src/share/classes/java/awt/Font.java src/share/classes/javax/imageio/stream/FileCacheImageInputStream.java src/share/classes/javax/imageio/stream/FileCacheImageOutputStream.java src/share/classes/javax/management/loading/MLet.java src/share/classes/sun/print/PSPrinterJob.java src/share/classes/sun/rmi/server/Activation.java src/share/classes/sun/tools/jar/Main.java src/share/classes/sun/tools/native2ascii/Main.java src/solaris/classes/sun/font/FcFontConfiguration.java src/solaris/classes/sun/print/UnixPrintJob.java src/solaris/classes/sun/print/UnixPrintServiceLookup.java
diffstat 13 files changed, 37 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/apple/applescript/AppleScriptEngine.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/macosx/classes/apple/applescript/AppleScriptEngine.java	Tue Feb 28 15:44:47 2012 -0800
@@ -26,6 +26,7 @@
 package apple.applescript;
 
 import java.io.*;
+import java.nio.file.Files;
 import java.util.*;
 import java.util.Map.Entry;
 
@@ -297,7 +298,7 @@
         File tmpfile;
         FileWriter tmpwrite;
         try {
-            tmpfile = File.createTempFile("AppleScriptEngine.", ".scpt");
+            tmpfile = Files.createTempFile("AppleScriptEngine.", ".scpt").toFile();
             tmpwrite = new FileWriter(tmpfile);
 
             // read in our input and write directly to tmpfile
--- a/src/share/classes/com/sun/java/util/jar/pack/Driver.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/com/sun/java/util/jar/pack/Driver.java	Tue Feb 28 15:44:47 2012 -0800
@@ -35,6 +35,7 @@
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.text.MessageFormat;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -385,9 +386,7 @@
         if ( base.getParentFile() == null && suffix.equals(".bak"))
             where = new File(".").getAbsoluteFile();
 
-
-        File f = File.createTempFile(prefix, suffix, where);
-        return f;
+        return Files.createTempFile(where.toPath(), prefix, suffix).toFile();
     }
 
     static private
--- a/src/share/classes/java/awt/Font.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/java/awt/Font.java	Tue Feb 28 15:44:47 2012 -0800
@@ -36,6 +36,7 @@
 import java.awt.peer.FontPeer;
 import java.io.*;
 import java.lang.ref.SoftReference;
+import java.nio.file.Files;
 import java.security.AccessController;
 import java.security.PrivilegedExceptionAction;
 import java.text.AttributedCharacterIterator.Attribute;
@@ -830,7 +831,7 @@
         File f = null;
         boolean hasPerm = false;
         try {
-            f = File.createTempFile("+~JT", ".tmp", null);
+            f = Files.createTempFile("+~JT", ".tmp").toFile();
             f.delete();
             f = null;
             hasPerm = true;
@@ -880,7 +881,7 @@
             final File tFile = AccessController.doPrivileged(
                 new PrivilegedExceptionAction<File>() {
                     public File run() throws IOException {
-                        return File.createTempFile("+~JF", ".tmp", null);
+                        return Files.createTempFile("+~JF", ".tmp").toFile();
                     }
                 }
             );
--- a/src/share/classes/javax/imageio/stream/FileCacheImageInputStream.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/javax/imageio/stream/FileCacheImageInputStream.java	Tue Feb 28 15:44:47 2012 -0800
@@ -29,6 +29,7 @@
 import java.io.InputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.file.Files;
 import com.sun.imageio.stream.StreamCloser;
 import com.sun.imageio.stream.StreamFinalizer;
 import sun.java2d.Disposer;
@@ -97,8 +98,11 @@
             throw new IllegalArgumentException("Not a directory!");
         }
         this.stream = stream;
-        this.cacheFile =
-            File.createTempFile("imageio", ".tmp", cacheDir);
+        if (cacheDir == null)
+            this.cacheFile = Files.createTempFile("imageio", ".tmp").toFile();
+        else
+            this.cacheFile = Files.createTempFile(cacheDir.toPath(), "imageio", ".tmp")
+                                  .toFile();
         this.cache = new RandomAccessFile(cacheFile, "rw");
 
         this.closeAction = StreamCloser.createCloseAction(this);
--- a/src/share/classes/javax/imageio/stream/FileCacheImageOutputStream.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/javax/imageio/stream/FileCacheImageOutputStream.java	Tue Feb 28 15:44:47 2012 -0800
@@ -29,6 +29,7 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.RandomAccessFile;
+import java.nio.file.Files;
 import com.sun.imageio.stream.StreamCloser;
 
 /**
@@ -83,8 +84,11 @@
             throw new IllegalArgumentException("Not a directory!");
         }
         this.stream = stream;
-        this.cacheFile =
-            File.createTempFile("imageio", ".tmp", cacheDir);
+        if (cacheDir == null)
+            this.cacheFile = Files.createTempFile("imageio", ".tmp").toFile();
+        else
+            this.cacheFile = Files.createTempFile(cacheDir.toPath(), "imageio", ".tmp")
+                                  .toFile();
         this.cache = new RandomAccessFile(cacheFile, "rw");
 
         this.closeAction = StreamCloser.createCloseAction(this);
--- a/src/share/classes/javax/management/loading/MLet.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/javax/management/loading/MLet.java	Tue Feb 28 15:44:47 2012 -0800
@@ -44,6 +44,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLStreamHandlerFactory;
+import java.nio.file.Files;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
@@ -1160,8 +1161,9 @@
                  try {
                      File directory = new File(libraryDirectory);
                      directory.mkdirs();
-                     File file = File.createTempFile(libname + ".", null,
-                             directory);
+                     File file = Files.createTempFile(directory.toPath(),
+                                                      libname + ".", null)
+                                      .toFile();
                      file.deleteOnExit();
                      FileOutputStream fileOutput = new FileOutputStream(file);
                      try {
--- a/src/share/classes/sun/print/PSPrinterJob.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/sun/print/PSPrinterJob.java	Tue Feb 28 15:44:47 2012 -0800
@@ -93,6 +93,7 @@
 import java.nio.charset.*;
 import java.nio.CharBuffer;
 import java.nio.ByteBuffer;
+import java.nio.file.Files;
 
 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
 import java.lang.reflect.Method;
@@ -655,7 +656,7 @@
                      * is not removed for some reason, request that it is
                      * removed when the VM exits.
                      */
-                    spoolFile = File.createTempFile("javaprint", ".ps", null);
+                    spoolFile = Files.createTempFile("javaprint", ".ps").toFile();
                     spoolFile.deleteOnExit();
 
                 result = new FileOutputStream(spoolFile);
--- a/src/share/classes/sun/rmi/server/Activation.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/sun/rmi/server/Activation.java	Tue Feb 28 15:44:47 2012 -0800
@@ -43,6 +43,7 @@
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
+import java.nio.file.Files;
 import java.nio.channels.Channel;
 import java.nio.channels.ServerSocketChannel;
 import java.rmi.AccessException;
@@ -1937,7 +1938,7 @@
                     new PrivilegedExceptionAction<Void>() {
                         public Void run() throws IOException {
                             File file =
-                                File.createTempFile("rmid-err", null, null);
+                                Files.createTempFile("rmid-err", null).toFile();
                             PrintStream errStream =
                                 new PrintStream(new FileOutputStream(file));
                             System.setErr(errStream);
--- a/src/share/classes/sun/tools/jar/Main.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/sun/tools/jar/Main.java	Tue Feb 28 15:44:47 2012 -0800
@@ -137,7 +137,7 @@
         File dir = file.getParentFile();
         if (dir == null)
             dir = new File(".");
-        return File.createTempFile("jartmp", null, dir);
+        return Files.createTempFile(dir.toPath(), "jartmp", null).toFile();
     }
 
     private boolean ok;
--- a/src/share/classes/sun/tools/native2ascii/Main.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/share/classes/sun/tools/native2ascii/Main.java	Tue Feb 28 15:44:47 2012 -0800
@@ -71,6 +71,7 @@
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.Charset;
 import java.nio.charset.IllegalCharsetNameException;
+import java.nio.file.Files;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.UnsupportedCharsetException;
 import sun.tools.native2ascii.A2NFilter;
@@ -240,9 +241,7 @@
             if (tempDir == null)
                 tempDir = new File(System.getProperty("user.dir"));
 
-            tempFile = File.createTempFile("_N2A",
-                                           ".TMP",
-                                            tempDir);
+            tempFile = Files.createTempFile(tempDir.toPath(), "_N2A", ".TMP").toFile();
             tempFile.deleteOnExit();
 
             try {
@@ -292,9 +291,7 @@
             File tempDir = f.getParentFile();
             if (tempDir == null)
                 tempDir = new File(System.getProperty("user.dir"));
-            tempFile =  File.createTempFile("_N2A",
-                                            ".TMP",
-                                            tempDir);
+            tempFile =  Files.createTempFile(tempDir.toPath(), "_N2A", ".TMP").toFile();
             tempFile.deleteOnExit();
 
             try {
--- a/src/solaris/classes/sun/font/FcFontConfiguration.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/solaris/classes/sun/font/FcFontConfiguration.java	Tue Feb 28 15:44:47 2012 -0800
@@ -33,6 +33,7 @@
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.charset.Charset;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Properties;
@@ -387,7 +388,7 @@
             File fcInfoFile = getFcInfoFile();
             File dir = fcInfoFile.getParentFile();
             dir.mkdirs();
-            File tempFile = File.createTempFile("fcinfo", null, dir);
+            File tempFile = Files.createTempFile(dir.toPath(), "fcinfo", null).toFile();
             FileOutputStream fos = new FileOutputStream(tempFile);
             props.store(fos,
                       "JDK Font Configuration Generated File: *Do Not Edit*");
--- a/src/solaris/classes/sun/print/UnixPrintJob.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/solaris/classes/sun/print/UnixPrintJob.java	Tue Feb 28 15:44:47 2012 -0800
@@ -40,6 +40,7 @@
 import java.io.IOException;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
+import java.nio.file.Files;
 import java.util.Vector;
 
 import javax.print.CancelablePrintJob;
@@ -936,7 +937,7 @@
                      * is not removed for some reason, request that it is
                      * removed when the VM exits.
                      */
-                    spoolFile = File.createTempFile("javaprint", ".ps", null);
+                    spoolFile = Files.createTempFile("javaprint", ".ps").toFile();
                     spoolFile.deleteOnExit();
                 }
                 result = new FileOutputStream(spoolFile);
--- a/src/solaris/classes/sun/print/UnixPrintServiceLookup.java	Mon Feb 27 20:18:28 2012 +0200
+++ b/src/solaris/classes/sun/print/UnixPrintServiceLookup.java	Tue Feb 28 15:44:47 2012 -0800
@@ -51,6 +51,7 @@
 import java.io.File;
 import java.io.FileReader;
 import java.net.URL;
+import java.nio.file.Files;
 
 /*
  * Remind: This class uses solaris commands. We also need a linux
@@ -714,7 +715,7 @@
 
                         Process proc;
                         BufferedReader bufferedReader = null;
-                        File f = File.createTempFile("prn","xc");
+                        File f = Files.createTempFile("prn","xc").toFile();
                         cmd[2] = cmd[2]+">"+f.getAbsolutePath();
 
                         proc = Runtime.getRuntime().exec(cmd);