Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit de1838e

Browse files
committed
CSV-75 ExtendedBufferReader does not handle EOL consistently
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1305694 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0833f45 commit de1838e

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class ExtendedBufferedReader extends BufferedReader {
5454

5555
@Override
5656
public int read() throws IOException {
57-
lastChar = super.read();
58-
59-
if (lastChar == '\n') {
57+
int current = super.read();
58+
if (current == '\r' || (current == '\n' && lastChar != '\r')) {
6059
lineCounter++;
6160
}
61+
lastChar = current;
6262
return lastChar;
6363
}
6464

@@ -85,14 +85,20 @@ public int read(char[] buf, int offset, int length) throws IOException {
8585
int len = super.read(buf, offset, length);
8686

8787
if (len > 0) {
88-
lastChar = buf[offset + len - 1];
89-
88+
9089
for (int i = offset; i < offset + len; i++) {
91-
if (buf[i] == '\n') {
90+
char ch = buf[i];
91+
if (ch == '\n') {
92+
if ('\r' != (i > 0 ? buf[i-1]: lastChar)) {
93+
lineCounter++;
94+
}
95+
} else if (ch == '\r') {
9296
lineCounter++;
9397
}
9498
}
95-
99+
100+
lastChar = buf[offset + len - 1];
101+
96102
} else if (len == -1) {
97103
lastChar = END_OF_STREAM;
98104
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ public void testGetLineNumberWithCRLF() throws Exception {
502502
}
503503

504504
@Test
505-
@Ignore("Line counting doesn't work with CR alone as the line separator, see CSV-75")
506505
public void testGetLineNumberWithCR() throws Exception {
507506
CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r"));
508507

0 commit comments

Comments
 (0)