From 40f9b396d932fb28550ade1b690d3abaa4a83b20 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Thu, 16 Dec 2010 11:42:39 +0000 Subject: [PATCH] SONAR-2016 improve the class TempFileUtils : add the method.createTempDirectory(String) and add some javadoc --- .../org/sonar/api/utils/TempFileUtils.java | 20 +++++++++++++++---- 1 file 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 : + *
    + *
  1. deleteOnExit() only deletes for normal JVM shutdowns, not crashes or killing the JVM process
  2. + *
  3. deleteOnExit() only deletes on JVM shutdown - not good for long running server processes because 3 :
  4. + *
  5. 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.
  6. + *
+ */ 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; } - } -- 2.39.5