[Schmitzm-commits] r118 - trunk/src/schmitzm/io

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat May 16 16:33:12 CEST 2009


Author: alfonx
Date: 2009-05-16 16:33:11 +0200 (Sat, 16 May 2009)
New Revision: 118

Modified:
   trunk/src/schmitzm/io/IOUtil.java
Log:
* New method: public static File getTempDir()

Modified: trunk/src/schmitzm/io/IOUtil.java
===================================================================
--- trunk/src/schmitzm/io/IOUtil.java	2009-05-14 01:15:22 UTC (rev 117)
+++ trunk/src/schmitzm/io/IOUtil.java	2009-05-16 14:33:11 UTC (rev 118)
@@ -501,8 +501,49 @@
 	      System.out.println(delCount+ " files deleted.");
 	    }
 	    else {
-	      System.err.println("Unknown function: "+func);
+	      LOGGER.error("Unknown function: "+func);
 	    }
 	  }
 
+	  /**
+	 * The method File getTempDir() caches the system's temp direcotry here.
+	 * 
+	 * @see #getTempDir()
+	 */
+	private static File tempDir;
+
+	/**
+	 * The system property <code>java.io.tmpdir</code> is not consistent on
+	 * Linux and Windows. On Windows&Solaris the path ends with a slash, on
+	 * Linux&MacOS it doesn't. This method deals with this potential pit-fall.<br/>
+	 * The folder is cached in a static {@link File} field.
+	 * 
+	 * @return A {@link File} object pointing to the system's temp directory.
+	 *         This method does some extra checks and returns a valid {@link File}
+	 * 
+	 * @see
+	 *      <code>http://rationalpi.wordpress.com/2007/01/26/javaiotmpdir-inconsitency/</code>
+	 */
+	public static File getTempDir() {
+
+		if (tempDir == null) {
+			String tempPath = System.getProperty("java.io.tmpdir", System
+					.getenv("TEMP"));
+
+			if (tempPath == null) {
+				LOGGER.warn("Temporary directory can't be found. Using /tmp");
+				tempPath = "/tmp";
+			}
+
+			while (tempPath.endsWith(File.separator))
+				tempPath = tempPath.substring(0, tempPath.length() - 1);
+
+			tempDir = new File(tempPath);
+			
+			if (!tempDir.exists()) throw new IllegalStateException("Temp direcotory can't be determined."); 
+		}
+
+		return tempDir;
+	}
+
 }



More information about the Schmitzm-commits mailing list