1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 30 37 public class RecursiveFileSearch { 38 private String baseDir; 39 private FileFilter fileFilter; 40 private LinkedList <File > directoryWorkList; 41 private HashSet <String > directoriesScanned = new HashSet <String >(); 42 private ArrayList <String > resultList; 43 44 51 public RecursiveFileSearch(String baseDir, FileFilter fileFilter) { 52 this.baseDir = baseDir; 53 this.fileFilter = fileFilter; 54 this.directoryWorkList = new LinkedList <File >(); 55 this.resultList = new ArrayList <String >(); 56 } 57 58 static String bestEffortCanonicalPath(File f) { 59 try { 60 return f.getCanonicalPath(); 61 } catch (IOException e) { 62 return f.getAbsolutePath(); 63 } 64 } 65 72 public RecursiveFileSearch search() throws InterruptedException { 73 File baseFile = new File (baseDir); 74 String basePath = bestEffortCanonicalPath(baseFile); 75 directoryWorkList.add(baseFile); 76 directoriesScanned.add(basePath); 77 78 while (!directoryWorkList.isEmpty()) { 79 File dir = directoryWorkList.removeFirst(); 80 if (!dir.isDirectory()) 81 continue; 82 83 File [] contentList = dir.listFiles(); 84 if (contentList == null) continue; 85 for (File aContentList : contentList) { 86 if (Thread.interrupted()) 87 throw new InterruptedException (); 88 89 File file = aContentList; 90 91 if (!fileFilter.accept(file)) 92 continue; 93 if (file.isDirectory()) { 94 String myPath = bestEffortCanonicalPath(file); 95 if (myPath.startsWith(basePath) && directoriesScanned.add(myPath)) 96 directoryWorkList.add(file); 97 } 98 else 99 resultList.add(file.getPath()); 100 } 101 } 102 103 return this; 104 } 105 106 110 public Iterator <String > fileNameIterator() { 111 return resultList.iterator(); 112 } 113 114 } 115 116 | Popular Tags |