[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:
PJ Fanning 2022-01-26 19:35:06 +00:00
parent 0ce3e46204
commit fc49da56ae
3 changed files with 21 additions and 4 deletions

View File

@ -25,6 +25,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
@ -101,7 +102,7 @@ public final class BigGridDemo {
}
//Step 2. Generate XML file.
tmp = File.createTempFile("sheet", ".xml");
tmp = Files.createTempFile("sheet", ".xml").toFile();
try (
FileOutputStream stream = new FileOutputStream(tmp);
Writer fw = new OutputStreamWriter(stream, XML_ENCODING)

View File

@ -21,6 +21,12 @@ import static org.apache.poi.util.TempFile.JAVA_IO_TMPDIR;
import java.io.File;
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}:
@ -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 */
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). */
private File dir;
@ -61,6 +69,10 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
*/
public DefaultTempFileCreationStrategy(File 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 {
@ -103,7 +115,10 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
createPOIFilesDirectory();
// 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
if (System.getProperty(DELETE_FILES_ON_EXIT) != null) {
@ -121,7 +136,7 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
createPOIFilesDirectory();
// 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();
File newDirectory = new File(dir, prefix + Long.toString(n));
createTempDirectory(newDirectory);

View File

@ -37,6 +37,7 @@ import java.io.PushbackInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.EmptyFileException;
@ -181,7 +182,7 @@ final class TestIOUtils {
@Test
void testCopyToFile() throws IOException {
File dest = File.createTempFile("poi-ioutils-", "");
File dest = Files.createTempFile("poi-ioutils-", "").toFile();
try {
try (InputStream is = new FileInputStream(TMP)) {
assertEquals(LENGTH, IOUtils.copy(is, dest));