2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import java.io.BufferedReader;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.StringReader;
27 * A file reader that concatenates lines
32 public class BufferedLineReader
36 String cleanLine(String l);
40 * a reader for the file being read
42 private BufferedReader br;
45 * optional handler to post-process each line as it is read
47 private LineCleaner cleaner;
50 * current buffer of <bufferSize> post-processed input lines
52 private String[] buffer;
54 private boolean atEof;
61 * the number of lines to concatenate at a time while reading
63 * an optional callback handler to post-process each line after
65 * @throws FileNotFoundException
67 public BufferedLineReader(BufferedReader reader, int bufferSize,
72 buffer = new String[bufferSize];
76 * load up the buffer with N-1 lines, ready for the first read
78 for (int i = 1; i < bufferSize; i++)
86 * Reads the next line from file, invokes the post-processor if one was
87 * provided, and returns the 'cleaned' line, or null at end of file.
91 private String readLine() // throws IOException
101 line = br.readLine();
102 } catch (IOException e)
113 line = cleaner.cleanLine(line);
117 * shuffle down the lines buffer and add the new line
118 * in the last position
120 for (int i = 1; i < buffer.length; i++)
122 buffer[i - 1] = buffer[i];
124 buffer[buffer.length - 1] = line;
129 * Returns a number of concatenated lines from the file, or null at end of
136 if (readLine() == null)
140 StringBuilder result = new StringBuilder(100 * buffer.length);
141 for (String line : buffer)
148 return result.toString();
152 * A main 'test' method!
154 * @throws IOException
156 public static void main(String[] args) throws IOException
158 String data = "Now is the winter\n" + "Of our discontent\n"
159 + "Made glorious summer\n" + "By this sun of York\n";
160 BufferedReader br = new BufferedReader(new StringReader(data));
161 BufferedLineReader reader = new BufferedLineReader(br, 3,
165 public String cleanLine(String l)
167 return l.toUpperCase();
170 String line = reader.read();
171 String expect = "NOW IS THE WINTEROF OUR DISCONTENTMADE GLORIOUS SUMMER";
172 if (!line.equals(expect))
174 System.err.println("Fail: expected '" + expect + "', found '" + line
179 System.out.println("Line one ok!");
181 line = reader.read();
182 expect = "OF OUR DISCONTENTMADE GLORIOUS SUMMERBY THIS SUN OF YORK";
183 if (!line.equals(expect))
185 System.err.println("Fail: expected '" + expect + "', found '" + line
190 System.out.println("Line two ok!!");
192 line = reader.read();
195 System.err.println("Fail: expected null at eof, got '" + line + "'");
199 System.out.println("EOF ok!!!");