提交 f487c2de 编写于 作者: R robm

7143606: File.createTempFile should be improved for temporary files created by the platform.

Reviewed-by: sherman
上级 d702a7e5
/* /*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
package apple.applescript; package apple.applescript;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
...@@ -297,7 +298,7 @@ public class AppleScriptEngine implements ScriptEngine { ...@@ -297,7 +298,7 @@ public class AppleScriptEngine implements ScriptEngine {
File tmpfile; File tmpfile;
FileWriter tmpwrite; FileWriter tmpwrite;
try { try {
tmpfile = File.createTempFile("AppleScriptEngine.", ".scpt"); tmpfile = Files.createTempFile("AppleScriptEngine.", ".scpt").toFile();
tmpwrite = new FileWriter(tmpfile); tmpwrite = new FileWriter(tmpfile);
// read in our input and write directly to tmpfile // read in our input and write directly to tmpfile
......
/* /*
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -35,6 +35,7 @@ import java.io.InputStream; ...@@ -35,6 +35,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
...@@ -385,9 +386,7 @@ class Driver { ...@@ -385,9 +386,7 @@ class Driver {
if ( base.getParentFile() == null && suffix.equals(".bak")) if ( base.getParentFile() == null && suffix.equals(".bak"))
where = new File(".").getAbsoluteFile(); where = new File(".").getAbsoluteFile();
return Files.createTempFile(where.toPath(), prefix, suffix).toFile();
File f = File.createTempFile(prefix, suffix, where);
return f;
} }
static private static private
......
/* /*
* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -36,6 +36,7 @@ import java.awt.geom.Rectangle2D; ...@@ -36,6 +36,7 @@ import java.awt.geom.Rectangle2D;
import java.awt.peer.FontPeer; import java.awt.peer.FontPeer;
import java.io.*; import java.io.*;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.nio.file.Files;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.text.AttributedCharacterIterator.Attribute; import java.text.AttributedCharacterIterator.Attribute;
...@@ -831,7 +832,7 @@ public class Font implements java.io.Serializable ...@@ -831,7 +832,7 @@ public class Font implements java.io.Serializable
File f = null; File f = null;
boolean hasPerm = false; boolean hasPerm = false;
try { try {
f = File.createTempFile("+~JT", ".tmp", null); f = Files.createTempFile("+~JT", ".tmp").toFile();
f.delete(); f.delete();
f = null; f = null;
hasPerm = true; hasPerm = true;
...@@ -881,7 +882,7 @@ public class Font implements java.io.Serializable ...@@ -881,7 +882,7 @@ public class Font implements java.io.Serializable
final File tFile = AccessController.doPrivileged( final File tFile = AccessController.doPrivileged(
new PrivilegedExceptionAction<File>() { new PrivilegedExceptionAction<File>() {
public File run() throws IOException { public File run() throws IOException {
return File.createTempFile("+~JF", ".tmp", null); return Files.createTempFile("+~JF", ".tmp").toFile();
} }
} }
); );
......
/* /*
* Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -29,6 +29,7 @@ import java.io.File; ...@@ -29,6 +29,7 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.file.Files;
import com.sun.imageio.stream.StreamCloser; import com.sun.imageio.stream.StreamCloser;
import com.sun.imageio.stream.StreamFinalizer; import com.sun.imageio.stream.StreamFinalizer;
import sun.java2d.Disposer; import sun.java2d.Disposer;
...@@ -97,8 +98,11 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl { ...@@ -97,8 +98,11 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl {
throw new IllegalArgumentException("Not a directory!"); throw new IllegalArgumentException("Not a directory!");
} }
this.stream = stream; this.stream = stream;
this.cacheFile = if (cacheDir == null)
File.createTempFile("imageio", ".tmp", cacheDir); this.cacheFile = Files.createTempFile("imageio", ".tmp").toFile();
else
this.cacheFile = Files.createTempFile(cacheDir.toPath(), "imageio", ".tmp")
.toFile();
this.cache = new RandomAccessFile(cacheFile, "rw"); this.cache = new RandomAccessFile(cacheFile, "rw");
this.closeAction = StreamCloser.createCloseAction(this); this.closeAction = StreamCloser.createCloseAction(this);
......
/* /*
* Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -29,6 +29,7 @@ import java.io.File; ...@@ -29,6 +29,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.file.Files;
import com.sun.imageio.stream.StreamCloser; import com.sun.imageio.stream.StreamCloser;
/** /**
...@@ -83,8 +84,11 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl { ...@@ -83,8 +84,11 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl {
throw new IllegalArgumentException("Not a directory!"); throw new IllegalArgumentException("Not a directory!");
} }
this.stream = stream; this.stream = stream;
this.cacheFile = if (cacheDir == null)
File.createTempFile("imageio", ".tmp", cacheDir); this.cacheFile = Files.createTempFile("imageio", ".tmp").toFile();
else
this.cacheFile = Files.createTempFile(cacheDir.toPath(), "imageio", ".tmp")
.toFile();
this.cache = new RandomAccessFile(cacheFile, "rw"); this.cache = new RandomAccessFile(cacheFile, "rw");
this.closeAction = StreamCloser.createCloseAction(this); this.closeAction = StreamCloser.createCloseAction(this);
......
/* /*
* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -44,6 +44,7 @@ import java.lang.reflect.Constructor; ...@@ -44,6 +44,7 @@ import java.lang.reflect.Constructor;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLStreamHandlerFactory; import java.net.URLStreamHandlerFactory;
import java.nio.file.Files;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -1160,8 +1161,9 @@ public class MLet extends java.net.URLClassLoader ...@@ -1160,8 +1161,9 @@ public class MLet extends java.net.URLClassLoader
try { try {
File directory = new File(libraryDirectory); File directory = new File(libraryDirectory);
directory.mkdirs(); directory.mkdirs();
File file = File.createTempFile(libname + ".", null, File file = Files.createTempFile(directory.toPath(),
directory); libname + ".", null)
.toFile();
file.deleteOnExit(); file.deleteOnExit();
FileOutputStream fileOutput = new FileOutputStream(file); FileOutputStream fileOutput = new FileOutputStream(file);
try { try {
......
/* /*
* Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -97,6 +97,7 @@ import sun.font.FontUtilities; ...@@ -97,6 +97,7 @@ import sun.font.FontUtilities;
import java.nio.charset.*; import java.nio.charset.*;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.file.Files;
//REMIND: Remove use of this class when IPPPrintService is moved to share directory. //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -659,7 +660,7 @@ public class PSPrinterJob extends RasterPrinterJob { ...@@ -659,7 +660,7 @@ public class PSPrinterJob extends RasterPrinterJob {
* is not removed for some reason, request that it is * is not removed for some reason, request that it is
* removed when the VM exits. * removed when the VM exits.
*/ */
spoolFile = File.createTempFile("javaprint", ".ps", null); spoolFile = Files.createTempFile("javaprint", ".ps").toFile();
spoolFile.deleteOnExit(); spoolFile.deleteOnExit();
result = new FileOutputStream(spoolFile); result = new FileOutputStream(spoolFile);
......
/* /*
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -43,6 +43,7 @@ import java.net.ServerSocket; ...@@ -43,6 +43,7 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.SocketException; import java.net.SocketException;
import java.nio.file.Files;
import java.nio.channels.Channel; import java.nio.channels.Channel;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import java.rmi.AccessException; import java.rmi.AccessException;
...@@ -1940,7 +1941,7 @@ public class Activation implements Serializable { ...@@ -1940,7 +1941,7 @@ public class Activation implements Serializable {
new PrivilegedExceptionAction<Void>() { new PrivilegedExceptionAction<Void>() {
public Void run() throws IOException { public Void run() throws IOException {
File file = File file =
File.createTempFile("rmid-err", null, null); Files.createTempFile("rmid-err", null).toFile();
PrintStream errStream = PrintStream errStream =
new PrintStream(new FileOutputStream(file)); new PrintStream(new FileOutputStream(file));
System.setErr(errStream); System.setErr(errStream);
......
/* /*
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -137,7 +137,7 @@ class Main { ...@@ -137,7 +137,7 @@ class Main {
File dir = file.getParentFile(); File dir = file.getParentFile();
if (dir == null) if (dir == null)
dir = new File("."); dir = new File(".");
return File.createTempFile("jartmp", null, dir); return Files.createTempFile(dir.toPath(), "jartmp", null).toFile();
} }
private boolean ok; private boolean ok;
......
/* /*
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -71,6 +71,7 @@ import java.text.MessageFormat; ...@@ -71,6 +71,7 @@ import java.text.MessageFormat;
import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.IllegalCharsetNameException;
import java.nio.file.Files;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import sun.tools.native2ascii.A2NFilter; import sun.tools.native2ascii.A2NFilter;
...@@ -240,9 +241,7 @@ public class Main { ...@@ -240,9 +241,7 @@ public class Main {
if (tempDir == null) if (tempDir == null)
tempDir = new File(System.getProperty("user.dir")); tempDir = new File(System.getProperty("user.dir"));
tempFile = File.createTempFile("_N2A", tempFile = Files.createTempFile(tempDir.toPath(), "_N2A", ".TMP").toFile();
".TMP",
tempDir);
tempFile.deleteOnExit(); tempFile.deleteOnExit();
try { try {
...@@ -292,9 +291,7 @@ public class Main { ...@@ -292,9 +291,7 @@ public class Main {
File tempDir = f.getParentFile(); File tempDir = f.getParentFile();
if (tempDir == null) if (tempDir == null)
tempDir = new File(System.getProperty("user.dir")); tempDir = new File(System.getProperty("user.dir"));
tempFile = File.createTempFile("_N2A", tempFile = Files.createTempFile(tempDir.toPath(), "_N2A", ".TMP").toFile();
".TMP",
tempDir);
tempFile.deleteOnExit(); tempFile.deleteOnExit();
try { try {
......
/* /*
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -33,6 +33,7 @@ import java.io.IOException; ...@@ -33,6 +33,7 @@ import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Properties; import java.util.Properties;
...@@ -387,7 +388,7 @@ public class FcFontConfiguration extends FontConfiguration { ...@@ -387,7 +388,7 @@ public class FcFontConfiguration extends FontConfiguration {
File fcInfoFile = getFcInfoFile(); File fcInfoFile = getFcInfoFile();
File dir = fcInfoFile.getParentFile(); File dir = fcInfoFile.getParentFile();
dir.mkdirs(); dir.mkdirs();
File tempFile = File.createTempFile("fcinfo", null, dir); File tempFile = Files.createTempFile(dir.toPath(), "fcinfo", null).toFile();
FileOutputStream fos = new FileOutputStream(tempFile); FileOutputStream fos = new FileOutputStream(tempFile);
props.store(fos, props.store(fos,
"JDK Font Configuration Generated File: *Do Not Edit*"); "JDK Font Configuration Generated File: *Do Not Edit*");
......
/* /*
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -42,6 +42,7 @@ import java.io.PrintWriter; ...@@ -42,6 +42,7 @@ import java.io.PrintWriter;
import java.io.Reader; import java.io.Reader;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.util.Vector; import java.util.Vector;
import javax.print.CancelablePrintJob; import javax.print.CancelablePrintJob;
...@@ -938,7 +939,7 @@ public class UnixPrintJob implements CancelablePrintJob { ...@@ -938,7 +939,7 @@ public class UnixPrintJob implements CancelablePrintJob {
* is not removed for some reason, request that it is * is not removed for some reason, request that it is
* removed when the VM exits. * removed when the VM exits.
*/ */
spoolFile = File.createTempFile("javaprint", ".ps", null); spoolFile = Files.createTempFile("javaprint", ".ps").toFile();
spoolFile.deleteOnExit(); spoolFile.deleteOnExit();
} }
result = new FileOutputStream(spoolFile); result = new FileOutputStream(spoolFile);
......
/* /*
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -51,6 +51,7 @@ import javax.print.attribute.standard.PrinterName; ...@@ -51,6 +51,7 @@ import javax.print.attribute.standard.PrinterName;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
/* /*
* Remind: This class uses solaris commands. We also need a linux * Remind: This class uses solaris commands. We also need a linux
...@@ -714,7 +715,7 @@ public class UnixPrintServiceLookup extends PrintServiceLookup ...@@ -714,7 +715,7 @@ public class UnixPrintServiceLookup extends PrintServiceLookup
Process proc; Process proc;
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
File f = File.createTempFile("prn","xc"); File f = Files.createTempFile("prn","xc").toFile();
cmd[2] = cmd[2]+">"+f.getAbsolutePath(); cmd[2] = cmd[2]+">"+f.getAbsolutePath();
proc = Runtime.getRuntime().exec(cmd); proc = Runtime.getRuntime().exec(cmd);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册