From de1838ea067f3fbc4c7c21b9eeae077c739ecb73 Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Tue, 27 Mar 2012 00:49:17 +0000 Subject: [PATCH] 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 --- .../commons/csv/ExtendedBufferedReader.java | 20 ++++++++++++------- .../org/apache/commons/csv/CSVParserTest.java | 1 - 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java index 2ae113f3..219fe68e 100644 --- a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java +++ b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java @@ -54,11 +54,11 @@ class ExtendedBufferedReader extends BufferedReader { @Override public int read() throws IOException { - lastChar = super.read(); - - if (lastChar == '\n') { + int current = super.read(); + if (current == '\r' || (current == '\n' && lastChar != '\r')) { lineCounter++; } + lastChar = current; return lastChar; } @@ -85,14 +85,20 @@ class ExtendedBufferedReader extends BufferedReader { int len = super.read(buf, offset, length); if (len > 0) { - lastChar = buf[offset + len - 1]; - + 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++; } } - + + lastChar = buf[offset + len - 1]; + } else if (len == -1) { lastChar = END_OF_STREAM; } diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 711aa2f5..e84d656b 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -502,7 +502,6 @@ public class CSVParserTest { } @Test - @Ignore("Line counting doesn't work with CR alone as the line separator, see CSV-75") public void testGetLineNumberWithCR() throws Exception { CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r"));