Package org.apache.commons.csv
Class ExtendedBufferedReader
- java.lang.Object
-
- java.io.Reader
-
- java.io.BufferedReader
-
- org.apache.commons.csv.ExtendedBufferedReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Readable
final class ExtendedBufferedReader extends java.io.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 withgetPosition()
.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
closed
private long
eolCounter
The count of EOLs (CR/LF/CRLF) seen so farprivate int
lastChar
The last char returnedprivate long
position
The position, which is number of characters read so far
-
Constructor Summary
Constructors Constructor Description ExtendedBufferedReader(java.io.Reader reader)
Created extended buffered reader using default buffer-size
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the stream.(package private) long
getCurrentLineNumber()
Returns the current line number(package private) int
getLastChar()
Returns the last character that was read as an integer (0 to 65535).(package private) long
getPosition()
Gets the character position in the reader.boolean
isClosed()
(package private) int
lookAhead()
Returns the next character in the current reader without consuming it.(package private) char[]
lookAhead(char[] buf)
Populates the buffer with the nextbuf.length
characters 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.int
read()
int
read(char[] buf, int offset, int length)
java.lang.String
readLine()
Gets the next line, dropping the line terminator(s).
-
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes the stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.BufferedReader
- Throws:
java.io.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
int lookAhead() throws java.io.IOException
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:
java.io.IOException
- If an I/O error occurs
-
lookAhead
char[] lookAhead(char[] buf) throws java.io.IOException
Populates the buffer with the nextbuf.length
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:
buf
- the buffer to fill for the look ahead.- Returns:
- the buffer itself
- Throws:
java.io.IOException
- If an I/O error occurs
-
lookAhead
char[] lookAhead(int n) throws java.io.IOException
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:
java.io.IOException
- If an I/O error occurs
-
read
public int read() throws java.io.IOException
- Overrides:
read
in classjava.io.BufferedReader
- Throws:
java.io.IOException
-
read
public int read(char[] buf, int offset, int length) throws java.io.IOException
- Overrides:
read
in classjava.io.BufferedReader
- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.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 updatesposition
.Sets
lastChar
toConstants.END_OF_STREAM
at EOF, otherwise the last EOL character.- Overrides:
readLine
in classjava.io.BufferedReader
- Returns:
- the line that was read, or null if reached EOF.
- Throws:
java.io.IOException
-
-