You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/07/21 17:56:05 UTC

(commons-csv) branch master updated: Fill the lookahead buffer on short reads from a chunked source (#625).

This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git


The following commit(s) were added to refs/heads/master by this push:
     new c4ac331f Fill the lookahead buffer on short reads from a chunked source (#625).
c4ac331f is described below

commit c4ac331f61bb196635ae9acd9b539738cea281c5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jul 21 10:55:59 2026 -0700

    Fill the lookahead buffer on short reads from a chunked source (#625).
    
    Sort members
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/csv/CSVParserTest.java | 26 ++++-----
 .../commons/csv/ExtendedBufferedReaderTest.java    | 66 +++++++++++-----------
 3 files changed, 47 insertions(+), 46 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4903cedf..4fe2c5e6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -69,6 +69,7 @@
       <action type="fix" dev="ggregory" due-to="Naveed Khan, Gary Gregory">Skip trailing delimiter on comment lines in printComment (#623).</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory, Naveed Khan">Fix SpotBugs 4.10.3 USO_UNSAFE_METHOD_SYNCHRONIZATION in CSVFormat print methods (#624).</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory, Naveed Khan">Count rows produced instead of getRow() in printRecords(ResultSet) (#627).</action>
+      <action type="fix" dev="ggregory" due-to="Gary Gregory, saleem malik">Fill the lookahead buffer on short reads from a chunked source (#625).</action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Gary Gregory, Indy, Sylvia van Os" issue="CSV-307">Add an "Android Compatibility" section to the web site.</action>
       <action type="add" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-325">Add CSVParser.Builder.setByteOffset(long) (#604).</action>
diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index 57b35492..c9caa308 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -1696,29 +1696,29 @@ class CSVParserTest {
     }
 
     @Test
-    void testParseWithDelimiterStringWithEscape() throws IOException {
-        final String source = "a![!|!]b![|]c[|]xyz\r\nabc[abc][|]xyz";
+    void testParseWithDelimiterStringFromChunkedReader() throws IOException {
         final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
-        try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
+        try (CSVParser csvParser = csvFormat.parse(new ChunkedReader("a[|]b\r\nc![!|!]d[|]e"))) {
             CSVRecord csvRecord = csvParser.nextRecord();
-            assertEquals("a[|]b![|]c", csvRecord.get(0));
-            assertEquals("xyz", csvRecord.get(1));
+            assertEquals("a", csvRecord.get(0));
+            assertEquals("b", csvRecord.get(1));
             csvRecord = csvParser.nextRecord();
-            assertEquals("abc[abc]", csvRecord.get(0));
-            assertEquals("xyz", csvRecord.get(1));
+            assertEquals("c[|]d", csvRecord.get(0));
+            assertEquals("e", csvRecord.get(1));
         }
     }
 
     @Test
-    void testParseWithDelimiterStringFromChunkedReader() throws IOException {
+    void testParseWithDelimiterStringWithEscape() throws IOException {
+        final String source = "a![!|!]b![|]c[|]xyz\r\nabc[abc][|]xyz";
         final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
-        try (CSVParser csvParser = csvFormat.parse(new ChunkedReader("a[|]b\r\nc![!|!]d[|]e"))) {
+        try (CSVParser csvParser = csvFormat.parse(new StringReader(source))) {
             CSVRecord csvRecord = csvParser.nextRecord();
-            assertEquals("a", csvRecord.get(0));
-            assertEquals("b", csvRecord.get(1));
+            assertEquals("a[|]b![|]c", csvRecord.get(0));
+            assertEquals("xyz", csvRecord.get(1));
             csvRecord = csvParser.nextRecord();
-            assertEquals("c[|]d", csvRecord.get(0));
-            assertEquals("e", csvRecord.get(1));
+            assertEquals("abc[abc]", csvRecord.get(0));
+            assertEquals("xyz", csvRecord.get(1));
         }
     }
 
diff --git a/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java b/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java
index 2cac4d02..ff318c38 100644
--- a/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java
+++ b/src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java
@@ -55,6 +55,39 @@ class ExtendedBufferedReaderTest {
         }
     }
 
+    @Test
+    void testPeekArrayPastEndOfChunkedReader() throws Exception {
+        try (ExtendedBufferedReader br = new ExtendedBufferedReader(new ChunkedReader("ab"))) {
+            final char[] peeked = new char[4];
+            assertEquals(2, br.peek(peeked));
+            assertEquals('a', peeked[0]);
+            assertEquals('b', peeked[1]);
+        }
+    }
+
+    @Test
+    void testReadAndPeekArrayFromChunkedReader() throws Exception {
+        try (ExtendedBufferedReader br = new ExtendedBufferedReader(new ChunkedReader("abcdef"))) {
+            final char[] peeked = new char[3];
+            assertEquals(3, br.peek(peeked));
+            assertArrayEquals(new char[] { 'a', 'b', 'c' }, peeked);
+            final char[] read = new char[3];
+            assertEquals(3, br.read(read, 0, 3));
+            assertArrayEquals(new char[] { 'a', 'b', 'c' }, read);
+        }
+    }
+
+    @Test
+    void testReadArrayPastEndOfChunkedReader() throws Exception {
+        try (ExtendedBufferedReader br = new ExtendedBufferedReader(new ChunkedReader("ab"))) {
+            final char[] read = new char[4];
+            assertEquals(2, br.read(read, 0, 4));
+            assertEquals('a', read[0]);
+            assertEquals('b', read[1]);
+            assertEquals(EOF, br.read(read, 0, 4));
+        }
+    }
+
     /*
      * Test to illustrate https://issues.apache.org/jira/browse/CSV-75
      */
@@ -240,37 +273,4 @@ class ExtendedBufferedReaderTest {
             assertEquals('d', br.getLastChar());
         }
     }
-
-    @Test
-    void testReadAndPeekArrayFromChunkedReader() throws Exception {
-        try (ExtendedBufferedReader br = new ExtendedBufferedReader(new ChunkedReader("abcdef"))) {
-            final char[] peeked = new char[3];
-            assertEquals(3, br.peek(peeked));
-            assertArrayEquals(new char[] { 'a', 'b', 'c' }, peeked);
-            final char[] read = new char[3];
-            assertEquals(3, br.read(read, 0, 3));
-            assertArrayEquals(new char[] { 'a', 'b', 'c' }, read);
-        }
-    }
-
-    @Test
-    void testReadArrayPastEndOfChunkedReader() throws Exception {
-        try (ExtendedBufferedReader br = new ExtendedBufferedReader(new ChunkedReader("ab"))) {
-            final char[] read = new char[4];
-            assertEquals(2, br.read(read, 0, 4));
-            assertEquals('a', read[0]);
-            assertEquals('b', read[1]);
-            assertEquals(EOF, br.read(read, 0, 4));
-        }
-    }
-
-    @Test
-    void testPeekArrayPastEndOfChunkedReader() throws Exception {
-        try (ExtendedBufferedReader br = new ExtendedBufferedReader(new ChunkedReader("ab"))) {
-            final char[] peeked = new char[4];
-            assertEquals(2, br.peek(peeked));
-            assertEquals('a', peeked[0]);
-            assertEquals('b', peeked[1]);
-        }
-    }
 }