Class LineOrientedBufferedReader

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

public final class LineOrientedBufferedReader extends BufferedReader implements LineOrientedReader
A special buffered reader which only returns data when a read-to-completion operation is guaranteed to return a complete line excluding the CR/LF characters.

This class is not thread safe.

  • Constructor Details

    • LineOrientedBufferedReader

      public LineOrientedBufferedReader(InputStream is, com.fishlib.base.string.EncodingInfo encoding)
      Create this reader with the specified InputStream and encoding. A new CharsetDecoder will be created that will throw exceptions on malformed input or unmappable characters.
      Parameters:
      is - the source input stream
      encoding - the encoding of the input stream
  • Method Details

    • read

      public int read() throws IOException
      Description copied from interface: LineOrientedReader
      Read a single character from the underlying reader. This method will only return characters if there is at least one complete line (terminated by a \r or \n character
      Specified by:
      read in interface LineOrientedReader
      Overrides:
      read in class BufferedReader
      Returns:
      The next character in the stream or -1 if there is not a complete line available.
      Throws:
      IOException - If the underlying read fails.
    • read

      public int read(@NotNull char[] buf, int offset, int length) throws IOException
      Read up to 'length' characters from the underlying buffer into 'buf' at 'offset'. This method will read no characters unless there is at least one complete line available. It will only return characters which form part of a complete available row.
      For example, if length == 10 and the current line has 4 chars remaining, and there are only 6 characters with no end line character, only 4 chars will be read. On the other hand, if there are 4 characters left, and 15 characters forming a complete line following, those will be returned as well (up to the requested length).
      Overrides:
      read in class BufferedReader
      Parameters:
      buf - The buffer to put characters in
      offset - The offset into buf to start.
      length - The maximum number of characters to read.
      Returns:
      The number of characters read, or -1 if EOS is reached, or there is not a complete line available.
      Throws:
      IOException - If any underlying read fails.
    • readLine

      public String readLine() throws IOException
      Description copied from interface: LineOrientedReader
      Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed by a line feed.
      Specified by:
      readLine in interface LineOrientedReader
      Overrides:
      readLine in class BufferedReader
      Returns:
      A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
      Throws:
      IOException - If an I/O error occurs
      See Also:
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class BufferedReader
      Throws:
      IOException
    • isClosed

      public boolean isClosed()
      Description copied from interface: LineOrientedReader
      Get if the buffer has been closed.
      Specified by:
      isClosed in interface LineOrientedReader
      Returns:
      true if the buffer has been closed
    • getLastChar

      public int getLastChar()
      Description copied from interface: LineOrientedReader
      Get the last character returned by LineOrientedReader.read()
      Specified by:
      getLastChar in interface LineOrientedReader
      Returns:
      the last character returned
    • lookAhead

      public int lookAhead() throws IOException
      Description copied from interface: LineOrientedReader
      Peek at the next character that will be returned by LineOrientedReader.read() without consuming it.
      Specified by:
      lookAhead in interface LineOrientedReader
      Returns:
      the next character that will be read
      Throws:
      IOException - if a problem occurs refilling the underlying buffer
    • getCurrentLineNumber

      public long getCurrentLineNumber()
      Description copied from interface: LineOrientedReader
      Get the total number of total lines read.
      Specified by:
      getCurrentLineNumber in interface LineOrientedReader
      Returns:
      the number of total lines read
    • getCharacterPosition

      public long getCharacterPosition()
      Description copied from interface: LineOrientedReader
      Get the current character position into the stream.
      Specified by:
      getCharacterPosition in interface LineOrientedReader
      Returns:
      the current character position
    • getBytePosition

      public int getBytePosition()
      Description copied from interface: LineOrientedReader
      Get the byte position into the underlying buffer immediately after the last character returned by any flavor of LineOrientedReader.read(), or LineOrientedReader.readLine().
      Specified by:
      getBytePosition in interface LineOrientedReader
      Returns:
      the byte position
    • resetState

      public void resetState()
      Reset this reader's last read character to allow for reading additional chars past a previous end of stream mark.