mirror of https://github.com/apache/poi.git
[bug-65849] use Files.createTempFile
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897515 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0ce3e46204
commit
fc49da56ae
|
@ -25,6 +25,7 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -101,7 +102,7 @@ public final class BigGridDemo {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Step 2. Generate XML file.
|
//Step 2. Generate XML file.
|
||||||
tmp = File.createTempFile("sheet", ".xml");
|
tmp = Files.createTempFile("sheet", ".xml").toFile();
|
||||||
try (
|
try (
|
||||||
FileOutputStream stream = new FileOutputStream(tmp);
|
FileOutputStream stream = new FileOutputStream(tmp);
|
||||||
Writer fw = new OutputStreamWriter(stream, XML_ENCODING)
|
Writer fw = new OutputStreamWriter(stream, XML_ENCODING)
|
||||||
|
|
|
@ -21,6 +21,12 @@ import static org.apache.poi.util.TempFile.JAVA_IO_TMPDIR;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.attribute.FileAttribute;
|
||||||
|
import java.nio.file.attribute.PosixFilePermission;
|
||||||
|
import java.nio.file.attribute.PosixFilePermissions;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
|
* Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
|
||||||
|
@ -40,6 +46,8 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
|
||||||
/** To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property */
|
/** To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property */
|
||||||
public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit";
|
public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit";
|
||||||
|
|
||||||
|
private final FileAttribute<Set<PosixFilePermission>> userPermissions;
|
||||||
|
|
||||||
/** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
|
/** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
|
||||||
private File dir;
|
private File dir;
|
||||||
|
|
||||||
|
@ -61,6 +69,10 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
|
||||||
*/
|
*/
|
||||||
public DefaultTempFileCreationStrategy(File dir) {
|
public DefaultTempFileCreationStrategy(File dir) {
|
||||||
this.dir = dir;
|
this.dir = dir;
|
||||||
|
Set<PosixFilePermission> permissions = new HashSet<>();
|
||||||
|
permissions.add(PosixFilePermission.OWNER_READ);
|
||||||
|
permissions.add(PosixFilePermission.OWNER_WRITE);
|
||||||
|
userPermissions = PosixFilePermissions.asFileAttribute(permissions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createPOIFilesDirectory() throws IOException {
|
private void createPOIFilesDirectory() throws IOException {
|
||||||
|
@ -103,7 +115,10 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
|
||||||
createPOIFilesDirectory();
|
createPOIFilesDirectory();
|
||||||
|
|
||||||
// Generate a unique new filename
|
// Generate a unique new filename
|
||||||
File newFile = File.createTempFile(prefix, suffix, dir);
|
HashSet<PosixFilePermission> permissions = new HashSet<>();
|
||||||
|
permissions.add(PosixFilePermission.OWNER_READ);
|
||||||
|
permissions.add(PosixFilePermission.OWNER_WRITE);
|
||||||
|
File newFile = Files.createTempFile(dir.toPath(), prefix, suffix, userPermissions).toFile();
|
||||||
|
|
||||||
// Set the delete on exit flag, but only when explicitly disabled
|
// Set the delete on exit flag, but only when explicitly disabled
|
||||||
if (System.getProperty(DELETE_FILES_ON_EXIT) != null) {
|
if (System.getProperty(DELETE_FILES_ON_EXIT) != null) {
|
||||||
|
@ -121,7 +136,7 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
|
||||||
createPOIFilesDirectory();
|
createPOIFilesDirectory();
|
||||||
|
|
||||||
// Generate a unique new filename
|
// Generate a unique new filename
|
||||||
// FIXME: Java 7+: use java.nio.Files#createTempDirectory
|
// FIXME: Java 7+: use java.nio.file.Files#createTempDirectory
|
||||||
final long n = RandomSingleton.getInstance().nextLong();
|
final long n = RandomSingleton.getInstance().nextLong();
|
||||||
File newDirectory = new File(dir, prefix + Long.toString(n));
|
File newDirectory = new File(dir, prefix + Long.toString(n));
|
||||||
createTempDirectory(newDirectory);
|
createTempDirectory(newDirectory);
|
||||||
|
|
|
@ -37,6 +37,7 @@ import java.io.PushbackInputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
|
||||||
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
|
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
|
||||||
import org.apache.poi.EmptyFileException;
|
import org.apache.poi.EmptyFileException;
|
||||||
|
@ -181,7 +182,7 @@ final class TestIOUtils {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCopyToFile() throws IOException {
|
void testCopyToFile() throws IOException {
|
||||||
File dest = File.createTempFile("poi-ioutils-", "");
|
File dest = Files.createTempFile("poi-ioutils-", "").toFile();
|
||||||
try {
|
try {
|
||||||
try (InputStream is = new FileInputStream(TMP)) {
|
try (InputStream is = new FileInputStream(TMP)) {
|
||||||
assertEquals(LENGTH, IOUtils.copy(is, dest));
|
assertEquals(LENGTH, IOUtils.copy(is, dest));
|
||||||
|
|
Loading…
Reference in New Issue