1 package net.sf.openrocket.gui.help.tours;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
8 import java.nio.charset.Charset;
9 import java.util.Iterator;
10 import java.util.NoSuchElementException;
12 import net.sf.openrocket.util.BugException;
15 * Read from a Reader object one line at a time, ignoring blank lines,
16 * preceding and trailing whitespace and comment lines starting with '#'.
18 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20 public class TextLineReader implements Iterator<String> {
22 private static final Charset UTF8 = Charset.forName("UTF-8");
26 private final BufferedReader reader;
28 private String next = null;
31 * Read from an input stream with UTF-8 character encoding.
33 public TextLineReader(InputStream inputStream) {
34 this(new InputStreamReader(inputStream, UTF8));
41 public TextLineReader(Reader reader) {
42 if (reader instanceof BufferedReader) {
43 this.reader = (BufferedReader) reader;
45 this.reader = new BufferedReader(reader);
51 * Test whether the file has more lines available.
54 public boolean hasNext() {
61 } catch (IOException e) {
62 throw new BugException(e);
70 * Retrieve the next non-blank, non-comment line.
73 public String next() {
80 throw new NoSuchElementException("End of file reached");
85 * Peek what the next line would be.
87 public String peek() {
92 throw new NoSuchElementException("End of file reached");
96 private String readLine() throws IOException {
100 String line = reader.readLine();
105 // Check whether to accept the line
107 if (line.length() > 0 && line.charAt(0) != '#') {
116 public void remove() {
117 throw new UnsupportedOperationException("Remove not supported");