blob: 36bfa39a04cf1c3f5caca7f077f20fce8c5a6b8a [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +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 {
15 this(new FileReader(in));
16 }
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;
40 } catch (Exception e) {
41 // ignore
42 return null;
43 }
44 }
45
46 public void remove() {
47 if (next == null)
48 throw new UnsupportedOperationException("Cannot remove");
49 }
50
51 public void close() throws IOException {
52 reader.close();
53 }
54}