Package org.apache.commons.csv
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.
-
Field Summary
-
Constructor Summary
Constructors Constructor Description LineOrientedBufferedReader(InputStream is, com.fishlib.base.string.EncodingInfo encoding)
Create this reader with the specifiedInputStream
andencoding
. -
Method Summary
Modifier and Type Method Description void
close()
int
getBytePosition()
Get the byte position into the underlying buffer immediately after the last character returned by any flavor ofLineOrientedReader.read()
, orLineOrientedReader.readLine()
.long
getCharacterPosition()
Get the current character position into the stream.long
getCurrentLineNumber()
Get the total number of total lines read.int
getLastChar()
Get the last character returned byLineOrientedReader.read()
boolean
isClosed()
Get if the buffer has been closed.int
lookAhead()
Peek at the next character that will be returned byLineOrientedReader.read()
without consuming it.int
read()
Read a single character from the underlying reader.int
read(char[] buf, int offset, int length)
Read up to 'length' characters from the underlying buffer into 'buf' at 'offset'.String
readLine()
Reads a line of text.void
resetState()
Reset this reader's last read character to allow for reading additional chars past a previous end of stream mark.
-
Constructor Details
-
LineOrientedBufferedReader
Create this reader with the specifiedInputStream
andencoding
. A newCharsetDecoder
will be created that will throw exceptions on malformed input or unmappable characters.- Parameters:
is
- the source input streamencoding
- the encoding of the input stream
-
-
Method Details
-
read
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 interfaceLineOrientedReader
- Overrides:
read
in classBufferedReader
- 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
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 classBufferedReader
- Parameters:
buf
- The buffer to put characters inoffset
- 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
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 interfaceLineOrientedReader
- Overrides:
readLine
in classBufferedReader
- 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:
Files.readAllLines(java.nio.file.Path, java.nio.charset.Charset)
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classBufferedReader
- Throws:
IOException
-
isClosed
public boolean isClosed()Description copied from interface:LineOrientedReader
Get if the buffer has been closed.- Specified by:
isClosed
in interfaceLineOrientedReader
- Returns:
- true if the buffer has been closed
-
getLastChar
public int getLastChar()Description copied from interface:LineOrientedReader
Get the last character returned byLineOrientedReader.read()
- Specified by:
getLastChar
in interfaceLineOrientedReader
- Returns:
- the last character returned
-
lookAhead
Description copied from interface:LineOrientedReader
Peek at the next character that will be returned byLineOrientedReader.read()
without consuming it.- Specified by:
lookAhead
in interfaceLineOrientedReader
- 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 interfaceLineOrientedReader
- 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 interfaceLineOrientedReader
- 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 ofLineOrientedReader.read()
, orLineOrientedReader.readLine()
.- Specified by:
getBytePosition
in interfaceLineOrientedReader
- 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.
-