Top Description Fields Constructors Methods
org.apache.commons.csv

pack-priv final Class ExtendedBufferedReader

extends UnsynchronizedBufferedReader
Class Inheritance
Static Imports
org.apache.commons.csv.Constants.CR, .Constants.LF, .Constants.UNDEFINED, org.apache.commons.io.IOUtils.EOF

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

Modifier and TypeField and Description
private long
bytesRead

The number of bytes read so far.

private long
private final CharsetEncoder
encoder

Encoder for calculating the number of bytes for each character read.

private int
lastChar

The last char returned

private int
private long
lineNumber

The count of EOLs (CR/LF/CRLF) seen so far

private long
private long
position

The position, which is the number of characters read so far

private long

Constructor Summary

AccessConstructor and Description
pack-priv
ExtendedBufferedReader(final Reader reader)

Constructs a new instance using the default buffer size.

pack-priv
ExtendedBufferedReader(final Reader
the reader supports a look-ahead option.
reader
,
final Charset
the character set for encoding, or null if not applicable.
charset
,
final boolean
true to enable byte tracking; false to disable it.
trackBytes
)

Constructs a new instance with the specified reader, character set, and byte tracking option.

Method Summary

Modifier and TypeMethod and Description
public void
pack-priv long

Returns:

the number of bytes read by the read
getBytesRead
()

Gets the number of bytes read by the reader.

private int

Returns:

