Skip to content

Commit 274b4ce

Browse files
committed
skip byte counting at EOF in ExtendedBufferedReader.read
1 parent b112daa commit 274b4ce

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public int read() throws IOException {
210210
if (current == CR || current == LF && lastChar != CR || current == EOF && lastChar != CR && lastChar != LF && lastChar != EOF) {
211211
lineNumber++;
212212
}
213-
if (encoder != null) {
213+
if (encoder != null && current != EOF) {
214214
this.bytesRead += getEncodedCharLength(current);
215215
}
216216
lastChar = current;

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,27 @@ void testGetBytePositionMultiCharacterDelimiterWithSupplementaryCharacter() thro
691691
}
692692
}
693693

694+
@Test
695+
void testGetBytePositionWithSingleByteCharset() throws IOException {
696+
// A single-byte charset cannot encode U+FFFF, the char value of the EOF sentinel.
697+
// Byte counting must skip the EOF read so a valid file parses without throwing.
698+
final String code = "a,b\nc,d\n";
699+
try (CSVParser parser = CSVParser.builder()
700+
.setReader(new StringReader(code))
701+
.setFormat(CSVFormat.DEFAULT)
702+
.setCharset(StandardCharsets.ISO_8859_1)
703+
.setTrackBytes(true)
704+
.get()) {
705+
final CSVRecord first = parser.nextRecord();
706+
final CSVRecord second = parser.nextRecord();
707+
assertNotNull(first);
708+
assertNotNull(second);
709+
assertNull(parser.nextRecord());
710+
assertEquals(0, first.getBytePosition());
711+
assertEquals(4, second.getBytePosition());
712+
}
713+
}
714+
694715
@Test
695716
void testGetBytePositionWithCharacterOffsetAndMultiBytePrefix() throws Exception {
696717
final String row0 = "é,x\n";

0 commit comments

Comments
 (0)