You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/12/28 08:08:09 UTC

svn commit: r1906238 - in /poi/trunk: poi-examples/src/main/java/org/apache/poi/examples/util/ poi-ooxml/src/test/java/org/apache/poi/util/tests/ poi/src/main/java/org/apache/poi/util/

Author: fanningpj
Date: Wed Dec 28 08:08:09 2022
New Revision: 1906238

URL: http://svn.apache.org/viewvc?rev=1906238&view=rev
Log:
[bug-66397] update temp file code. Thanks to lsq27.

Modified:
    poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/util/TempFileUtils.java
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java
    poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
    poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java

Modified: poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/util/TempFileUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/util/TempFileUtils.java?rev=1906238&r1=1906237&r2=1906238&view=diff
==============================================================================
--- poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/util/TempFileUtils.java (original)
+++ poi/trunk/poi-examples/src/main/java/org/apache/poi/examples/util/TempFileUtils.java Wed Dec 28 08:08:09 2022
@@ -19,18 +19,19 @@
 
 package org.apache.poi.examples.util;
 
-import java.io.File;
-
+import org.apache.poi.util.DefaultTempFileCreationStrategy;
 import org.apache.poi.util.TempFile;
 
+import java.io.File;
+import java.nio.file.Paths;
+
 public final class TempFileUtils {
     private TempFileUtils() {
     }
 
     @SuppressWarnings("java:S106")
     public static void checkTempFiles() {
-        String tmpDir = System.getProperty(TempFile.JAVA_IO_TMPDIR) + "/poifiles";
-        File tempDir = new File(tmpDir);
+        File tempDir = Paths.get(System.getProperty(TempFile.JAVA_IO_TMPDIR), DefaultTempFileCreationStrategy.POIFILES).toFile();
         if(tempDir.exists()) {
             String[] tempFiles = tempDir.list();
             if(tempFiles != null && tempFiles.length > 0) {

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java?rev=1906238&r1=1906237&r2=1906238&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/util/tests/TestTempFileThreaded.java Wed Dec 28 08:08:09 2022
@@ -24,6 +24,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -76,10 +77,11 @@ class TestTempFileThreaded {
     public static void setUpClass() throws IOException {
         String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
         if (tmpDir == null) {
-            throw new IOException("Systems temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!");
+            throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!");
         }
 
-        TempFile.setTempFileCreationStrategy(createTempFileCreationStrategy(new File(new File(tmpDir, POIFILES), "TestTempFileThreaded")));
+        TempFile.setTempFileCreationStrategy(createTempFileCreationStrategy(
+                Paths.get(tmpDir, POIFILES, "TestTempFileThreaded").toFile()));
     }
 
     @BeforeEach

Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java?rev=1906238&r1=1906237&r2=1906238&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java Wed Dec 28 08:08:09 2022
@@ -23,7 +23,10 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.attribute.FileAttribute;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
@@ -38,13 +41,17 @@ import java.nio.file.attribute.FileAttri
  * processes or limited temporary storage.
  */
 public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
+    /** Name of POI files directory in temporary directory. */
     public static final String POIFILES = "poifiles";
 
     /** 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";
 
     /** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
-    private File dir;
+    private volatile File dir;
+
+    /** The lock to make dir initialized only once. */
+    private final Lock dirLock = new ReentrantLock();
 
     /**
      * Creates the strategy so that it creates the temporary files in the default directory.
@@ -67,36 +74,22 @@ public class DefaultTempFileCreationStra
     }
 
     private void createPOIFilesDirectory() throws IOException {
-        // Identify and create our temp dir, if needed
+        // Create our temp dir only once by double-checked locking
         // The directory is not deleted, even if it was created by this TempFileCreationStrategy
         if (dir == null) {
-            String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
-            if (tmpDir == null) {
-                throw new IOException("Systems temporary directory not defined - set the -D"+JAVA_IO_TMPDIR+" jvm property!");
+            dirLock.lock();
+            try {
+                if (dir == null) {
+                    String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
+                    if (tmpDir == null) {
+                        throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR + " jvm property!");
+                    }
+                    Path dirPath = Paths.get(tmpDir, POIFILES);
+                    dir = Files.createDirectories(dirPath).toFile();
+                }
+            } finally {
+                dirLock.unlock();
             }
-            dir = new File(tmpDir, POIFILES);
-        }
-
-        createTempDirectory(dir);
-    }
-
-    /**
-     * Attempt to create a directory, including any necessary parent directories.
-     * Does nothing if directory already exists.
-     * The method is synchronized to ensure that multiple threads don't try to create the directory at the same time.
-     *
-     * @param directory  the directory to create
-     * @throws IOException if unable to create temporary directory or it is not a directory
-     */
-    private synchronized void createTempDirectory(File directory) throws IOException {
-        // create directory if it doesn't exist
-        final boolean dirExists = (directory.exists() || directory.mkdirs());
-
-        if (!dirExists) {
-            throw new IOException("Could not create temporary directory '" + directory + "'");
-        }
-        else if (!directory.isDirectory()) {
-            throw new IOException("Could not create temporary directory. '" + directory + "' exists but is not a directory.");
         }
     }
 
@@ -124,10 +117,7 @@ public class DefaultTempFileCreationStra
         createPOIFilesDirectory();
 
         // Generate a unique new filename
-        // 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);
+        File newDirectory = Files.createTempDirectory(dir.toPath(), prefix).toFile();
 
         //this method appears to be only used in tests, so it is probably ok to use deleteOnExit
         newDirectory.deleteOnExit();

Modified: poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java?rev=1906238&r1=1906237&r2=1906238&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/util/TempFile.java Wed Dec 28 08:08:09 2022
@@ -49,9 +49,10 @@ public final class TempFile {
     }
     
     /**
-     * Creates a new and empty temporary file. By default, files are collected into one directory and are
-     * deleted on exit from the VM, although they can be kept by defining the system property
-     * <code>poi.keep.tmp.files</code> (see {@link DefaultTempFileCreationStrategy}).
+     * Creates a new and empty temporary file. By default, files are collected into one directory and are not
+     * deleted on exit from the VM, although they can be deleted by defining the system property
+     * <code>poi.delete.tmp.files.on.exit</code> (see {@link DefaultTempFileCreationStrategy}), which may
+     * cause {@link OutOfMemoryError} problem due to the frequent usage of {@link File#deleteOnExit()}
      * <p>
      * Don't forget to close all files or it might not be possible to delete them.
      *



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org