aa3593b077df98a7de4e57b129a57c228c79435e
[scannotation] / src / main / java / org / scannotation / archiveiterator / FileIterator.java
1 package org.scannotation.archiveiterator;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 /**
11  * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
12  * @version $Revision: 1 $
13  */
14 public class FileIterator implements StreamIterator
15 {
16    private ArrayList files;
17    private int index = 0;
18
19    public FileIterator(File file, Filter filter)
20    {
21       files = new ArrayList();
22       try
23       {
24          create(files, file, filter);
25       }
26       catch (Exception e)
27       {
28          throw new RuntimeException(e);
29       }
30    }
31
32    protected static void create(List list, File dir, Filter filter) throws Exception
33    {
34       File[] files = dir.listFiles();
35       for (int i = 0; i < files.length; i++)
36       {
37          if (files[i].isDirectory())
38          {
39             create(list, files[i], filter);
40          }
41          else
42          {
43             if (filter == null || filter.accepts(files[i].getAbsolutePath()))
44             {
45                list.add(files[i]);
46             }
47          }
48       }
49    }
50
51    public InputStream next()
52    {
53       if (index >= files.size()) return null;
54       File fp = (File) files.get(index++);
55       try
56       {
57          return new FileInputStream(fp);
58       }
59       catch (FileNotFoundException e)
60       {
61          throw new RuntimeException(e);
62       }
63    }
64
65    public void close()
66    {
67    }
68 }