View Javadoc

1   /* LineReadingIterator
2   *
3   * $Id: LineReadingIterator.java 4650 2006-09-25 18:09:42Z paul_jack $
4   *
5   * Created on Jul 27, 2004
6   *
7   * Copyright (C) 2004 Internet Archive.
8   *
9   * This file is part of the Heritrix web crawler (crawler.archive.org).
10  *
11  * Heritrix is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * any later version.
15  *
16  * Heritrix is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser Public License
22  * along with Heritrix; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */ 
25  package org.archive.util.iterator;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.util.logging.Logger;
30  
31  /***
32   * Utility class providing an Iterator interface over line-oriented
33   * text input, as a thin wrapper over a BufferedReader.
34   * 
35   * @author gojomo
36   */
37  public class LineReadingIterator extends LookaheadIterator<String> {
38      private static final Logger logger =
39          Logger.getLogger(LineReadingIterator.class.getName());
40  
41      protected BufferedReader reader = null;
42  
43      public LineReadingIterator(BufferedReader r) {
44          reader = r;
45      }
46  
47      /***
48       * Loads next line into lookahead spot
49       * 
50       * @return whether any item was loaded into next field
51       */
52      protected boolean lookahead() {
53          try {
54              next = this.reader.readLine();
55              if(next == null) {
56                  // TODO: make this close-on-exhaust optional?
57                  reader.close();
58              }
59              return (next!=null);
60          } catch (IOException e) {
61              logger.warning(e.toString());
62              return false;
63          }
64      }
65  }