3 import java.io.BufferedReader;
\r
6 * A simple class to read a designated number of bytes from a
\r
7 * file and then return them line by line, skipping lines that
\r
8 * start with #, and including the \n or \r characters at line ends.
\r
10 * Generally useful for determining what sort of data a file contains.
\r
13 public class LimitedLineReader {
\r
16 private int ichCurrent;
\r
18 public LimitedLineReader(BufferedReader bufferedReader, int readLimit)
\r
20 bufferedReader.mark(readLimit + 1);
\r
21 buf = new char[readLimit];
\r
22 cchBuf = Math.max(bufferedReader.read(buf, 0, readLimit), 0);
\r
24 bufferedReader.reset();
\r
27 public String getHeader(int n) {
\r
28 return (n == 0 ? new String(buf) : new String(buf, 0, Math.min(cchBuf, n)));
\r
31 public String readLineWithNewline() {
\r
32 while (ichCurrent < cchBuf) {
\r
33 int ichBeginningOfLine = ichCurrent;
\r
35 while (ichCurrent < cchBuf &&
\r
36 (ch = buf[ichCurrent++]) != '\r' && ch != '\n') {
\r
38 if (ch == '\r' && ichCurrent < cchBuf && buf[ichCurrent] == '\n')
\r
40 int cchLine = ichCurrent - ichBeginningOfLine;
\r
41 if (buf[ichBeginningOfLine] == '#')
\r
42 continue; // flush comment lines;
\r
43 return new String(buf, ichBeginningOfLine, cchLine);
\r