Merge branch 'patch/JAL-4036_uniprot_fts_legacy_endpoint' into develop
[jalview.git] / utils / BufferedLineReader.java
1 import java.io.BufferedReader;
2 import java.io.FileNotFoundException;
3 import java.io.IOException;
4 import java.io.StringReader;
5
6 /**
7  * A file reader that concatenates lines
8  * 
9  * @author gmcarstairs
10  *
11  */
12 public class BufferedLineReader
13 {
14   interface LineCleaner
15   {
16     String cleanLine(String l);
17   }
18
19   /*
20    * a reader for the file being read
21    */
22   private BufferedReader br;
23   
24   /*
25    * optional handler to post-process each line as it is read
26    */
27   private LineCleaner cleaner;
28
29   /*
30    * current buffer of <bufferSize> post-processed input lines
31    */
32   private String[] buffer;
33
34   private boolean atEof;
35
36   /**
37    * Constructor
38    * 
39    * @param reader
40    * @param bufferSize
41    *          the number of lines to concatenate at a time while reading
42    * @param tidier
43    *          an optional callback handler to post-process each line after
44    *          reading
45    * @throws FileNotFoundException
46    */
47   public BufferedLineReader(BufferedReader reader, int bufferSize,
48           LineCleaner tidier)
49           throws IOException
50   {
51     br = reader;
52     buffer = new String[bufferSize];
53     cleaner = tidier;
54
55     /*
56      * load up the buffer with N-1 lines, ready for the first read
57      */
58     for (int i = 1; i < bufferSize; i++)
59     {
60       readLine();
61     }
62
63   }
64
65   /**
66    * Reads the next line from file, invokes the post-processor if one was
67    * provided, and returns the 'cleaned' line, or null at end of file.
68    * 
69    * @return
70    */
71   private String readLine() // throws IOException
72   {
73     if (atEof)
74     {
75       return null;
76     }
77
78     String line = null;
79     try
80     {
81       line = br.readLine();
82     } catch (IOException e)
83     {
84       e.printStackTrace();
85     }
86     if (line == null)
87     {
88       atEof = true;
89       return null;
90     }
91     if (cleaner != null)
92     {
93       line = cleaner.cleanLine(line);
94     }
95
96     /*
97      * shuffle down the lines buffer and add the new line
98      * in the last position
99      */
100     for (int i = 1; i < buffer.length; i++)
101     {
102       buffer[i - 1] = buffer[i];
103     }
104     buffer[buffer.length - 1] = line;
105     return line;
106   }
107
108   /**
109    * Returns a number of concatenated lines from the file, or null at end of
110    * file.
111    * 
112    * @return
113    */
114   public String read()
115   {
116     if (readLine() == null)
117     {
118       return null;
119     }
120     StringBuilder result = new StringBuilder(100 * buffer.length);
121     for (String line : buffer)
122     {
123       if (line != null)
124       {
125         result.append(line);
126       }
127     }
128     return result.toString();
129   }
130
131   /**
132    * A main 'test' method!
133    * 
134    * @throws IOException
135    */
136   public static void main(String[] args) throws IOException
137   {
138     String data = "Now is the winter\n" + "Of our discontent\n"
139             + "Made glorious summer\n" + "By this sun of York\n";
140     BufferedReader br = new BufferedReader(new StringReader(data));
141     BufferedLineReader reader = new BufferedLineReader(br, 3,
142             new LineCleaner()
143             {
144               @Override
145               public String cleanLine(String l)
146               {
147                 return l.toUpperCase();
148               }
149             });
150     String line = reader.read();
151     String expect = "NOW IS THE WINTEROF OUR DISCONTENTMADE GLORIOUS SUMMER";
152     if (!line.equals(expect))
153     {
154       System.err.println("Fail: expected '" + expect + "', found '" + line
155               + ";");
156     }
157     else
158     {
159       System.out.println("Line one ok!");
160     }
161     line = reader.read();
162     expect = "OF OUR DISCONTENTMADE GLORIOUS SUMMERBY THIS SUN OF YORK";
163     if (!line.equals(expect))
164     {
165       System.err.println("Fail: expected '" + expect + "', found '" + line
166               + "'");
167     }
168     else
169     {
170       System.out.println("Line two ok!!");
171     }
172     line = reader.read();
173     if (line != null)
174     {
175       System.err.println("Fail: expected null at eof, got '" + line + "'");
176     }
177     else
178     {
179       System.out.println("EOF ok!!!");
180     }
181   }
182 }