From fc49da56ae2e95b6eff27b23bfd0f07d109ac551 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 26 Jan 2022 19:35:06 +0000 Subject: [PATCH] [bug-65849] use Files.createTempFile git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897515 13f79535-47bb-0310-9956-ffa450edef68 --- .../examples/xssf/usermodel/BigGridDemo.java | 3 ++- .../util/DefaultTempFileCreationStrategy.java | 19 +++++++++++++++++-- .../java/org/apache/poi/util/TestIOUtils.java | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java index 06bc962d5d..1bb8409369 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/usermodel/BigGridDemo.java @@ -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) diff --git a/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java b/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java index bbfb03e9ab..b5ff8afbeb 100644 --- a/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java +++ b/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java @@ -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 -Dpoi.delete.tmp.files.on.exit JVM property */ public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit"; + private final FileAttribute> userPermissions; + /** The directory where the temporary files will be created (null 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 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 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); diff --git a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java index e5a70fa3bb..1ce390f96a 100644 --- a/poi/src/test/java/org/apache/poi/util/TestIOUtils.java +++ b/poi/src/test/java/org/apache/poi/util/TestIOUtils.java @@ -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));