[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
This commit is contained in:
Gary D. Gregory 2014-09-17 02:57:46 +00:00
parent 2785c355aa
commit 9e4e3dc379
1 changed files with 16 additions and 1 deletions

View File

@ -30,7 +30,8 @@ import java.io.Reader;
* A special buffered reader which supports sophisticated read access.
* <p>
* In particular the reader supports a look-ahead option, which allows you to see the next char returned by
* {@link #read()}.
* {@link #read()}. This reader also tracks how many characters have been read with {@link #getPosition()}.
* </p>
*
* @version $Id$
*/
@ -42,6 +43,9 @@ final class ExtendedBufferedReader extends BufferedReader {
/** The count of EOLs (CR/LF/CRLF) seen so far */
private long eolCounter = 0;
/** The position, which is number of characters read so far */
private long position = 0;
private boolean closed;
/**
@ -58,6 +62,7 @@ final class ExtendedBufferedReader extends BufferedReader {
eolCounter++;
}
lastChar = current;
this.position++;
return lastChar;
}
@ -100,6 +105,7 @@ final class ExtendedBufferedReader extends BufferedReader {
lastChar = END_OF_STREAM;
}
position += len;
return len;
}
@ -157,6 +163,15 @@ final class ExtendedBufferedReader extends BufferedReader {
return eolCounter + 1; // Allow for counter being incremented only at EOL
}
/**
* Gets the character position in the reader.
*
* @return the current position in the reader (counting characters, not bytes since this is a Reader)
*/
long getPosition() {
return this.position;
}
public boolean isClosed() {
return closed;
}