Joshua
open source statistical hierarchical phrase-based machine translation system
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
joshua.util.io.LineReader Class Reference
Inheritance diagram for joshua.util.io.LineReader:
[legend]
Collaboration diagram for joshua.util.io.LineReader:
[legend]

List of all members.

Public Member Functions

 LineReader (String filename) throws IOException
 LineReader (String filename, boolean show_progress) throws IOException
 LineReader (InputStream in)
int progress ()
void close () throws IOException
boolean ready () throws IOException
String readLine () throws IOException
Iterator< String > iterator ()
boolean hasNext ()
String next () throws NoSuchElementException
int lineno ()
void remove () throws UnsupportedOperationException
int countLines () throws IOException

Static Public Member Functions

static void main (String[] args)

Protected Member Functions

void finalize () throws Throwable

Private Attributes

BufferedReader reader
ProgressInputStream rawStream
String buffer
IOException error
int lineno = 0
boolean display_progress = false
int progress = 0

Static Private Attributes

static final Charset FILE_ENCODING = Charset.forName("UTF-8")

Detailed Description

This class provides an Iterator interface to a BufferedReader. This covers the most common use-cases for reading from files without ugly code to check whether we got a line or not.

Author:
wren ng thornton wren@.nosp@m.user.nosp@m.s.sou.nosp@m.rcef.nosp@m.orge..nosp@m.net
Matt Post post@.nosp@m.cs.j.nosp@m.hu.ed.nosp@m.u

Constructor & Destructor Documentation

joshua.util.io.LineReader.LineReader ( String  filename) throws IOException

Opens a file for iterating line by line. The special "-" filename can be used to specify STDIN. GZIP'd files are tested for automatically.

Parameters:
filenamethe file to be opened ("-" for STDIN)

Here is the call graph for this function:

Here is the caller graph for this function:

joshua.util.io.LineReader.LineReader ( String  filename,
boolean  show_progress 
) throws IOException

Wraps an InputStream for iterating line by line. Stream encoding is assumed to be UTF-8.


Member Function Documentation

void joshua.util.io.LineReader.close ( ) throws IOException

This method will close the file handle, and will raise any exceptions that occured during iteration. The method is idempotent, and all calls after the first are no-ops (unless the thread was interrupted or killed). For correctness, you must call this method before the object falls out of scope.

Here is the caller graph for this function:

int joshua.util.io.LineReader.countLines ( ) throws IOException

Iterates over all lines, ignoring their contents, and returns the count of lines. If some lines have already been read, this will return the count of remaining lines. Because no lines will remain after calling this method, we implicitly call close.

Returns:
the number of lines read

Here is the call graph for this function:

void joshua.util.io.LineReader.finalize ( ) throws Throwable [protected]

We attempt to avoid leaking file descriptors if you fail to call close before the object falls out of scope. However, the language spec makes no guarantees about timeliness of garbage collection. It is a bug to rely on this method to release the resources. Also, the garbage collector will discard any exceptions that have queued up, without notifying the application in any way.

Having a finalizer means the JVM can't do "fast allocation" of LineReader objects (or subclasses). This isn't too important due to disk latency, but may be worth noting.

See also:
Performance Tips
Techniques

Here is the call graph for this function:

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Here is the caller graph for this function:

Iterator<String> joshua.util.io.LineReader.iterator ( )

Return self as an iterator.

Here is the caller graph for this function:

static void joshua.util.io.LineReader.main ( String[]  args) [static]

Example usage code.

Here is the call graph for this function:

String joshua.util.io.LineReader.next ( ) throws NoSuchElementException

Return the next line of the file. If an error is encountered, NoSuchElementException is thrown. The actual IOException encountered will be thrown later, when the LineReader is closed. Also if there is no line to be read then NoSuchElementException is thrown.

Here is the call graph for this function:

Here is the caller graph for this function:

Chain to the underlying ProgressInputStream.

Returns:
an integer from 0..100, indicating how much of the file has been read.

Here is the call graph for this function:

Here is the caller graph for this function:

String joshua.util.io.LineReader.readLine ( ) throws IOException

This method is like next() except that it throws the IOException directly. If there are no lines to be read then null is returned.

Here is the call graph for this function:

Here is the caller graph for this function:

boolean joshua.util.io.LineReader.ready ( ) throws IOException

Determine if the reader is ready to read a line.

Here is the caller graph for this function:

void joshua.util.io.LineReader.remove ( ) throws UnsupportedOperationException

Unsupported.


Member Data Documentation

boolean joshua.util.io.LineReader.display_progress = false [private]
IOException joshua.util.io.LineReader.error [private]
final Charset joshua.util.io.LineReader.FILE_ENCODING = Charset.forName("UTF-8") [static, private]
BufferedReader joshua.util.io.LineReader.reader [private]