net.sf.beanlib.io
Class FastLineNumberReader

java.lang.Object
  extended by java.io.Reader
      extended by net.sf.beanlib.io.FastLineNumberReader
All Implemented Interfaces:
Closeable, Readable

public class FastLineNumberReader
extends Reader

A non-thread-safe fast line number reader that preserves the end-of-line character(s). Code is originally based on BufferedReader.

Author:
Joe D. Velopar

Nested Class Summary
private static class FastLineNumberReader.EolEnum
          End-of-line enums.
 
Field Summary
private  char[] charbuf
          Character buffer.
private  boolean checkNextLF
          True iff we need to read in more data into the character buffer to check if the next char is a LF; False otherwise.
private static int DEFAULT_CHAR_BUFFER_SIZE
           
private static int DEFAULT_LINE_LENGTH
           
private  FastLineNumberReader.EolEnum eolEnum
          Last End of line.
private  int lineNumber
          Current line number.
private  int nextCharIdx
          Index to the next character.
private  int numCharInBuf
          Number of characters in the character buffer.
private  Reader reader
          The underlying reader.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
FastLineNumberReader(Reader in)
          Create a buffering character-input stream that uses a default-sized input buffer.
FastLineNumberReader(Reader in, int sz)
          Create a buffering character-input stream that uses an input buffer of the specified size.
 
Method Summary
 void close()
           
private  void fill()
          Reads and fills the internal character buffer.
 int getLineNumber()
          Returns the current line number.
 int read(char[] cbuf, int off, int len)
           
private  int read1(char[] cbuf, int off, int len)
           
 String readEndOfLine()
          Returns the end of line character(s) as string.
 String readLine()
          Returns the next line read.
 
Methods inherited from class java.io.Reader
mark, markSupported, read, read, read, ready, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHAR_BUFFER_SIZE

private static int DEFAULT_CHAR_BUFFER_SIZE

DEFAULT_LINE_LENGTH

private static int DEFAULT_LINE_LENGTH

reader

private Reader reader
The underlying reader.


charbuf

private char[] charbuf
Character buffer.


numCharInBuf

private int numCharInBuf
Number of characters in the character buffer.


nextCharIdx

private int nextCharIdx
Index to the next character.


checkNextLF

private boolean checkNextLF
True iff we need to read in more data into the character buffer to check if the next char is a LF; False otherwise.


lineNumber

private int lineNumber
Current line number.


eolEnum

private FastLineNumberReader.EolEnum eolEnum
Last End of line.

Constructor Detail

FastLineNumberReader

public FastLineNumberReader(Reader in,
                            int sz)
Create a buffering character-input stream that uses an input buffer of the specified size.

Parameters:
in - A Reader
sz - Input-buffer size
Throws:
IllegalArgumentException - If sz is <= 0

FastLineNumberReader

public FastLineNumberReader(Reader in)
Create a buffering character-input stream that uses a default-sized input buffer.

Parameters:
in - A Reader
Method Detail

fill

private void fill()
           throws IOException
Reads and fills the internal character buffer.

Throws:
IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException
Specified by:
read in class Reader
Throws:
IOException

read1

private int read1(char[] cbuf,
                  int off,
                  int len)
           throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException

readEndOfLine

public String readEndOfLine()
                     throws IOException
Returns the end of line character(s) as string.

Throws:
IOException

getLineNumber

public int getLineNumber()
Returns the current line number.


readLine

public String readLine()
                throws IOException
Returns the next line read.

Throws:
IOException