From 4ecea70e9b75546d922e43fde8f61550094119ee Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 17 Sep 2008 16:12:53 +0000 Subject: [PATCH] Bring back memoization of File.list git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@696355 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/DirectoryScanner.java | 15 ++++++++++-- .../ant/types/selectors/TokenizedPath.java | 24 +++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/org/apache/tools/ant/DirectoryScanner.java b/src/main/org/apache/tools/ant/DirectoryScanner.java index 081f8a081..2bc5382e3 100644 --- a/src/main/org/apache/tools/ant/DirectoryScanner.java +++ b/src/main/org/apache/tools/ant/DirectoryScanner.java @@ -286,6 +286,11 @@ public class DirectoryScanner */ private Map fileListMap = new HashMap(); + /** + * Uses fileListMap to cache directory listings. + */ + private final TokenizedPath.FileLister fileLister = new CachedFileLister(); + /** * List of all scanned directories. * @@ -957,7 +962,8 @@ public class DirectoryScanner : FILE_UTILS.removeLeadingPath(canonBase, getCanonicalFile(myfile)); if (!path.equals(currentelement) || ON_VMS) { - myfile = currentPath.findFile(basedir, true); + myfile = currentPath.findFile(basedir, true, + fileLister); if (myfile != null && basedir != null) { currentelement = FILE_UTILS.removeLeadingPath( basedir, myfile); @@ -974,7 +980,7 @@ public class DirectoryScanner } if ((myfile == null || !myfile.exists()) && !isCaseSensitive()) { - File f = currentPath.findFile(basedir, false); + File f = currentPath.findFile(basedir, false, fileLister); if (f != null && f.exists()) { // adapt currentelement to the case we've // actually found @@ -1896,4 +1902,9 @@ public class DirectoryScanner return new File(getCanonicalPath(file)); } + private class CachedFileLister implements TokenizedPath.FileLister { + public String[] list(File f) { + return DirectoryScanner.this.list(f); + } + } } diff --git a/src/main/org/apache/tools/ant/types/selectors/TokenizedPath.java b/src/main/org/apache/tools/ant/types/selectors/TokenizedPath.java index aecd55da8..4e222b1e5 100644 --- a/src/main/org/apache/tools/ant/types/selectors/TokenizedPath.java +++ b/src/main/org/apache/tools/ant/types/selectors/TokenizedPath.java @@ -105,7 +105,7 @@ public class TokenizedPath { * @param cs whether to scan case-sensitively. * @return File object that points to the file in question or null. */ - public File findFile(File base, final boolean cs) { + public File findFile(File base, final boolean cs, FileLister fileLister) { String[] tokens = tokenizedPath; if (FileUtils.isAbsolutePath(path)) { if (base == null) { @@ -123,7 +123,7 @@ public class TokenizedPath { tokens = SelectorUtils.tokenizePathAsArray(s); } } - return findFile(base, tokens, cs); + return findFile(base, tokens, cs, fileLister); } /** @@ -170,12 +170,12 @@ public class TokenizedPath { * @return File object that points to the file in question or null. */ private static File findFile(File base, final String[] pathElements, - final boolean cs) { + final boolean cs, FileLister fileLister) { for (int current = 0; current < pathElements.length; current++) { if (!base.isDirectory()) { return null; } - String[] files = base.list(); + String[] files = fileLister.list(base); if (files == null) { throw new BuildException("IO error scanning directory " + base.getAbsolutePath()); @@ -206,4 +206,20 @@ public class TokenizedPath { public TokenizedPattern toPattern() { return new TokenizedPattern(path, tokenizedPath); } + + /** + * Helper that obtains the listing of a directory. + */ + public static interface FileLister { + String[] list(File file); + } + + /** + * Default implementation using File.list(). + */ + public static final class DefaultLister implements FileLister { + public String[] list(File file) { + return file.list(); + } + } }