removes org.json from srcjar
[jalview.git] / unused / javajs / util / LimitedLineReader.java
1 package javajs.util;
2
3 import java.io.BufferedReader;
4
5 /**
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.
9  *  
10  *  Generally useful for determining what sort of data a file contains.
11  *   
12  */
13 public class LimitedLineReader {
14   private char[] buf;
15   private int cchBuf;
16   private int ichCurrent;
17
18   public LimitedLineReader(BufferedReader bufferedReader, int readLimit)
19     throws Exception {  
20     bufferedReader.mark(readLimit + 1);
21     buf = new char[readLimit];
22     cchBuf = Math.max(bufferedReader.read(buf, 0, readLimit), 0);
23     ichCurrent = 0;
24     bufferedReader.reset();
25   }
26
27   public String getHeader(int n) {
28     return (n == 0 ? new String(buf) : new String(buf, 0, Math.min(cchBuf, n)));
29   }
30   
31   public String readLineWithNewline() {
32     while (ichCurrent < cchBuf) {
33       int ichBeginningOfLine = ichCurrent;
34       char ch = 0;
35       while (ichCurrent < cchBuf &&
36              (ch = buf[ichCurrent++]) != '\r' && ch != '\n') {
37       }
38       if (ch == '\r' && ichCurrent < cchBuf && buf[ichCurrent] == '\n')
39         ++ichCurrent;
40       int cchLine = ichCurrent - ichBeginningOfLine;
41       if (buf[ichBeginningOfLine] == '#')
42         continue; // flush comment lines;
43       return new String(buf, ichBeginningOfLine, cchLine);
44     }
45     return "";
46   }
47 }
48