Package org.apache.commons.csv
Class ExtendedBufferedReader
java.lang.Object
java.io.Reader
java.io.BufferedReader
org.apache.commons.csv.ExtendedBufferedReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
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
FieldsModifier and TypeFieldDescriptionprivate booleanprivate longThe count of EOLs (CR/LF/CRLF) seen so farprivate intThe last char returnedprivate longThe position, which is number of characters read so far -
Constructor Summary
ConstructorsConstructorDescriptionExtendedBufferedReader(Reader reader) Created extended buffered reader using default buffer-size -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the stream.(package private) longReturns the current line number(package private) intReturns the last character that was read as an integer (0 to 65535).(package private) longGets the character position in the reader.booleanisClosed()(package private) intReturns the next character in the current reader without consuming it.(package private) char[]lookAhead(char[] buf) Populates the buffer with the nextbuf.lengthcharacters in the current reader without consuming them.(package private) char[]lookAhead(int n) Returns the next n characters in the current reader without consuming them.intread()intread(char[] buf, int offset, int length) readLine()Gets the next line, dropping the line terminator(s).Methods inherited from class java.io.BufferedReader
lines, mark, markSupported, ready, reset, skipMethods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
Field Details
-
lastChar
private int lastCharThe last char returned -
eolCounter
private long eolCounterThe count of EOLs (CR/LF/CRLF) seen so far -
position
private long positionThe position, which is number of characters read so far -
closed
private boolean closed
-
-
Constructor Details
-
ExtendedBufferedReader
ExtendedBufferedReader(Reader reader) Created extended buffered reader using default buffer-size
-
-
Method Details
-
close
Closes the stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classBufferedReader- 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 thelookAhead()method. If no character has been read then this will returnConstants.UNDEFINED. If the end of the stream was reached on the last read then this will returnConstants.END_OF_STREAM.- 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
Returns the next character in the current reader without consuming it. So the next call toread()will still return this value. Does not affect line number or last character.- Returns:
- the next character
- Throws:
IOException- If an I/O error occurs
-
lookAhead
Populates the buffer with the nextbuf.lengthcharacters in the current reader without consuming them. The next call toread()will still return the next value. This doesn't affect line number or last character.- Parameters:
buf- the buffer to fill for the look ahead.- Returns:
- the buffer itself
- Throws:
IOException- If an I/O error occurs
-
lookAhead
Returns the next n characters in the current reader without consuming them. The next call toread()will still return the next value. This doesn't affect line number or last character.- Parameters:
n- the number characters look ahead.- Returns:
- the next n characters.
- Throws:
IOException- If an I/O error occurs
-
read
- Overrides:
readin classBufferedReader- Throws:
IOException
-
read
- Overrides:
readin classBufferedReader- Throws:
IOException
-
readLine
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
eolCounterand updatesposition.Sets
lastChartoConstants.END_OF_STREAMat EOF, otherwise the last EOL character.- Overrides:
readLinein classBufferedReader- Returns:
- the line that was read, or null if reached EOF.
- Throws:
IOException
-