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
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
ConstructorsConstructorDescriptionLineOrientedBufferedReader(InputStream is, com.fishlib.base.string.EncodingInfo encoding) Create this reader with the specifiedInputStreamandencoding. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()intGet the byte position into the underlying buffer immediately after the last character returned by any flavor ofLineOrientedReader.read(), orLineOrientedReader.readLine().longGet the current character position into the stream.longGet the total number of total lines read.intGet the last character returned byLineOrientedReader.read()booleanisClosed()Get if the buffer has been closed.intPeek at the next character that will be returned byLineOrientedReader.read()without consuming it.intread()Read a single character from the underlying reader.intread(char[] buf, int offset, int length) Read up to 'length' characters from the underlying buffer into 'buf' at 'offset'.readLine()Reads a line of text.voidReset this reader's last read character to allow for reading additional chars past a previous end of stream mark.Methods inherited from class java.io.BufferedReader
lines, mark, markSupported, ready, reset, skipMethods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
Constructor Details
-
LineOrientedBufferedReader
Create this reader with the specifiedInputStreamandencoding. A newCharsetDecoderwill 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:LineOrientedReaderRead 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:
readin interfaceLineOrientedReader- Overrides:
readin 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:
readin 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:LineOrientedReaderReads 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:
readLinein interfaceLineOrientedReader- Overrides:
readLinein 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:
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classBufferedReader- Throws:
IOException
-
isClosed
public boolean isClosed()Description copied from interface:LineOrientedReaderGet if the buffer has been closed.- Specified by:
isClosedin interfaceLineOrientedReader- Returns:
- true if the buffer has been closed
-
getLastChar
public int getLastChar()Description copied from interface:LineOrientedReaderGet the last character returned byLineOrientedReader.read()- Specified by:
getLastCharin interfaceLineOrientedReader- Returns:
- the last character returned
-
lookAhead
Description copied from interface:LineOrientedReaderPeek at the next character that will be returned byLineOrientedReader.read()without consuming it.- Specified by:
lookAheadin 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:LineOrientedReaderGet the total number of total lines read.- Specified by:
getCurrentLineNumberin interfaceLineOrientedReader- Returns:
- the number of total lines read
-
getCharacterPosition
public long getCharacterPosition()Description copied from interface:LineOrientedReaderGet the current character position into the stream.- Specified by:
getCharacterPositionin interfaceLineOrientedReader- Returns:
- the current character position
-
getBytePosition
public int getBytePosition()Description copied from interface:LineOrientedReaderGet the byte position into the underlying buffer immediately after the last character returned by any flavor ofLineOrientedReader.read(), orLineOrientedReader.readLine().- Specified by:
getBytePositionin 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.
-