the byte length of the character.
getEncodedCharLength
(final int
the current character to process.
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.

pack-priv int

Returns:

the last character that was read
getLastChar
()

Returns the last character that was read as an integer (0 to 65535).

pack-priv long

Returns:

the current line number
getLineNumber
()

Returns the current line number

pack-priv long

Returns:

the current position in the reader (counting characters, not bytes since this is a Reader)
getPosition
()

Gets the character position in the reader.

public void
mark(final int
the number of characters that can be read before the mark is invalidated.
readAheadLimit
)

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.mark.

Sets a mark position in this reader.

public int
read()

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.read.

Reads a single character from this reader and returns it with the two higher-order bytes set to 0.

public int
read(final char[]
the character array to store the characters read.
buf
,
final int
the initial position in buffer to store the bytes read from this reader.
offset
,
final int
the maximum number of characters to read, must be non-negative.
length
)

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.read.

Reads at most length characters from this reader and stores them at offset in the character array buffer.

public String

Returns:

the line that was read, or null if reached EOF.
readLine
()

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.readLine.

Gets the next line, dropping the line terminator(s).

public void
reset()

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.reset.

Resets this reader's position to the last mark() location.

Inherited from org.apache.commons.io.input.UnsynchronizedBufferedReader:
markSupportedpeekpeekreadyskip

Field Detail

bytesReadback to summary
private long bytesRead

The number of bytes read so far.

bytesReadMarkback to summary
private long bytesReadMark
encoderback to summary
private final CharsetEncoder encoder

Encoder for calculating the number of bytes for each character read.

lastCharback to summary
private int lastChar

The last char returned

lastCharMarkback to summary
private int lastCharMark
lineNumberback to summary
private long lineNumber

The count of EOLs (CR/LF/CRLF) seen so far

lineNumberMarkback to summary
private long lineNumberMark
positionback to summary
private long position

The position, which is the number of characters read so far

positionMarkback to summary
private long positionMark

Constructor Detail

ExtendedBufferedReaderback to summary
pack-priv ExtendedBufferedReader(final Reader reader)

Constructs a new instance using the default buffer size.

ExtendedBufferedReaderback to summary
pack-priv ExtendedBufferedReader(final Reader reader, final Charset charset, final 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:Reader

the reader supports a look-ahead option.

charset:Charset

the character set for encoding, or null if not applicable.

trackBytes:boolean

true to enable byte tracking; false to disable it.

Method Detail

closeback to summary
public void close() throws IOException

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.close.

Implements java.io.Closeable.close.

Closes the stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getBytesReadback to summary
pack-priv long getBytesRead()

Gets the number of bytes read by the reader.

Returns:long

the number of bytes read by the read

getEncodedCharLengthback to summary
private int getEncodedCharLength(final int current) throws CharacterCodingException

Gets 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.
  • U+10000 to U+10FFFF (Supplementary Characters):
    • Represented as a pair of chars:
    • The first char is from the high-surrogates range (?-?).
    • The second char is from the low-surrogates range (?-?).
    • Includes UTF-8 encodings of some 3-byte characters and all 4-byte characters.
Parameters
current:int

the current character to process.

Returns:int

the byte length of the character.

Exceptions
CharacterCodingException:
if the character cannot be encoded.
getLastCharback to summary
pack-priv 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 the peek() method. If no character has been read then this will return Constants#UNDEFINED. If the end of the stream was reached on the last read then this will return IOUtils#EOF.

Returns:int

the last character that was read

getLineNumberback to summary
pack-priv long getLineNumber()

Returns the current line number

Returns:long

the current line number

getPositionback to summary
pack-priv long getPosition()

Gets the character position in the reader.

Returns:long

the current position in the reader (counting characters, not bytes since this is a Reader)

markback to summary
public void mark(final int readAheadLimit) throws IOException

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.mark.

Doc from org.apache.commons.io.input.UnsynchronizedBufferedReader.mark.

Sets a mark position in this reader. The parameter markLimit indicates how many characters can be read before the mark is invalidated. Calling reset() will reposition the reader back to the marked position if markLimit has not been surpassed.

Parameters
readAheadLimit:int

the number of characters that can be read before the mark is invalidated.

Annotations
@Override
Exceptions
IOException:
if an error occurs while setting a mark in this reader.
readback to summary
public int read() throws IOException

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.read.

Doc from org.apache.commons.io.input.UnsynchronizedBufferedReader.read.

Reads a single character from this reader and returns it with the two higher-order bytes set to 0. If possible, BufferedReader returns a character from the buffer. If there are no characters available in the buffer, it fills the buffer and then returns a character. It returns -1 if there are no more characters in the source reader.

Returns:int

the character read or -1 if the end of the source reader has been reached.

Annotations
@Override
Exceptions
IOException:
if this reader is closed or some other I/O error occurs.
readback to summary
public int read(final char[] buf, final int offset, final int length) throws IOException

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.read.

Doc from org.apache.commons.io.input.UnsynchronizedBufferedReader.read.

Reads at most length characters from this reader and stores them at offset in the character array buffer. Returns the number of characters actually read or -1 if the end of the source reader has been reached. If all the buffered characters have been used, a mark has not been set and the requested number of characters is larger than this readers buffer size, BufferedReader bypasses the buffer and simply places the results directly into buffer.

Parameters
buf:char[]

the character array to store the characters read.

offset:int

the initial position in buffer to store the bytes read from this reader.

length:int

the maximum number of characters to read, must be non-negative.

Returns:int

number of characters read or -1 if the end of the source reader has been reached.

Annotations
@Override
Exceptions
IOException:
if this reader is closed or some other I/O error occurs.
readLineback to summary
public String readLine() throws IOException

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.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 lineNumber and updates position.

Sets lastChar to Constants.EOF at EOF, otherwise the last EOL character.

Returns:String

the line that was read, or null if reached EOF.

Annotations
@Override
Exceptions
IOException:

Doc from org.apache.commons.io.input.UnsynchronizedBufferedReader.readLine.

if this reader is closed or some other I/O error occurs.

resetback to summary
public void reset() throws IOException

Overrides org.apache.commons.io.input.UnsynchronizedBufferedReader.reset.

Doc from org.apache.commons.io.input.UnsynchronizedBufferedReader.reset.

Resets this reader's position to the last mark() location. Invocations of read() and skip() will occur from this new location.

Annotations
@Override
Exceptions
IOException:
if this reader is closed or no mark has been set.