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