Source for a reader. @param r reader to draw from @return LineSource @throws IOException on underlying exception
(Reader r)
| 251 | * @throws IOException on underlying exception |
| 252 | */ |
| 253 | public static LineSource source(Reader r) throws IOException { |
| 254 | BufferedReader br = new BufferedReader(r); |
| 255 | String first = br.readLine(); |
| 256 | return new LineSource() { |
| 257 | String next = first; |
| 258 | |
| 259 | @Override |
| 260 | public boolean hasLine() { |
| 261 | return next != null; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public String nextLine() throws IOException { |
| 266 | if (hasLine()) { |
| 267 | String p = next; |
| 268 | next = br.readLine(); |
| 269 | return p; |
| 270 | } |
| 271 | throw new NoSuchElementException(); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void close() throws IOException { |
| 276 | r.close(); |
| 277 | } |
| 278 | }; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Source from a string. |