1 package org.scannotation.archiveiterator;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.InputStream;
7 import java.util.ArrayList;
11 * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
12 * @version $Revision: 1 $
14 public class FileIterator implements StreamIterator
16 private ArrayList files;
17 private int index = 0;
19 public FileIterator(File file, Filter filter)
21 files = new ArrayList();
24 create(files, file, filter);
28 throw new RuntimeException(e);
31 protected static void create(List list, File dir, Filter filter) throws Exception
33 create(list, dir, filter, dir.getCanonicalPath());
35 protected static void create(List list, File dir, Filter filter, String prefix) throws Exception
37 File[] files = dir.listFiles();
38 for (int i = 0; i < files.length; i++)
40 if (files[i].isDirectory())
42 create(list, files[i], filter, prefix);
46 String path = files[i].getCanonicalPath();
47 String relativePath = path.substring(prefix.length() + 1);
48 if (File.separatorChar == '\\')
49 relativePath = relativePath.replace('\\', '/');
50 if (filter == null || filter.accepts(relativePath))
58 public InputStream next()
60 if (index >= files.size()) return null;
61 File fp = (File) files.get(index++);
64 return new FileInputStream(fp);
66 catch (FileNotFoundException e)
68 throw new RuntimeException(e);