[Arakhnę-Dev] [206] * Add functions FileSystem#delete and FileSystem#deleteOnExit.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 206
Author:   galland
Date:     2011-02-15 22:24:18 +0100 (Tue, 15 Feb 2011)
Log Message:
-----------
* Add functions FileSystem#delete and FileSystem#deleteOnExit.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2011-02-14 17:51:20 UTC (rev 205)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2011-02-15 21:24:18 UTC (rev 206)
@@ -111,6 +111,8 @@
 	 */
 	public static final String JAR_URL_FILE_ROOT = "!/"; //$NON-NLS-1$
 	
+	private static final DeleteOnExitHook deleteOnExitHook = new DeleteOnExitHook();
+	
 	/** Decode the given string.
 	 * 
 	 * @param s
@@ -1041,6 +1043,83 @@
 		return filename;
 	}
 
+	/** Delete the given directory and all its subdirectories.
+	 * If the given <var>file</var> is a directory, its
+	 * content and the <var>file</var> itself are recursivelly removed. 
+	 * 
+	 * @param file is the file to delete.
+	 * @throws IOException
+	 * @see File#delete() for the deletion on a file only.
+	 * @see File#mkdir() to create a directory.
+	 * @see File#mkdirs() to create a directory and all its parents.
+	 * @since 6.0
+	 */
+	public static void delete(File file) throws IOException {
+		if (file!=null) {
+			LinkedList<File> candidates = new LinkedList<File>();
+			candidates.add(file);
+			File f;
+			File[] children;
+			while (candidates.isEmpty()) {
+				f = candidates.getFirst();
+				if (f.isDirectory()) {
+					children = f.listFiles();
+					if (children!=null) {
+						// Non empty directory
+						for(File c : children) {
+							candidates.push(c);
+						}
+					}
+					else {
+						// empty directory
+						candidates.removeFirst();
+						f.delete();
+					}
+				}
+				else {
+					// not a directory
+					candidates.removeFirst();
+					f.delete();
+				}
+			}
+		}
+	}
+	
+	/** Delete the given directory and all its subdirectories when the JVM is exiting.
+	 * If the given <var>file</var> is a directory, its
+	 * content and the <var>file</var> itself are recursivelly removed.
+	 * <p>
+	 * To cancel this action, see {@link #undeleteOnExit(File)}. 
+	 * 
+	 * @param file is the file to delete.
+	 * @throws IOException
+	 * @see File#deleteOnExit() for the deletion on a file only.
+	 * @see File#mkdir() to create a directory.
+	 * @see File#mkdirs() to create a directory and all its parents.
+	 * @since 6.0
+	 */
+	public static void deleteOnExit(File file) throws IOException {
+		if (file!=null) {
+			deleteOnExitHook.add(file);
+		}
+	}
+
+	/** Cancel the deletion of the given directory and all its subdirectories when the JVM is exiting.
+	 * 
+	 * @param file is the file to undelete.
+	 * @throws IOException
+	 * @see #deleteOnExit(File)
+	 * @see File#deleteOnExit() for the deletion on a file only.
+	 * @see File#mkdir() to create a directory.
+	 * @see File#mkdirs() to create a directory and all its parents.
+	 * @since 6.0
+	 */
+	public static void undeleteOnExit(File file) throws IOException {
+		if (file!=null) {
+			deleteOnExitHook.remove(file);
+		}
+	}
+
 	/** Copy the first file into the second file.
 	 * <p>
 	 * The content of the second file will be lost.
@@ -1051,8 +1130,26 @@
 	 * @param out is the target file
 	 * @throws IOException in case of error.
 	 * @see #fileCopy(URL, File)
+	 * @deprecated {@link #copy(File, File)}
 	 */
+	@Deprecated
 	public static void fileCopy(File in, File out) throws IOException {
+		copy(in, out);
+	}
+
+	/** Copy the first file into the second file.
+	 * <p>
+	 * The content of the second file will be lost.
+	 * This copy function allows to do a copy between two different
+	 * partitions.
+	 * 
+	 * @param in is the file to copy.
+	 * @param out is the target file
+	 * @throws IOException in case of error.
+	 * @see #copy(URL, File)
+	 * @since 6.0
+	 */
+	public static void copy(File in, File out) throws IOException {
 		assert(in!=null);
 		assert(out!=null);
 		FileChannel inChannel = new FileInputStream(in).getChannel();
@@ -1088,8 +1185,26 @@
 	 * @param out is the target file
 	 * @throws IOException in case of error.
 	 * @see #fileCopy(File, File)
+	 * @deprecated {@link #copy(URL, File)}
 	 */
+	@Deprecated
 	public static void fileCopy(URL in, File out) throws IOException {
+		copy(in, out);
+	}
+
+	/** Copy the first file into the second file.
+	 * <p>
+	 * The content of the second file will be lost.
+	 * This copy function allows to do a copy between two different
+	 * partitions.
+	 * 
+	 * @param in is the file to copy.
+	 * @param out is the target file
+	 * @throws IOException in case of error.
+	 * @see #copy(File, File)
+	 * @since 6.0
+	 */
+	public static void copy(URL in, File out) throws IOException {
 		assert(in!=null);
 		assert(out!=null);
 		URLConnection connection = in.openConnection();
@@ -2347,5 +2462,75 @@
 		return url;
 	}
 
+	/** Hook to recursively delete files on JVM exit. 
+	 * 
+	 * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+	 * @version $Name$ $Revision$ $Date$
+	 * @mavengroupid $GroupId$
+	 * @mavenartifactid $ArtifactId$
+	 * @since 6.0
+	 */
+	private static class DeleteOnExitHook extends Thread {
+
+		private List<File> filesToDelete = null;
+
+		public DeleteOnExitHook() {
+			setName("DeleteOnExitHook"); //$NON-NLS-1$
+		}
+		
+		/**
+		 * {@inheritDoc}
+		 */
+		@Override
+		public void run() {
+			synchronized(this) {
+				if (this.filesToDelete!=null) {
+					for(File f : this.filesToDelete) {
+						try {
+							delete(f);
+						}
+						catch(IOException e) {
+							// Ignore error
+						}
+					}
+					this.filesToDelete.clear();
+					this.filesToDelete = null;
+				}
+			}
+		}
+		
+		/** Add a file to delete.
+		 * 
+		 * @param file
+		 */
+		public void add(File file) {
+			assert(file!=null);
+			synchronized(this) {
+				if (this.filesToDelete==null) {
+					this.filesToDelete = new LinkedList<File>();
+					Runtime.getRuntime().addShutdownHook(this);
+				}
+				this.filesToDelete.add(file);
+			}
+		}
+		
+		/** Remove a file to delete.
+		 * 
+		 * @param file
+		 */
+		public void remove(File file) {
+			synchronized(this) {
+				if (this.filesToDelete!=null) {
+					this.filesToDelete.remove(file);
+					if (this.filesToDelete.isEmpty()) {
+						this.filesToDelete = null;
+						Runtime.getRuntime().removeShutdownHook(this);
+					}
+				}
+			}
+		}
+		
+	}
+	
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/