mirror of
https://github.com/mitb-archive/filebot
synced 2025-01-11 13:58:16 -05:00
* due to permission issues File.listFiles() can return null
This commit is contained in:
parent
81f7c32b8c
commit
f85c561b78
@ -48,8 +48,6 @@ import javax.swing.SwingUtilities;
|
|||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
import net.miginfocom.swing.MigLayout;
|
|
||||||
import net.sf.ehcache.CacheManager;
|
|
||||||
import net.filebot.cli.ArgumentBean;
|
import net.filebot.cli.ArgumentBean;
|
||||||
import net.filebot.cli.ArgumentProcessor;
|
import net.filebot.cli.ArgumentProcessor;
|
||||||
import net.filebot.cli.CmdlineOperations;
|
import net.filebot.cli.CmdlineOperations;
|
||||||
@ -63,6 +61,8 @@ import net.filebot.util.ByteBufferInputStream;
|
|||||||
import net.filebot.util.PreferencesMap.PreferencesEntry;
|
import net.filebot.util.PreferencesMap.PreferencesEntry;
|
||||||
import net.filebot.util.TeePrintStream;
|
import net.filebot.util.TeePrintStream;
|
||||||
import net.filebot.web.CachedResource;
|
import net.filebot.web.CachedResource;
|
||||||
|
import net.miginfocom.swing.MigLayout;
|
||||||
|
import net.sf.ehcache.CacheManager;
|
||||||
|
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
@ -464,7 +464,7 @@ public class Main {
|
|||||||
isNewCache = true;
|
isNewCache = true;
|
||||||
|
|
||||||
// delete all files related to previous cache instances
|
// delete all files related to previous cache instances
|
||||||
for (File it : cache.listFiles()) {
|
for (File it : getChildren(cache)) {
|
||||||
if (!it.equals(lockFile)) {
|
if (!it.equals(lockFile)) {
|
||||||
delete(cache);
|
delete(cache);
|
||||||
}
|
}
|
||||||
|
@ -165,7 +165,7 @@ public class ArgumentBean {
|
|||||||
if (recursive) {
|
if (recursive) {
|
||||||
files.addAll(listFiles(file));
|
files.addAll(listFiles(file));
|
||||||
} else {
|
} else {
|
||||||
files.addAll(asList(file.listFiles()));
|
files.addAll(getChildren(file));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
files.add(file);
|
files.add(file);
|
||||||
|
@ -57,11 +57,7 @@ public class ScriptShellMethods {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static List<File> listFiles(File self, Closure<?> closure) {
|
public static List<File> listFiles(File self, Closure<?> closure) {
|
||||||
File[] files = self.listFiles();
|
return (List<File>) DefaultGroovyMethods.findAll(FileUtilities.getChildren(self), closure);
|
||||||
if (files == null)
|
|
||||||
return emptyList();
|
|
||||||
|
|
||||||
return (List<File>) DefaultGroovyMethods.findAll(asList(files), closure);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isVideo(File self) {
|
public static boolean isVideo(File self) {
|
||||||
@ -101,11 +97,7 @@ public class ScriptShellMethods {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasFile(File self, Closure<?> closure) {
|
public static boolean hasFile(File self, Closure<?> closure) {
|
||||||
File[] files = self.listFiles();
|
return DefaultGroovyMethods.find(FileUtilities.getChildren(self), closure) != null;
|
||||||
if (files == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return DefaultGroovyMethods.find(asList(files), closure) != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<File> getFiles(File self) {
|
public static List<File> getFiles(File self) {
|
||||||
@ -163,7 +155,7 @@ public class ScriptShellMethods {
|
|||||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||||
File folder = dir.toFile();
|
File folder = dir.toFile();
|
||||||
|
|
||||||
if (FileUtilities.filter(asList(folder.listFiles()), VIDEO_FILES).size() > 0 || MediaDetection.isDiskFolder(folder)) {
|
if (FileUtilities.filter(FileUtilities.getChildren(folder), VIDEO_FILES).size() > 0 || MediaDetection.isDiskFolder(folder)) {
|
||||||
mediaFolders.add(folder);
|
mediaFolders.add(folder);
|
||||||
return FileVisitResult.SKIP_SUBTREE;
|
return FileVisitResult.SKIP_SUBTREE;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
|
|
||||||
package net.filebot.ui.analyze;
|
package net.filebot.ui.analyze;
|
||||||
|
|
||||||
|
|
||||||
import static net.filebot.ui.NotificationLogging.*;
|
import static net.filebot.ui.NotificationLogging.*;
|
||||||
|
import static net.filebot.util.FileUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -14,25 +13,20 @@ import net.filebot.ui.analyze.FileTree.FolderNode;
|
|||||||
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
|
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
|
||||||
import net.filebot.util.ExceptionUtilities;
|
import net.filebot.util.ExceptionUtilities;
|
||||||
import net.filebot.util.FastFile;
|
import net.filebot.util.FastFile;
|
||||||
import net.filebot.util.FileUtilities;
|
|
||||||
|
|
||||||
|
|
||||||
class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<AbstractTreeNode> {
|
class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<AbstractTreeNode> {
|
||||||
|
|
||||||
private final FileTree tree;
|
private final FileTree tree;
|
||||||
|
|
||||||
|
|
||||||
public FileTreeTransferablePolicy(FileTree tree) {
|
public FileTreeTransferablePolicy(FileTree tree) {
|
||||||
this.tree = tree;
|
this.tree = tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean accept(List<File> files) {
|
protected boolean accept(List<File> files) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clear() {
|
protected void clear() {
|
||||||
super.clear();
|
super.clear();
|
||||||
@ -40,7 +34,6 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
|
|||||||
tree.clear();
|
tree.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process(List<AbstractTreeNode> chunks) {
|
protected void process(List<AbstractTreeNode> chunks) {
|
||||||
FolderNode root = tree.getRoot();
|
FolderNode root = tree.getRoot();
|
||||||
@ -52,13 +45,11 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
|
|||||||
tree.getModel().reload();
|
tree.getModel().reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process(Exception e) {
|
protected void process(Exception e) {
|
||||||
UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e);
|
UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void load(List<File> files) {
|
protected void load(List<File> files) {
|
||||||
try {
|
try {
|
||||||
@ -74,14 +65,13 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private AbstractTreeNode getTreeNode(File file) throws InterruptedException {
|
private AbstractTreeNode getTreeNode(File file) throws InterruptedException {
|
||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new InterruptedException();
|
throw new InterruptedException();
|
||||||
|
|
||||||
File[] files = file.listFiles();
|
if (file.isDirectory()) {
|
||||||
if (files != null && file.isDirectory()) {
|
List<File> files = getChildren(file);
|
||||||
FolderNode node = new FolderNode(FileUtilities.getFolderName(file), files.length);
|
FolderNode node = new FolderNode(getFolderName(file), files.size());
|
||||||
|
|
||||||
// add folders first
|
// add folders first
|
||||||
for (File f : files) {
|
for (File f : files) {
|
||||||
@ -102,7 +92,6 @@ class FileTreeTransferablePolicy extends BackgroundFileTransferablePolicy<Abstra
|
|||||||
return new FileNode(file);
|
return new FileNode(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFileFilterDescription() {
|
public String getFileFilterDescription() {
|
||||||
return "files and folders";
|
return "files and folders";
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.filebot.ui.rename;
|
package net.filebot.ui.rename;
|
||||||
|
|
||||||
import static java.util.Arrays.*;
|
|
||||||
import static net.filebot.MediaTypes.*;
|
import static net.filebot.MediaTypes.*;
|
||||||
import static net.filebot.ui.transfer.FileTransferable.*;
|
import static net.filebot.ui.transfer.FileTransferable.*;
|
||||||
import static net.filebot.util.FileUtilities.*;
|
import static net.filebot.util.FileUtilities.*;
|
||||||
@ -11,7 +10,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.TreeSet;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -91,10 +89,7 @@ class FilesListTransferablePolicy extends FileTransferablePolicy {
|
|||||||
} else if (!recursive || f.isFile() || MediaDetection.isDiskFolder(f)) {
|
} else if (!recursive || f.isFile() || MediaDetection.isDiskFolder(f)) {
|
||||||
entries.add(f);
|
entries.add(f);
|
||||||
} else if (f.isDirectory()) {
|
} else if (f.isDirectory()) {
|
||||||
File[] children = f.listFiles();
|
queue.addAll(0, sortByUniquePath(getChildren(f))); // FORCE NATURAL FILE ORDER
|
||||||
if (children != null) {
|
|
||||||
queue.addAll(0, new TreeSet<File>(asList(children))); // FORCE NATURAL FILE ORDER
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
|
|
||||||
package net.filebot.ui.sfv;
|
package net.filebot.ui.sfv;
|
||||||
|
|
||||||
|
|
||||||
import static java.util.Collections.*;
|
import static java.util.Collections.*;
|
||||||
import static net.filebot.hash.VerificationUtilities.*;
|
import static net.filebot.hash.VerificationUtilities.*;
|
||||||
import static net.filebot.ui.NotificationLogging.*;
|
import static net.filebot.ui.NotificationLogging.*;
|
||||||
@ -22,25 +20,21 @@ import net.filebot.hash.VerificationFileReader;
|
|||||||
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
|
import net.filebot.ui.transfer.BackgroundFileTransferablePolicy;
|
||||||
import net.filebot.util.ExceptionUtilities;
|
import net.filebot.util.ExceptionUtilities;
|
||||||
|
|
||||||
|
|
||||||
class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<ChecksumCell> {
|
class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<ChecksumCell> {
|
||||||
|
|
||||||
private final ChecksumTableModel model;
|
private final ChecksumTableModel model;
|
||||||
private final ChecksumComputationService computationService;
|
private final ChecksumComputationService computationService;
|
||||||
|
|
||||||
|
|
||||||
public ChecksumTableTransferablePolicy(ChecksumTableModel model, ChecksumComputationService checksumComputationService) {
|
public ChecksumTableTransferablePolicy(ChecksumTableModel model, ChecksumComputationService checksumComputationService) {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.computationService = checksumComputationService;
|
this.computationService = checksumComputationService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean accept(List<File> files) {
|
protected boolean accept(List<File> files) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void clear() {
|
protected void clear() {
|
||||||
super.clear();
|
super.clear();
|
||||||
@ -49,7 +43,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
model.clear();
|
model.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void prepare(List<File> files) {
|
protected void prepare(List<File> files) {
|
||||||
if (files.size() == 1 && getHashType(files.get(0)) != null) {
|
if (files.size() == 1 && getHashType(files.get(0)) != null) {
|
||||||
@ -57,23 +50,19 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process(List<ChecksumCell> chunks) {
|
protected void process(List<ChecksumCell> chunks) {
|
||||||
model.addAll(chunks);
|
model.addAll(chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process(Exception e) {
|
protected void process(Exception e) {
|
||||||
UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e);
|
UILogger.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private final ThreadLocal<ExecutorService> executor = new ThreadLocal<ExecutorService>();
|
private final ThreadLocal<ExecutorService> executor = new ThreadLocal<ExecutorService>();
|
||||||
private final ThreadLocal<VerificationTracker> verificationTracker = new ThreadLocal<VerificationTracker>();
|
private final ThreadLocal<VerificationTracker> verificationTracker = new ThreadLocal<VerificationTracker>();
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void load(List<File> files) throws IOException {
|
protected void load(List<File> files) throws IOException {
|
||||||
// initialize drop parameters
|
// initialize drop parameters
|
||||||
@ -87,7 +76,7 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
}
|
}
|
||||||
// handle single folder drop
|
// handle single folder drop
|
||||||
else if (files.size() == 1 && files.get(0).isDirectory()) {
|
else if (files.size() == 1 && files.get(0).isDirectory()) {
|
||||||
for (File file : files.get(0).listFiles()) {
|
for (File file : getChildren(files.get(0))) {
|
||||||
load(file, null, files.get(0));
|
load(file, null, files.get(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +98,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void loadVerificationFile(File file, HashType type) throws IOException, InterruptedException {
|
protected void loadVerificationFile(File file, HashType type) throws IOException, InterruptedException {
|
||||||
VerificationFileReader parser = new VerificationFileReader(createTextReader(file), type.getFormat());
|
VerificationFileReader parser = new VerificationFileReader(createTextReader(file), type.getFormat());
|
||||||
|
|
||||||
@ -137,7 +125,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void load(File absoluteFile, File relativeFile, File root) throws IOException, InterruptedException {
|
protected void load(File absoluteFile, File relativeFile, File root) throws IOException, InterruptedException {
|
||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new InterruptedException();
|
throw new InterruptedException();
|
||||||
@ -151,7 +138,7 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
|
|
||||||
if (absoluteFile.isDirectory()) {
|
if (absoluteFile.isDirectory()) {
|
||||||
// load all files in the file tree
|
// load all files in the file tree
|
||||||
for (File child : absoluteFile.listFiles()) {
|
for (File child : getChildren(absoluteFile)) {
|
||||||
load(child, relativeFile, root);
|
load(child, relativeFile, root);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -170,7 +157,6 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected ChecksumCell createComputationCell(String name, File root, HashType hash) {
|
protected ChecksumCell createComputationCell(String name, File root, HashType hash) {
|
||||||
ChecksumCell cell = new ChecksumCell(name, root, new ChecksumComputationTask(new File(root, name), hash));
|
ChecksumCell cell = new ChecksumCell(name, root, new ChecksumComputationTask(new File(root, name), hash));
|
||||||
|
|
||||||
@ -180,13 +166,11 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFileFilterDescription() {
|
public String getFileFilterDescription() {
|
||||||
return "files, folders and sfv files";
|
return "files, folders and sfv files";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class VerificationTracker {
|
private static class VerificationTracker {
|
||||||
|
|
||||||
private final Map<File, Integer> seen = new HashMap<File, Integer>();
|
private final Map<File, Integer> seen = new HashMap<File, Integer>();
|
||||||
@ -195,12 +179,10 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
|
|
||||||
private final int maxDepth;
|
private final int maxDepth;
|
||||||
|
|
||||||
|
|
||||||
public VerificationTracker(int maxDepth) {
|
public VerificationTracker(int maxDepth) {
|
||||||
this.maxDepth = maxDepth;
|
this.maxDepth = maxDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<File, String> getHashByVerificationFile(File file) throws IOException {
|
public Map<File, String> getHashByVerificationFile(File file) throws IOException {
|
||||||
// cache all verification files
|
// cache all verification files
|
||||||
File folder = file.getParentFile();
|
File folder = file.getParentFile();
|
||||||
@ -250,12 +232,10 @@ class ChecksumTableTransferablePolicy extends BackgroundFileTransferablePolicy<C
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public HashType getVerificationFileType(File verificationFile) {
|
public HashType getVerificationFileType(File verificationFile) {
|
||||||
return types.get(verificationFile);
|
return types.get(verificationFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Completely read a verification file and resolve all relative file paths against a given base folder
|
* Completely read a verification file and resolve all relative file paths against a given base folder
|
||||||
*/
|
*/
|
||||||
|
@ -380,6 +380,17 @@ public final class FileUtilities {
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<File> getChildren(File file) {
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
|
||||||
|
// children array may be null if folder permissions do not allow listing of files
|
||||||
|
if (files == null) {
|
||||||
|
return asList(new File[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return asList(files);
|
||||||
|
}
|
||||||
|
|
||||||
public static List<File> listFiles(File... folders) {
|
public static List<File> listFiles(File... folders) {
|
||||||
return listFiles(asList(folders));
|
return listFiles(asList(folders));
|
||||||
}
|
}
|
||||||
@ -417,11 +428,7 @@ public final class FileUtilities {
|
|||||||
if (depth > maxDepth)
|
if (depth > maxDepth)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
File[] children = folder.listFiles();
|
for (File it : getChildren(folder)) {
|
||||||
if (children == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
for (File it : children) {
|
|
||||||
if (!addHidden && it.isHidden()) // ignore hidden files
|
if (!addHidden && it.isHidden()) // ignore hidden files
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
package net.filebot.util;
|
package net.filebot.util;
|
||||||
|
|
||||||
|
import static net.filebot.util.FileUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -10,19 +10,15 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
public final class TemporaryFolder {
|
public final class TemporaryFolder {
|
||||||
|
|
||||||
private static final Map<String, TemporaryFolder> folders = new HashMap<String, TemporaryFolder>();
|
private static final Map<String, TemporaryFolder> folders = new HashMap<String, TemporaryFolder>();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a {@link TemporaryFolder} instance for a given name. The actual directory will be
|
* Get a {@link TemporaryFolder} instance for a given name. The actual directory will be created lazily (e.g. when a file is created). The name of the directory will start with the given name (lower-case) and contain a unique id, so multiple application instances may run at the same time without the risk of interference.
|
||||||
* created lazily (e.g. when a file is created). The name of the directory will start with
|
|
||||||
* the given name (lower-case) and contain a unique id, so multiple application instances
|
|
||||||
* may run at the same time without the risk of interference.
|
|
||||||
*
|
*
|
||||||
* @param name case-insensitive name of a temporary folder (e.g. application name)
|
* @param name
|
||||||
|
* case-insensitive name of a temporary folder (e.g. application name)
|
||||||
* @return temporary folder for this name
|
* @return temporary folder for this name
|
||||||
*/
|
*/
|
||||||
public static TemporaryFolder getFolder(String name) {
|
public static TemporaryFolder getFolder(String name) {
|
||||||
@ -44,7 +40,6 @@ public final class TemporaryFolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete all temporary folders on shutdown
|
* Delete all temporary folders on shutdown
|
||||||
*/
|
*/
|
||||||
@ -64,18 +59,18 @@ public final class TemporaryFolder {
|
|||||||
|
|
||||||
private final File root;
|
private final File root;
|
||||||
|
|
||||||
|
|
||||||
private TemporaryFolder(File root) {
|
private TemporaryFolder(File root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an empty file in this temporary folder.
|
* Create an empty file in this temporary folder.
|
||||||
*
|
*
|
||||||
* @param name name of the file
|
* @param name
|
||||||
|
* name of the file
|
||||||
* @return newly created file
|
* @return newly created file
|
||||||
* @throws IOException if an I/O error occurred
|
* @throws IOException
|
||||||
|
* if an I/O error occurred
|
||||||
*/
|
*/
|
||||||
public File createFile(String name) throws IOException {
|
public File createFile(String name) throws IOException {
|
||||||
|
|
||||||
@ -86,32 +81,28 @@ public final class TemporaryFolder {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an empty file in this temporary folder, using the given prefix and suffix to
|
* Creates an empty file in this temporary folder, using the given prefix and suffix to generate its name.
|
||||||
* generate its name.
|
|
||||||
*
|
*
|
||||||
* @param prefix The prefix string to be used in generating the file's name; must be at
|
* @param prefix
|
||||||
* least three characters long
|
* The prefix string to be used in generating the file's name; must be at least three characters long
|
||||||
* @param suffix The suffix string to be used in generating the file's name; may be null,
|
* @param suffix
|
||||||
* in which case the suffix ".tmp" will be used
|
* The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
|
||||||
* @return An abstract pathname denoting a newly-created empty file
|
* @return An abstract pathname denoting a newly-created empty file
|
||||||
* @throws IOException If a file could not be created
|
* @throws IOException
|
||||||
|
* If a file could not be created
|
||||||
* @see File#createTempFile(String, String)
|
* @see File#createTempFile(String, String)
|
||||||
*/
|
*/
|
||||||
public File createFile(String prefix, String suffix) throws IOException {
|
public File createFile(String prefix, String suffix) throws IOException {
|
||||||
return File.createTempFile(prefix, suffix, getFolder());
|
return File.createTempFile(prefix, suffix, getFolder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean deleteFile(String name) {
|
public boolean deleteFile(String name) {
|
||||||
return new File(getFolder(), name).delete();
|
return new File(getFolder(), name).delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the {@link File} object for this {@link TemporaryFolder}. The actual directory
|
* Retrieve the {@link File} object for this {@link TemporaryFolder}. The actual directory for the {@link TemporaryFolder} instance will be created by this method.
|
||||||
* for the {@link TemporaryFolder} instance will be created by this method.
|
|
||||||
*
|
*
|
||||||
* @return the {@link File} object for this {@link TemporaryFolder}
|
* @return the {@link File} object for this {@link TemporaryFolder}
|
||||||
*/
|
*/
|
||||||
@ -123,12 +114,10 @@ public final class TemporaryFolder {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public TemporaryFolder subFolder(String name) {
|
public TemporaryFolder subFolder(String name) {
|
||||||
return new TemporaryFolder(new File(getFolder(), name));
|
return new TemporaryFolder(new File(getFolder(), name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<File> list(boolean recursive) {
|
public List<File> list(boolean recursive) {
|
||||||
List<File> list = new ArrayList<File>();
|
List<File> list = new ArrayList<File>();
|
||||||
|
|
||||||
@ -137,10 +126,9 @@ public final class TemporaryFolder {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void list(File file, List<File> list, boolean recursive) {
|
private void list(File file, List<File> list, boolean recursive) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
for (File entry : file.listFiles()) {
|
for (File entry : getChildren(file)) {
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
if (recursive) {
|
if (recursive) {
|
||||||
list(entry, list, recursive);
|
list(entry, list, recursive);
|
||||||
@ -152,20 +140,19 @@ public final class TemporaryFolder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void delete() {
|
public void delete() {
|
||||||
delete(root);
|
delete(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete files/folders recursively
|
* Delete files/folders recursively
|
||||||
*
|
*
|
||||||
* @param file file/folder that will be deleted
|
* @param file
|
||||||
|
* file/folder that will be deleted
|
||||||
*/
|
*/
|
||||||
private void delete(File file) {
|
private void delete(File file) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
for (File entry : file.listFiles()) {
|
for (File entry : getChildren(file)) {
|
||||||
delete(entry);
|
delete(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user