2004-10-12 05:49:01 +00:00
|
|
|
/* ====================================================================
|
2006-12-22 19:18:16 +00:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
2004-10-12 05:49:01 +00:00
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==================================================================== */
|
2009-08-18 19:49:28 +00:00
|
|
|
|
2004-10-12 05:49:01 +00:00
|
|
|
package org.apache.poi.util;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2014-07-24 18:58:27 +00:00
|
|
|
import java.io.IOException;
|
2004-10-12 05:49:01 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-07-24 18:58:27 +00:00
|
|
|
* Interface for creating temporary files. Collects them all into one directory by default.
|
2004-10-12 05:49:01 +00:00
|
|
|
*/
|
2009-08-18 19:49:28 +00:00
|
|
|
public final class TempFile {
|
2014-07-24 18:58:27 +00:00
|
|
|
/** The strategy used by {@link #createTempFile(String, String)} to create the temporary files. */
|
|
|
|
|
private static TempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy();
|
2016-03-12 16:56:33 +00:00
|
|
|
|
|
|
|
|
/** Define a constant for this property as it is sometimes mistypes as "tempdir" otherwise */
|
|
|
|
|
public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
|
|
|
|
|
|
2004-10-12 05:49:01 +00:00
|
|
|
/**
|
2014-07-24 18:58:27 +00:00
|
|
|
* Configures the strategy used by {@link #createTempFile(String, String)} to create the temporary files.
|
|
|
|
|
*
|
|
|
|
|
* @param strategy The new strategy to be used to create the temporary files.
|
|
|
|
|
*
|
|
|
|
|
* @throws IllegalArgumentException When the given strategy is <code>null</code>.
|
|
|
|
|
*/
|
|
|
|
|
public static void setTempFileCreationStrategy(TempFileCreationStrategy strategy) {
|
|
|
|
|
if (strategy == null) {
|
|
|
|
|
throw new IllegalArgumentException("strategy == null");
|
|
|
|
|
}
|
|
|
|
|
TempFile.strategy = strategy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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}).
|
2004-10-12 05:49:01 +00:00
|
|
|
* <p>
|
2009-08-18 19:49:28 +00:00
|
|
|
* Don't forget to close all files or it might not be possible to delete them.
|
2014-07-24 18:58:27 +00:00
|
|
|
*
|
|
|
|
|
* @param prefix The prefix to be used to generate the name of the temporary file.
|
|
|
|
|
* @param suffix The suffix to be used to generate the name of the temporary file.
|
|
|
|
|
*
|
|
|
|
|
* @return The path to the newly created and empty temporary file.
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException If no temporary file could be created.
|
2004-10-12 05:49:01 +00:00
|
|
|
*/
|
2014-07-24 18:58:27 +00:00
|
|
|
public static File createTempFile(String prefix, String suffix) throws IOException {
|
|
|
|
|
return strategy.createTempFile(prefix, suffix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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>.
|
|
|
|
|
*/
|
|
|
|
|
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);
|
2004-10-12 05:49:01 +00:00
|
|
|
}
|
2014-07-24 18:58:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
2013-10-10 16:20:07 +00:00
|
|
|
}
|
2014-07-24 18:58:27 +00:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public File createTempFile(String prefix, String suffix) throws IOException {
|
|
|
|
|
// Identify and create our temp dir, if needed
|
2016-03-12 16:56:33 +00:00
|
|
|
if (dir == null) {
|
2016-06-05 16:49:18 +00:00
|
|
|
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!");
|
2016-03-12 16:56:33 +00:00
|
|
|
}
|
2016-06-05 16:49:18 +00:00
|
|
|
dir = new File(tmpDir, "poifiles");
|
2014-07-24 18:58:27 +00:00
|
|
|
}
|
2013-10-10 16:20:07 +00:00
|
|
|
|
2016-06-05 16:49:18 +00:00
|
|
|
if (!(dir.exists() || dir.mkdirs()) || !dir.isDirectory()) {
|
|
|
|
|
throw new IOException("Could not create temporary directory '" + dir + "'");
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-24 18:58:27 +00:00
|
|
|
// Generate a unique new filename
|
|
|
|
|
File newFile = File.createTempFile(prefix, suffix, dir);
|
2013-10-10 16:20:07 +00:00
|
|
|
|
2014-07-24 18:58:27 +00:00
|
|
|
// Set the delete on exit flag, unless explicitly disabled
|
2016-06-05 16:49:18 +00:00
|
|
|
if (System.getProperty("poi.keep.tmp.files") == null) {
|
2014-07-24 18:58:27 +00:00
|
|
|
newFile.deleteOnExit();
|
2016-06-05 16:49:18 +00:00
|
|
|
}
|
2014-07-24 18:58:27 +00:00
|
|
|
|
|
|
|
|
// All done
|
|
|
|
|
return newFile;
|
|
|
|
|
}
|
2004-10-12 05:49:01 +00:00
|
|
|
}
|
|
|
|
|
}
|