Class ExtendedBufferedReader

java.lang.Object
java.io.Reader
java.io.BufferedReader
org.apache.commons.csv.ExtendedBufferedReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

final class ExtendedBufferedReader extends BufferedReader
A special buffered reader which supports sophisticated read access.

In particular the reader supports a look-ahead option, which allows you to see the next char returned by read(). This reader also tracks how many characters have been read with getPosition().

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
     
    private long
    The count of EOLs (CR/LF/CRLF) seen so far
    private int
    The last char returned
    private long
    The position, which is the number of characters read so far

    Fields inherited from class java.io.Reader

    lock
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new instance using the default buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the stream.
    (package private) long
    Returns the current line number
    (package private) int
    Returns the last character that was read as an integer (0 to 65535).
    (package private) long
    Gets the character position in the reader.
    boolean
     
    (package private) int
    Returns the next character in the current reader without consuming it.
    (package private) char[]
    lookAhead(char[] buf)
    Populates the buffer with the next buf.length characters in the current reader without consuming them.
    int
     
    int
    read(char[] buf, int offset, int length)
     
    Gets the next line, dropping the line terminator(s).

    Methods inherited from class java.io.BufferedReader

    lines, mark, markSupported, ready, reset, skip

    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
  • Field Details

    • lastChar

      private int lastChar
      The last char returned
    • eolCounter

      private long eolCounter
      The count of EOLs (CR/LF/CRLF) seen so far
    • position

      private long position
      The position, which is the number of characters read so far
    • closed

      private boolean closed
  • Constructor Details

    • ExtendedBufferedReader

      ExtendedBufferedReader(Reader reader)
      Constructs a new instance using the default buffer size.
  • Method Details

    • close

      public void close() throws IOException
      Closes the stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class BufferedReader
      Throws:
      IOException - If an I/O error occurs
    • getCurrentLineNumber

      long getCurrentLineNumber()
      Returns the current line number
      Returns:
      the current line number
    • getLastChar

      int getLastChar()
      Returns the last character that was read as an integer (0 to 65535). This will be the last character returned by any of the read methods. This will not include a character read using the lookAhead() method. If no character has been read then this will return Constants.UNDEFINED. If the end of the stream was reached on the last read then this will return
      invalid reference
      Constants#EOF
      .
      Returns:
      the last character that was read
    • getPosition

      long getPosition()
      Gets the character position in the reader.
      Returns:
      the current position in the reader (counting characters, not bytes since this is a Reader)
    • isClosed

      public boolean isClosed()
    • lookAhead

      int lookAhead() throws IOException
      Returns the next character in the current reader without consuming it. So the next call to read() will still return this value. Does not affect the line number or the last character.
      Returns:
      the next character
      Throws:
      IOException - If an I/O error occurs
    • lookAhead

      char[] lookAhead(char[] buf) throws IOException
      Populates the buffer with the next buf.length characters in the current reader without consuming them. The next call to read() will still return the next value. This doesn't affect the line number or the last character.
      Parameters:
      buf - the buffer to fill for the look ahead.
      Returns:
      the buffer itself
      Throws:
      IOException - If an I/O error occurs
    • read

      public int read() throws IOException
      Overrides:
      read in class BufferedReader
      Throws:
      IOException
    • read

      public int read(char[] buf, int offset, int length) throws IOException
      Overrides:
      read in class BufferedReader
      Throws:
      IOException
    • readLine

      public String readLine() throws IOException
      Gets the next line, dropping the line terminator(s). This method should only be called when processing a comment, otherwise, information can be lost.

      Increments eolCounter and updates position.

      Sets lastChar to Constants.EOF at EOF, otherwise the last EOL character.

      Overrides:
      readLine in class BufferedReader
      Returns:
      the line that was read, or null if reached EOF.
      Throws:
      IOException