Skip to content

Commit

Permalink
MLE-12358: Mark and reset the byteRead
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenJAN committed Oct 31, 2024
1 parent b397c16 commit 16756c6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
28 changes: 5 additions & 23 deletions src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,11 @@ final class ExtendedBufferedReader extends UnsynchronizedBufferedReader {

/** The number of bytes read so far */
private long bytesRead;
private long bytesReadMark;

/** Encoder used to calculate the bytes of characters */
CharsetEncoder encoder;

/**
* A flag to indicate if the read is a peek operation.
*/
private boolean isReadPeek;

/**
* Constructs a new instance using the default buffer size.
*/
Expand All @@ -75,7 +72,6 @@ final class ExtendedBufferedReader extends UnsynchronizedBufferedReader {
if (encoding != null) {
encoder = Charset.forName(encoding).newEncoder();
}
isReadPeek = false;
}

/**
Expand Down Expand Up @@ -130,6 +126,7 @@ public void mark(final int readAheadLimit) throws IOException {
lineNumberMark = lineNumber;
lastCharMark = lastChar;
positionMark = position;
bytesReadMark = bytesRead;
super.mark(readAheadLimit);
}

Expand All @@ -140,7 +137,7 @@ public int read() throws IOException {
current == EOF && lastChar != CR && lastChar != LF && lastChar != EOF) {
lineNumber++;
}
if (encoder != null && !isReadPeek) {
if (encoder != null) {
this.bytesRead += getCharBytes(current);
}
lastChar = current;
Expand Down Expand Up @@ -241,6 +238,7 @@ public void reset() throws IOException {
lineNumber = lineNumberMark;
lastChar = lastCharMark;
position = positionMark;
bytesRead = bytesReadMark;
super.reset();
}

Expand All @@ -253,20 +251,4 @@ long getBytesRead() {
return this.bytesRead;
}

/**
* Returns the next character in the current reader without consuming it. So the next call to {@link #read()} will still return this value.
*
* @return the next character
* @throws IOException If an I/O error occurs
*/
@Override
public int peek() throws IOException {
isReadPeek = true;
mark(1);
final int c = read();
reset();
isReadPeek = false;
return c;
}

}
1 change: 1 addition & 0 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ public void testGetRecordThreeBytesRead() throws Exception {
"11111111111111,'4017-09-01',きちんと節分近くには咲いてる~,v4\n" +
"22222222222222,'4017-01-01',おはよう私の友人~,v4\n" +
"33333333333333,'4017-01-01',きる自然の力ってすごいな~,v4\n";
// String code = "'1',4";
// final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'');
final CSVFormat format = CSVFormat.Builder.create()
.setDelimiter(',')
Expand Down

0 comments on commit 16756c6

Please sign in to comment.