2222import static org .apache .commons .csv .Constants .UNDEFINED ;
2323import static org .apache .commons .io .IOUtils .EOF ;
2424
25- import java .io .BufferedReader ;
2625import java .io .IOException ;
2726import java .io .Reader ;
2827
2928import org .apache .commons .io .IOUtils ;
29+ import org .apache .commons .io .input .UnsynchronizedBufferedReader ;
3030
3131/**
3232 * A special buffered reader which supports sophisticated read access.
3535 * {@link #read()}. This reader also tracks how many characters have been read with {@link #getPosition()}.
3636 * </p>
3737 */
38- final class ExtendedBufferedReader extends BufferedReader {
38+ final class ExtendedBufferedReader extends UnsynchronizedBufferedReader {
3939
4040 /** The last char returned */
4141 private int lastChar = UNDEFINED ;
42+ private int lastCharMark = UNDEFINED ;
4243
4344 /** The count of EOLs (CR/LF/CRLF) seen so far */
4445 private long lineNumber ;
46+ private long lineNumberMark ;
4547
4648 /** The position, which is the number of characters read so far */
4749 private long position ;
48-
49- private boolean closed ;
50+ private long positionMark ;
5051
5152 /**
5253 * Constructs a new instance using the default buffer size.
@@ -55,6 +56,22 @@ final class ExtendedBufferedReader extends BufferedReader {
5556 super (reader );
5657 }
5758
59+ @ Override
60+ public void mark (final int readAheadLimit ) throws IOException {
61+ lineNumberMark = lineNumber ;
62+ lastCharMark = lastChar ;
63+ positionMark = position ;
64+ super .mark (readAheadLimit );
65+ }
66+
67+ @ Override
68+ public void reset () throws IOException {
69+ lineNumber = lineNumberMark ;
70+ lastChar = lastCharMark ;
71+ position = positionMark ;
72+ super .reset ();
73+ }
74+
5875 /**
5976 * Closes the stream.
6077 *
@@ -64,7 +81,6 @@ final class ExtendedBufferedReader extends BufferedReader {
6481 @ Override
6582 public void close () throws IOException {
6683 // Set ivars before calling super close() in case close() throws an IOException.
67- closed = true ;
6884 lastChar = EOF ;
6985 super .close ();
7086 }
@@ -74,7 +90,7 @@ public void close() throws IOException {
7490 *
7591 * @return the current line number
7692 */
77- long getCurrentLineNumber () {
93+ long getLineNumber () {
7894 // Check if we are at EOL or EOF or just starting
7995 if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == EOF ) {
8096 return lineNumber ; // counter is accurate
@@ -103,42 +119,6 @@ long getPosition() {
103119 return this .position ;
104120 }
105121
106- public boolean isClosed () {
107- return closed ;
108- }
109-
110- /**
111- * Returns the next character in the current reader without consuming it. So the next call to {@link #read()} will
112- * still return this value. Does not affect the line number or the last character.
113- *
114- * @return the next character
115- *
116- * @throws IOException
117- * If an I/O error occurs
118- */
119- int peek () throws IOException {
120- super .mark (1 );
121- final int c = super .read ();
122- super .reset ();
123- return c ;
124- }
125-
126- /**
127- * Populates the buffer with the next {@code buf.length} characters in the current reader without consuming them. The next call to {@link #read()} will
128- * still return the next value. This doesn't affect the line number or the last character.
129- *
130- * @param buf the buffer to fill for the look ahead.
131- * @return The number of characters peeked, or -1 if the end of the stream has been reached.
132- * @throws IOException If an I/O error occurs
133- */
134- int peek (final char [] buf ) throws IOException {
135- final int n = buf .length ;
136- super .mark (n );
137- final int c = super .read (buf , 0 , n );
138- super .reset ();
139- return c ;
140- }
141-
142122 @ Override
143123 public int read () throws IOException {
144124 final int current = super .read ();
0 commit comments