Skip to content

Commit 9e4e3dc

Browse files
committed
[CSV-131] Save positions of records to enable random access. First commit for this new feature. Let the ExtendedBufferedReader track how many characters it has read so far.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1625455 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2785c35 commit 9e4e3dc

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
* A special buffered reader which supports sophisticated read access.
3131
* <p>
3232
* In particular the reader supports a look-ahead option, which allows you to see the next char returned by
33-
* {@link #read()}.
33+
* {@link #read()}. This reader also tracks how many characters have been read with {@link #getPosition()}.
34+
* </p>
3435
*
3536
* @version $Id$
3637
*/
@@ -42,6 +43,9 @@ final class ExtendedBufferedReader extends BufferedReader {
4243
/** The count of EOLs (CR/LF/CRLF) seen so far */
4344
private long eolCounter = 0;
4445

46+
/** The position, which is number of characters read so far */
47+
private long position = 0;
48+
4549
private boolean closed;
4650

4751
/**
@@ -58,6 +62,7 @@ public int read() throws IOException {
5862
eolCounter++;
5963
}
6064
lastChar = current;
65+
this.position++;
6166
return lastChar;
6267
}
6368

@@ -100,6 +105,7 @@ public int read(final char[] buf, final int offset, final int length) throws IOE
100105
lastChar = END_OF_STREAM;
101106
}
102107

108+
position += len;
103109
return len;
104110
}
105111

@@ -157,6 +163,15 @@ long getCurrentLineNumber() {
157163
return eolCounter + 1; // Allow for counter being incremented only at EOL
158164
}
159165

166+
/**
167+
* Gets the character position in the reader.
168+
*
169+
* @return the current position in the reader (counting characters, not bytes since this is a Reader)
170+
*/
171+
long getPosition() {
172+
return this.position;
173+
}
174+
160175
public boolean isClosed() {
161176
return closed;
162177
}

0 commit comments

Comments
 (0)