diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-16 11:42:39 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-16 11:42:39 +0000 |
commit | 40f9b396d932fb28550ade1b690d3abaa4a83b20 (patch) | |
tree | 8bbf80ef8c5633cceaa7ce7142c124f6fcaa9389 | |
parent | 24476cef14d977263d901394132a69c6bd8e94df (diff) | |
download | sonarqube-40f9b396d932fb28550ade1b690d3abaa4a83b20.tar.gz sonarqube-40f9b396d932fb28550ade1b690d3abaa4a83b20.zip |
SONAR-2016 improve the class TempFileUtils : add the method.createTempDirectory(String) and add some javadoc
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java index 6b8ec3ea8c9..06a5b96e403 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java @@ -28,16 +28,28 @@ public final class TempFileUtils { // only static methods } + /** + * Create a temporary directory. This directory is NOT deleted when the JVM stops, because using File#deleteOnExit() + * is evil (google "deleteonExit evil"). Copied from http://stackoverflow.com/questions/617414/create-a-temporary-directory-in-java : + * <ol> + * <li>deleteOnExit() only deletes for normal JVM shutdowns, not crashes or killing the JVM process</li> + * <li>deleteOnExit() only deletes on JVM shutdown - not good for long running server processes because 3 :</li> + * <li>The most evil of all - deleteOnExit() consumes memory for each temp file entry. If your process is running for months, or creates a lot of temp files in a short time, you consume memory and never release it until the JVM shuts down.</li> + * </ol> + */ public static File createTempDirectory() throws IOException { - final File temp = File.createTempFile("temp", Long.toString(System.nanoTime())); - if (!(temp.delete())) { + return createTempDirectory("temp"); + } + + public static File createTempDirectory(String prefix) throws IOException { + final File temp = File.createTempFile(prefix, Long.toString(System.nanoTime())); + if (!temp.delete()) { throw new IOException("Could not delete temp file: " + temp.getAbsolutePath()); } - if (!(temp.mkdir())) { + if (!temp.mkdir()) { throw new IOException("Could not create temp directory: " + temp.getAbsolutePath()); } return temp; } - } |