Package org.apache.commons.csv
Class ExtendedBufferedReader
- java.lang.Object
-
- java.io.Reader
-
- org.apache.commons.io.input.UnsynchronizedReader
-
- org.apache.commons.io.input.UnsynchronizedBufferedReader
-
- org.apache.commons.csv.ExtendedBufferedReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
final class ExtendedBufferedReader extends org.apache.commons.io.input.UnsynchronizedBufferedReaderA 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 longbytesReadThe number of bytes read so far.private longbytesReadMarkprivate java.nio.charset.CharsetEncoderencoderEncoder for calculating the number of bytes for each character read.private intlastCharThe last char returnedprivate intlastCharMarkprivate longlineNumberThe count of EOLs (CR/LF/CRLF) seen so farprivate longlineNumberMarkprivate longpositionThe position, which is the number of characters read so farprivate longpositionMark
-
Constructor Summary
Constructors Constructor Description ExtendedBufferedReader(java.io.Reader reader)Constructs a new instance using the default buffer size.ExtendedBufferedReader(java.io.Reader reader, java.nio.charset.Charset charset, boolean trackBytes)Constructs a new instance with the specified reader, character set, and byte tracking option.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the stream.(package private) longgetBytesRead()Gets the number of bytes read by the reader.private intgetEncodedCharLength(int current)Gets the byte length of the given character based on the the original Unicode specification, which defined characters as fixed-width 16-bit entities.(package private) intgetLastChar()Returns the last character that was read as an integer (0 to 65535).(package private) longgetLineNumber()Returns the current line number(package private) longgetPosition()Gets the character position in the reader.voidmark(int readAheadLimit)intread()intread(char[] buf, int offset, int length)java.lang.StringreadLine()Gets the next line, dropping the line terminator(s).voidreset()
-
-
-
Field Detail
-
lastChar
private int lastChar
The last char returned
-
lastCharMark
private int lastCharMark
-
lineNumber
private long lineNumber
The count of EOLs (CR/LF/CRLF) seen so far
-
lineNumberMark
private long lineNumberMark
-
position
private long position
The position, which is the number of characters read so far
-
positionMark
private long positionMark
-
bytesRead
private long bytesRead
The number of bytes read so far.
-
bytesReadMark
private long bytesReadMark
-
encoder
private final java.nio.charset.CharsetEncoder encoder
Encoder for calculating the number of bytes for each character read.
-
-
Constructor Detail
-
ExtendedBufferedReader
ExtendedBufferedReader(java.io.Reader reader)
Constructs a new instance using the default buffer size.
-
ExtendedBufferedReader
ExtendedBufferedReader(java.io.Reader reader, java.nio.charset.Charset charset, boolean trackBytes)Constructs a new instance with the specified reader, character set, and byte tracking option. Initializes an encoder if byte tracking is enabled and a character set is provided.- Parameters:
reader- the reader supports a look-ahead option.charset- the character set for encoding, ornullif not applicable.trackBytes-trueto enable byte tracking;falseto disable it.
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionCloses the stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classorg.apache.commons.io.input.UnsynchronizedBufferedReader- Throws:
java.io.IOException- If an I/O error occurs
-
getBytesRead
long getBytesRead()
Gets the number of bytes read by the reader.- Returns:
- the number of bytes read by the read
-
getEncodedCharLength
private int getEncodedCharLength(int current) throws java.nio.charset.CharacterCodingExceptionGets the byte length of the given character based on the the original Unicode specification, which defined characters as fixed-width 16-bit entities.The Unicode characters are divided into two main ranges:
- U+0000 to U+FFFF (Basic Multilingual Plane, BMP):
- Represented using a single 16-bit
char. - Includes UTF-8 encodings of 1-byte, 2-byte, and some 3-byte characters.
- Represented using a single 16-bit
- U+10000 to U+10FFFF (Supplementary Characters):
- Represented as a pair of
chars: - The first
charis from the high-surrogates range (?-?). - The second
charis from the low-surrogates range (?-?). - Includes UTF-8 encodings of some 3-byte characters and all 4-byte characters.
- Represented as a pair of
- Parameters:
current- the current character to process.- Returns:
- the byte length of the character.
- Throws:
java.nio.charset.CharacterCodingException- if the character cannot be encoded.
- U+0000 to U+FFFF (Basic Multilingual Plane, BMP):
-
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 theUnsynchronizedBufferedReader.peek()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 returnIOUtils.EOF.- Returns:
- the last character that was read
-
getLineNumber
long getLineNumber()
Returns the current line number- Returns:
- the current line number
-
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)
-
mark
public void mark(int readAheadLimit) throws java.io.IOException- Overrides:
markin classorg.apache.commons.io.input.UnsynchronizedBufferedReader- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Overrides:
readin classorg.apache.commons.io.input.UnsynchronizedBufferedReader- Throws:
java.io.IOException
-
read
public int read(char[] buf, int offset, int length) throws java.io.IOException- Overrides:
readin classorg.apache.commons.io.input.UnsynchronizedBufferedReader- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOExceptionGets the next line, dropping the line terminator(s). This method should only be called when processing a comment, otherwise, information can be lost.Increments
lineNumberand updatesposition.Sets
lastChartoConstants.EOFat EOF, otherwise the last EOL character.- Overrides:
readLinein classorg.apache.commons.io.input.UnsynchronizedBufferedReader- Returns:
- the line that was read, or null if reached EOF.
- Throws:
java.io.IOException
-
reset
public void reset() throws java.io.IOException- Overrides:
resetin classorg.apache.commons.io.input.UnsynchronizedBufferedReader- Throws:
java.io.IOException
-
-