DirectoryIteratorpublic class DirectoryIterator extends Object implements ClassFileIteratorAn iterator which iterates through the contents of a java directory. The
iterator should be created with the directory at the root of the Java
namespace. |
Fields Summary |
---|
private Stack | enumStackThis is a stack of current iterators supporting the depth first
traversal of the directory tree. | private Enumeration | currentEnumThe current directory iterator. As directories encounter lower level
directories, the current iterator is pushed onto the iterator stack
and a new iterator over the sub directory becomes the current
directory. This implements a depth first traversal of the directory
namespace. |
Constructors Summary |
---|
public DirectoryIterator(File rootDirectory, boolean changeInto)Creates a directory iterator. The directory iterator is created to
scan the root directory. If the changeInto flag is given, then the
entries returned will be relative to this directory and not the
current directory.
super();
enumStack = new Stack();
Vector filesInRoot = getDirectoryEntries(rootDirectory);
currentEnum = filesInRoot.elements();
|
Methods Summary |
---|
private java.util.Vector | getDirectoryEntries(java.io.File directory)Get a vector covering all the entries (files and subdirectories in a
directory).
Vector files = new Vector();
// File[] filesInDir = directory.listFiles();
String[] filesInDir = directory.list();
if (filesInDir != null) {
int length = filesInDir.length;
for (int i = 0; i < length; ++i) {
files.addElement(new File(directory, filesInDir[i]));
}
}
return files;
| public ClassFile | getNextClassFile()Template method to allow subclasses to supply elements for the
iteration. The directory iterator maintains a stack of iterators
covering each level in the directory hierarchy. The current iterator
covers the current directory being scanned. If the next entry in that
directory is a subdirectory, the current iterator is pushed onto the
stack and a new iterator is created for the subdirectory. If the
entry is a file, it is returned as the next element and the iterator
remains valid. If there are no more entries in the current directory,
the topmost iterator on the stack is popped off to become the
current iterator.
ClassFile nextElement = null;
try {
while (nextElement == null) {
if (currentEnum.hasMoreElements()) {
File element = (File) currentEnum.nextElement();
if (element.isDirectory()) {
// push the current iterator onto the stack and then
// iterate through this directory.
enumStack.push(currentEnum);
Vector files = getDirectoryEntries(element);
currentEnum = files.elements();
} else {
// we have a file. create a stream for it
FileInputStream inFileStream
= new FileInputStream(element);
if (element.getName().endsWith(".class")) {
// create a data input stream from the jar
// input stream
ClassFile javaClass = new ClassFile();
javaClass.read(inFileStream);
nextElement = javaClass;
}
}
} else {
// this iterator is exhausted. Can we pop one off the stack
if (enumStack.empty()) {
break;
} else {
currentEnum = (Enumeration) enumStack.pop();
}
}
}
} catch (IOException e) {
nextElement = null;
}
return nextElement;
|
|