Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public int read() throws IOException {
if (current == CR || current == LF && lastChar != CR || current == EOF && lastChar != CR && lastChar != LF && lastChar != EOF) {
lineNumber++;
}
if (encoder != null) {
if (encoder != null && current != EOF) {
this.bytesRead += getEncodedCharLength(current);
}
lastChar = current;
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,27 @@ void testGetBytePositionMultiCharacterDelimiterWithSupplementaryCharacter() thro
}
}

@Test
void testGetBytePositionWithSingleByteCharset() throws IOException {
// A single-byte charset cannot encode U+FFFF, the char value of the EOF sentinel.
// Byte counting must skip the EOF read so a valid file parses without throwing.
final String code = "a,b\nc,d\n";
try (CSVParser parser = CSVParser.builder()
.setReader(new StringReader(code))
.setFormat(CSVFormat.DEFAULT)
.setCharset(StandardCharsets.ISO_8859_1)
.setTrackBytes(true)
.get()) {
final CSVRecord first = parser.nextRecord();
final CSVRecord second = parser.nextRecord();
assertNotNull(first);
assertNotNull(second);
assertNull(parser.nextRecord());
assertEquals(0, first.getBytePosition());
assertEquals(4, second.getBytePosition());
}
}

@Test
void testGetBytePositionWithCharacterOffsetAndMultiBytePrefix() throws Exception {
final String row0 = "é,x\n";
Expand Down
Loading