Build 1.0_r1(from source)

java.io
Class LineNumberReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.BufferedReader
          extended by java.io.LineNumberReader
All Implemented Interfaces:
Closeable, Readable

public class LineNumberReader
extends BufferedReader

LineNumberReader is a buffered character input reader which counts line numbers as data is being read. The line number starts at 0 and is incremented any time '\r', '\n', or '\r\n' is read.

See Also:
BufferedWriter

Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
LineNumberReader(Reader in)
          Constructs a new buffered LineNumberReader on the Reader in.
LineNumberReader(Reader in, int size)
          Constructs a new buffered LineNumberReader on the Reader in.
 
Method Summary
 int getLineNumber()
          Returns a int representing the current line number for this LineNumberReader.
 void mark(int readlimit)
          Set a Mark position in this LineNumberReader.
 int read()
          Reads a single char from this LineNumberReader and returns the result as an int.
 int read(char[] buffer, int offset, int count)
          Reads at most count chars from this LineNumberReader and stores them in char array buffer starting at offset offset.
 String readLine()
          Returns a String representing the next line of text available in this LineNumberReader.
 void reset()
          Reset this LineNumberReader to the last marked location.
 void setLineNumber(int lineNumber)
          Sets the lineNumber of this LineNumberReader to the specified lineNumber.
 long skip(long count)
          Skips count number of chars in this LineNumberReader.
 
Methods inherited from class java.io.BufferedReader
close, markSupported, ready
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LineNumberReader

public LineNumberReader(Reader in)
Constructs a new buffered LineNumberReader on the Reader in. The default buffer size (8K) is allocated and all reads can now be filtered through this LineNumberReader.

Parameters:
in - the Reader to buffer reads on.

LineNumberReader

public LineNumberReader(Reader in,
                        int size)
Constructs a new buffered LineNumberReader on the Reader in. The buffer size is specified by the parameter size and all reads can now be filtered through this LineNumberReader.

Parameters:
in - the Reader to buffer reads on.
size - the size of buffer to allocate.
Method Detail

getLineNumber

public int getLineNumber()
Returns a int representing the current line number for this LineNumberReader.

Returns:
int the current line number.

mark

public void mark(int readlimit)
          throws IOException
Set a Mark position in this LineNumberReader. The parameter readLimit indicates how many characters can be read before a mark is invalidated. Sending reset() will reposition the reader back to the marked position provided readLimit has not been surpassed. The lineNumber associated with this marked position will also be saved and restored when reset() is sent provided readLimit has not been surpassed.

Overrides:
mark in class BufferedReader
Parameters:
readlimit - an int representing how many characters must be read before invalidating the mark.
Throws:
IOException - If an error occurs attempting mark this LineNumberReader.

read

public int read()
         throws IOException
Reads a single char from this LineNumberReader and returns the result as an int. The low-order 2 bytes are returned or -1 of the end of reader was encountered. This implementation returns a char from the target reader. The line number count is incremented if a line terminator is encountered. A line delimiter sequence is determined by '\r', '\n', or '\r\n'. In this method, the sequence is always translated into '\n'.

Overrides:
read in class BufferedReader
Returns:
int The char read or -1 if end of reader.
Throws:
IOException - If the reader is already closed or another IOException occurs.

read

public int read(char[] buffer,
                int offset,
                int count)
         throws IOException
Reads at most count chars from this LineNumberReader and stores them in char array buffer starting at offset offset. Answer the number of chars actually read or -1 if no chars were read and end of reader was encountered. This implementation reads chars from the target stream. The line number count is incremented if a line terminator is encountered. A line delimiter sequence is determined by '\r', '\n', or '\r\n'. In this method, the sequence is always translated into '\n'.

Overrides:
read in class BufferedReader
Parameters:
buffer - the char array in which to store the read chars.
offset - the offset in buffer to store the read chars.
count - the maximum number of chars to store in buffer.
Returns:
the number of chars actually read or -1 if end of reader.
Throws:
IOException - If the reader is already closed or another IOException occurs.

readLine

public String readLine()
                throws IOException
Returns a String representing the next line of text available in this LineNumberReader. A line is represented by 0 or more characters followed by '\n', '\r', "\n\r" or end of stream. The String does not include the newline sequence.

Overrides:
readLine in class BufferedReader
Returns:
String the contents of the line or null if no characters were read before end of stream.
Throws:
IOException - If the LineNumberReader is already closed or some other IO error occurs.

reset

public void reset()
           throws IOException
Reset this LineNumberReader to the last marked location. If the readlimit has been passed or no mark has been set, throw IOException. This implementation resets the target reader. It also resets the line count to what is was when this reader was marked.

Overrides:
reset in class BufferedReader
Throws:
IOException - If the reader is already closed or another IOException occurs.

setLineNumber

public void setLineNumber(int lineNumber)
Sets the lineNumber of this LineNumberReader to the specified lineNumber. Note that this may have side effects on the line number associated with the last marked position.

Parameters:
lineNumber - the new lineNumber value.

skip

public long skip(long count)
          throws IOException
Skips count number of chars in this LineNumberReader. Subsequent read()'s will not return these chars unless reset() is used. This implementation skips count number of chars in the target stream and increments the lineNumber count as chars are skipped.

Overrides:
skip in class BufferedReader
Parameters:
count - the number of chars to skip.
Returns:
the number of chars actually skipped.
Throws:
IOException - If the reader is already closed or another IOException occurs.

Build 1.0_r1(from source)

Please submit a feedback, bug or feature