aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org
diff options
context:
space:
mode:
authorJaven O'Neal <onealj@apache.org>2016-07-04 00:00:20 +0000
committerJaven O'Neal <onealj@apache.org>2016-07-04 00:00:20 +0000
commita2d96fe7e3225a0ae843738c45c82412094ad1af (patch)
treec1822e8a4a5e4fb17d18655b722498b5217760b9 /src/java/org
parent25aebedc693d8129f17750b5c8ae2c3294c56575 (diff)
downloadpoi-a2d96fe7e3225a0ae843738c45c82412094ad1af.tar.gz
poi-a2d96fe7e3225a0ae843738c45c82412094ad1af.zip
bug 59788: move DefaultTempFileCreationStrategy from TempFile inner class to its own class
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751185 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
-rw-r--r--src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java99
-rw-r--r--src/java/org/apache/poi/util/TempFile.java89
2 files changed, 101 insertions, 87 deletions
diff --git a/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java b/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
new file mode 100644
index 0000000000..0ed08c15d7
--- /dev/null
+++ b/src/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java
@@ -0,0 +1,99 @@
+package org.apache.poi.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+/**
+ * Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
+ * Files are collected into one directory and by default are deleted on exit from the VM.
+ * Files may be manually deleted by user prior to JVM exit.
+ * Files can be kept by defining the system property {@link #KEEP_FILES}.
+ */
+public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
+ /** Define a constant for this property as it is sometimes mistypes as "tempdir" otherwise */
+ private static final String JAVA_IO_TMPDIR = TempFile.JAVA_IO_TMPDIR;
+ /** To keep files after JVM exit, set the <code>-Dpoi.keep.tmp.files</code> JVM property */
+ public static final String KEEP_FILES = "poi.keep.tmp.files";
+
+ /** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
+ private File dir;
+
+ /**
+ * Creates the strategy so that it creates the temporary files in the default directory.
+ *
+ * @see File#createTempFile(String, String)
+ */
+ public DefaultTempFileCreationStrategy() {
+ this(null);
+ }
+
+ /**
+ * Creates the strategy allowing to set the
+ *
+ * @param dir The directory where the temporary files will be created (<code>null</code> to use the default directory).
+ *
+ * @see File#createTempFile(String, String, File)
+ */
+ public DefaultTempFileCreationStrategy(File dir) {
+ this.dir = dir;
+ }
+
+ private void createPOIFilesDirectory() throws IOException {
+ // Identify and create our temp dir, if needed
+ // 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!");
+ }
+ dir = new File(tmpDir, "poifiles");
+ }
+
+ createTempDirectory(dir);
+ }
+
+ private void createTempDirectory(File directory) throws IOException {
+ if (!(directory.exists() || directory.mkdirs()) || !directory.isDirectory()) {
+ throw new IOException("Could not create temporary directory '" + directory + "'");
+ }
+ }
+
+ @Override
+ public File createTempFile(String prefix, String suffix) throws IOException {
+ // Identify and create our temp dir, if needed
+ createPOIFilesDirectory();
+
+ // Generate a unique new filename
+ File newFile = File.createTempFile(prefix, suffix, dir);
+
+ // Set the delete on exit flag, unless explicitly disabled
+ if (System.getProperty(KEEP_FILES) == null) {
+ newFile.deleteOnExit();
+ }
+
+ // All done
+ return newFile;
+ }
+
+ private static final SecureRandom random = new SecureRandom();
+ @Override
+ public File createTempDirectory(String prefix) throws IOException {
+ // Identify and create our temp dir, if needed
+ createPOIFilesDirectory();
+
+ // Generate a unique new filename
+ // FIXME: Java 7+: use java.nio.Files#createTempDirectory
+ final long n = random.nextLong();
+ File newDirectory = new File(dir, prefix + Long.toString(n));
+ createTempDirectory(newDirectory);
+
+ // Set the delete on exit flag, unless explicitly disabled
+ if (System.getProperty(KEEP_FILES) == null) {
+ newDirectory.deleteOnExit();
+ }
+
+ // All done
+ return newDirectory;
+ }
+} \ No newline at end of file
diff --git a/src/java/org/apache/poi/util/TempFile.java b/src/java/org/apache/poi/util/TempFile.java
index b2b9c56bd4..ceb7c1d1c6 100644
--- a/src/java/org/apache/poi/util/TempFile.java
+++ b/src/java/org/apache/poi/util/TempFile.java
@@ -19,7 +19,6 @@ package org.apache.poi.util;
import java.io.File;
import java.io.IOException;
-import java.security.SecureRandom;
/**
* Interface for creating temporary files. Collects them all into one directory by default.
@@ -72,91 +71,7 @@ public final class TempFile {
}
/**
- * Default implementation of the {@link TempFileCreationStrategy} used by {@link TempFile}:
- * Files are collected into one directory and by default are deleted on exit from the VM.
- * Files can be kept by defining the system property <code>poi.keep.tmp.files</code>.
+ * @deprecated POI 3.15 beta 3. Moved to {@link org.apache.poi.util.DefaultTempFileCreationStrategy}.
*/
- public static class DefaultTempFileCreationStrategy implements TempFileCreationStrategy {
-
- /** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
- private File dir;
-
- /**
- * Creates the strategy so that it creates the temporary files in the default directory.
- *
- * @see File#createTempFile(String, String)
- */
- public DefaultTempFileCreationStrategy() {
- this(null);
- }
-
- /**
- * Creates the strategy allowing to set the
- *
- * @param dir The directory where the temporary files will be created (<code>null</code> to use the default directory).
- *
- * @see File#createTempFile(String, String, File)
- */
- public DefaultTempFileCreationStrategy(File dir) {
- this.dir = dir;
- }
-
- private void createPOIFilesDirectory() throws IOException {
- // Identify and create our temp dir, if needed
- // The directory is not deleted, even if it was created by this TempFleCreationStrategy
- 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!");
- }
- dir = new File(tmpDir, "poifiles");
- }
-
- createTempDirectory(dir);
- }
-
- private void createTempDirectory(File directory) throws IOException {
- if (!(directory.exists() || directory.mkdirs()) || !directory.isDirectory()) {
- throw new IOException("Could not create temporary directory '" + directory + "'");
- }
- }
-
- @Override
- public File createTempFile(String prefix, String suffix) throws IOException {
- // Identify and create our temp dir, if needed
- createPOIFilesDirectory();
-
- // Generate a unique new filename
- File newFile = File.createTempFile(prefix, suffix, dir);
-
- // Set the delete on exit flag, unless explicitly disabled
- if (System.getProperty("poi.keep.tmp.files") == null) {
- newFile.deleteOnExit();
- }
-
- // All done
- return newFile;
- }
-
- private static final SecureRandom random = new SecureRandom();
- @Override
- public File createTempDirectory(String prefix) throws IOException {
- // Identify and create our temp dir, if needed
- createPOIFilesDirectory();
-
- // Generate a unique new filename
- // FIXME: Java 7+: use java.nio.Files#createTempDirectory
- final long n = random.nextLong();
- File newDirectory = new File(dir, prefix + Long.toString(n));
- createTempDirectory(newDirectory);
-
- // Set the delete on exit flag, unless explicitly disabled
- if (System.getProperty("poi.keep.tmp.files") == null) {
- newDirectory.deleteOnExit();
- }
-
- // All done
- return newDirectory;
- }
- }
+ public static class DefaultTempFileCreationStrategy extends org.apache.poi.util.DefaultTempFileCreationStrategy {}
}