blob: 0670712a84843fdad6094078106d47eeca13da80 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.collections;
2
3import java.io.*;
4import java.util.*;
5
6public class LineCollection implements Iterator<String>, Closeable {
7 final BufferedReader reader;
8 String next;
9
10 public LineCollection(InputStream in) throws IOException {
11 this(new InputStreamReader(in, "UTF8"));
12 }
13
14 public LineCollection(File in) throws IOException {
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 this(new InputStreamReader(new FileInputStream(in), "UTF-8"));
Stuart McCullochbb014372012-06-07 21:57:32 +000016 }
17
18 public LineCollection(Reader reader) throws IOException {
19 this(new BufferedReader(reader));
20 }
21
22 public LineCollection(BufferedReader reader) throws IOException {
23 this.reader = reader;
24 next = reader.readLine();
25 }
26
27 public boolean hasNext() {
28 return next != null;
29 }
30
31 public String next() {
32 if (next == null)
33 throw new IllegalStateException("Iterator has finished");
34 try {
35 String result = next;
36 next = reader.readLine();
37 if (next == null)
38 reader.close();
39 return result;
Stuart McCulloch2286f232012-06-15 13:27:53 +000040 }
41 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +000042 // ignore
43 return null;
44 }
45 }
46
47 public void remove() {
48 if (next == null)
49 throw new UnsupportedOperationException("Cannot remove");
50 }
51
52 public void close() throws IOException {
53 reader.close();
54 }
55}