2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2008-03-25 21:03:32 +00:00
|
|
|
import java.util.ArrayList;
|
2008-03-19 22:14:38 +00:00
|
|
|
import java.util.HashMap;
|
2008-03-25 21:03:32 +00:00
|
|
|
import java.util.List;
|
2008-03-19 22:14:38 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
public class TemporaryFolder {
|
|
|
|
|
|
|
|
private static final String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
|
|
|
private static final Map<String, TemporaryFolder> folders = new HashMap<String, TemporaryFolder>();
|
|
|
|
|
|
|
|
|
|
|
|
public static TemporaryFolder getFolder(String name) {
|
|
|
|
synchronized (folders) {
|
|
|
|
TemporaryFolder folder = folders.get(name);
|
|
|
|
|
|
|
|
if (folder == null) {
|
|
|
|
folder = new TemporaryFolder(new File(tmpdir, name));
|
|
|
|
folders.put(name, folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all temporary folders on shutdown
|
|
|
|
*/
|
|
|
|
static {
|
2008-03-22 21:12:34 +00:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread("TemporaryFolder Cleanup") {
|
2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (folders) {
|
|
|
|
for (TemporaryFolder folder : folders.values()) {
|
|
|
|
folder.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
folders.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private final File root;
|
|
|
|
|
|
|
|
|
|
|
|
private TemporaryFolder(File root) {
|
|
|
|
this.root = root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public File createFile(String name) throws IOException {
|
2008-03-25 21:03:32 +00:00
|
|
|
if (!root.exists()) {
|
2008-03-20 20:11:08 +00:00
|
|
|
root.mkdir();
|
2008-03-25 21:03:32 +00:00
|
|
|
}
|
2008-03-20 20:11:08 +00:00
|
|
|
|
2008-03-19 22:14:38 +00:00
|
|
|
File file = new File(root, name);
|
|
|
|
file.createNewFile();
|
2008-03-20 20:11:08 +00:00
|
|
|
|
2008-03-19 22:14:38 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void deleteFile(String name) {
|
|
|
|
File file = new File(root, name);
|
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-25 21:03:32 +00:00
|
|
|
public File getFolder() {
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TemporaryFolder createFolder(String name) {
|
|
|
|
if (!root.exists()) {
|
|
|
|
root.mkdir();
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryFolder folder = new TemporaryFolder(new File(root, name));
|
|
|
|
|
|
|
|
folder.root.mkdir();
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<File> list(boolean recursive) {
|
|
|
|
List<File> list = new ArrayList<File>();
|
|
|
|
|
|
|
|
list(root, list, recursive);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void list(File file, List<File> list, boolean recursive) {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
for (File entry : file.listFiles()) {
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
if (recursive) {
|
|
|
|
list(entry, list, recursive);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list.add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-19 22:14:38 +00:00
|
|
|
public void delete() {
|
|
|
|
delete(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete files/folders recursively
|
|
|
|
*
|
|
|
|
* @param file file/folder that will be deleted
|
|
|
|
*/
|
|
|
|
private void delete(File file) {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
for (File entry : file.listFiles()) {
|
|
|
|
delete(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|