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
This commit is contained in:
Sebastian Bazley 2012-03-27 00:49:17 +00:00
parent 0833f45bff
commit de1838ea06
2 changed files with 13 additions and 8 deletions

View File

@ -54,11 +54,11 @@ class ExtendedBufferedReader extends BufferedReader {
@Override @Override
public int read() throws IOException { public int read() throws IOException {
lastChar = super.read(); int current = super.read();
if (current == '\r' || (current == '\n' && lastChar != '\r')) {
if (lastChar == '\n') {
lineCounter++; lineCounter++;
} }
lastChar = current;
return lastChar; return lastChar;
} }
@ -85,14 +85,20 @@ class ExtendedBufferedReader extends BufferedReader {
int len = super.read(buf, offset, length); int len = super.read(buf, offset, length);
if (len > 0) { if (len > 0) {
lastChar = buf[offset + len - 1];
for (int i = offset; i < offset + len; i++) { for (int i = offset; i < offset + len; i++) {
if (buf[i] == '\n') { char ch = buf[i];
if (ch == '\n') {
if ('\r' != (i > 0 ? buf[i-1]: lastChar)) {
lineCounter++;
}
} else if (ch == '\r') {
lineCounter++; lineCounter++;
} }
} }
lastChar = buf[offset + len - 1];
} else if (len == -1) { } else if (len == -1) {
lastChar = END_OF_STREAM; lastChar = END_OF_STREAM;
} }

View File

@ -502,7 +502,6 @@ public class CSVParserTest {
} }
@Test @Test
@Ignore("Line counting doesn't work with CR alone as the line separator, see CSV-75")
public void testGetLineNumberWithCR() throws Exception { public void testGetLineNumberWithCR() throws Exception {
CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r")); CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r"));