From a6ca4165a8f8d156f0fbfe33627bac1509e7ae9c Mon Sep 17 00:00:00 2001 From: Alexander Bondarev Date: Wed, 7 Jul 2021 15:35:17 +0300 Subject: [PATCH] [CSV-158] Fix EOL checking for read array in ExtendedBufferedReader (#5) * Fix eol checking for read array in ExtendedBufferedReader * Add test for eol checking when reading to different buffers Co-authored-by: mirasrael Co-authored-by: Alexander Bondarev --- .../org/apache/commons/csv/ExtendedBufferedReader.java | 2 +- .../apache/commons/csv/ExtendedBufferedReaderTest.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java index b4b451f7..63efb640 100644 --- a/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java +++ b/src/main/java/org/apache/commons/csv/ExtendedBufferedReader.java @@ -164,7 +164,7 @@ final class ExtendedBufferedReader extends BufferedReader { for (int i = offset; i < offset + len; i++) { final char ch = buf[i]; if (ch == LF) { - if (CR != (i > 0 ? buf[i - 1] : lastChar)) { + if (CR != (i > offset ? buf[i - 1] : lastChar)) { eolCounter++; } } else if (ch == CR) { diff --git a/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java b/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java index a570aec5..e83f94b1 100644 --- a/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java +++ b/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java @@ -204,6 +204,15 @@ public class ExtendedBufferedReaderTest { } } + @Test + public void testReadingInDifferentBuffer() throws Exception { + char[] tmp1 = new char[2], tmp2 = new char[4]; + ExtendedBufferedReader br = createBufferedReader("1\r\n2\r\n"); + br.read(tmp1, 0, 2); + br.read(tmp2, 2, 2); + assertEquals(2, br.getCurrentLineNumber()); + } + private ExtendedBufferedReader createBufferedReader(final String s) { return new ExtendedBufferedReader(new StringReader(s)); }