< prev index next >
test/lib/testlibrary/jdk/testlibrary/FileUtils.java
Print this page
rev 1531 : 8171415: Remove Java 7 features from testlibrary
Reviewed-by: omajid
@@ -21,18 +21,12 @@
* questions.
*/
package jdk.testlibrary;
+import java.io.File;
import java.io.IOException;
-import java.nio.file.DirectoryNotEmptyException;
-import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
-import java.nio.file.NoSuchFileException;
-import java.nio.file.Path;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
/**
@@ -56,11 +50,11 @@
* if the file is a directory and could not otherwise be deleted
* because the directory is not empty (optional specific exception)
* @throws IOException
* if an I/O error occurs
*/
- public static void deleteFileWithRetry(Path path)
+ public static void deleteFileWithRetry(File path)
throws IOException
{
try {
deleteFileWithRetry0(path);
} catch (InterruptedException x) {
@@ -80,50 +74,32 @@
* if the file is a directory and could not otherwise be deleted
* because the directory is not empty (optional specific exception)
* @throws IOException
* if an I/O error occurs
*/
- public static void deleteFileIfExistsWithRetry(Path path)
+ public static void deleteFileIfExistsWithRetry(File path)
throws IOException
{
try {
- if(Files.exists(path))
+ if (path.exists())
deleteFileWithRetry0(path);
} catch (InterruptedException x) {
throw new IOException("Interrupted while deleting.", x);
}
}
- private static void deleteFileWithRetry0(Path path)
+ private static void deleteFileWithRetry0(File path)
throws IOException, InterruptedException
{
int times = 0;
- IOException ioe = null;
- while (true) {
- try {
- Files.delete(path);
- while (Files.exists(path)) {
+ boolean result = path.delete();
+ while (!result) {
times++;
if (times > MAX_RETRY_DELETE_TIMES)
throw new IOException("File still exists after " + times + " waits.");
Thread.sleep(RETRY_DELETE_MILLIS);
- }
- break;
- } catch (NoSuchFileException | DirectoryNotEmptyException x) {
- throw x;
- } catch (IOException x) {
- // Backoff/retry in case another process is accessing the file
- times++;
- if (ioe == null)
- ioe = x;
- else
- ioe.addSuppressed(x);
-
- if (times > MAX_RETRY_DELETE_TIMES)
- throw ioe;
- Thread.sleep(RETRY_DELETE_MILLIS);
- }
+ result = path.delete();
}
}
/**
* Deletes a directory and its subdirectories, retrying if necessary.
@@ -135,60 +111,27 @@
* internally. If only one is caught, then it is re-thrown.
* If more than one exception is caught, then the second and
* following exceptions are added as suppressed exceptions of the
* first one caught, which is then re-thrown.
*/
- public static void deleteFileTreeWithRetry(Path dir)
+ public static void deleteFileTreeWithRetry(File dir)
throws IOException
{
- IOException ioe = null;
- final List<IOException> excs = deleteFileTreeUnchecked(dir);
- if (!excs.isEmpty()) {
- ioe = excs.remove(0);
- for (IOException x : excs)
- ioe.addSuppressed(x);
- }
- if (ioe != null)
- throw ioe;
- }
-
- public static List<IOException> deleteFileTreeUnchecked(Path dir) {
- final List<IOException> excs = new ArrayList<>();
- try {
- java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
- try {
- deleteFileWithRetry0(file);
- } catch (IOException x) {
- excs.add(x);
- } catch (InterruptedException x) {
- excs.add(new IOException("Interrupted while deleting.", x));
- return FileVisitResult.TERMINATE;
- }
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
- try {
- deleteFileWithRetry0(dir);
- } catch (IOException x) {
- excs.add(x);
- } catch (InterruptedException x) {
- excs.add(new IOException("Interrupted while deleting.", x));
- return FileVisitResult.TERMINATE;
- }
- return FileVisitResult.CONTINUE;
+ boolean failed = false;
+ final List<Boolean> results = deleteFileTreeUnchecked(dir);
+ failed = !results.isEmpty();
+ if (failed)
+ throw new IOException();
+ }
+
+ public static List<Boolean> deleteFileTreeUnchecked(File dir) {
+ final List<Boolean> results = new ArrayList<Boolean>();
+ for (File file : dir.listFiles()) {
+ if (file.isDirectory()) {
+ results.addAll(deleteFileTreeUnchecked(file));
+ } else {
+ results.add(file.delete());
}
- @Override
- public FileVisitResult visitFileFailed(Path file, IOException exc) {
- excs.add(exc);
- return FileVisitResult.CONTINUE;
}
- });
- } catch (IOException x) {
- excs.add(x);
- }
- return excs;
+ return results;
}
}
-
< prev index next >