Skip to content

Commit 966b385

Browse files
[CSV-327] Limit parser maxRows by produced records
1 parent 361056d commit 966b385

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-322">CSVFormat.Builder.setQuote() does not refresh quotedNullString (#2447).</action>
5252
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-324">Lexer.isDelimiter() accepts a partial multi-character delimiter at EOF (#603).</action>
5353
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-325">CSVParser applies characterOffset to bytePosition (#604).</action>
54+
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-327">CSVParser applies maxRows to record numbers instead of rows produced when setRecordNumber(...) is used.</action>
5455
<!-- ADD -->
5556
<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>
5657
<action type="add" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-325">Add CSVParser.Builder.setByteOffset(long) (#604).</action>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public Builder setTrackBytes(final boolean trackBytes) {
237237

238238
final class CSVRecordIterator implements Iterator<CSVRecord> {
239239
private CSVRecord current;
240+
private long recordCount;
240241

241242
/**
242243
* Gets the next record or null at the end of stream or max rows read.
@@ -247,8 +248,11 @@ final class CSVRecordIterator implements Iterator<CSVRecord> {
247248
*/
248249
private CSVRecord getNextRecord() {
249250
CSVRecord record = null;
250-
if (format.useRow(recordNumber + 1)) {
251+
if (format.useRow(recordCount + 1)) {
251252
record = Uncheck.get(CSVParser.this::nextRecord);
253+
if (record != null) {
254+
recordCount++;
255+
}
252256
}
253257
return record;
254258
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,23 @@ void testGetRecordsMaxRows(final long maxRows) throws IOException {
965965
}
966966
}
967967

968+
/**
969+
* Tests <a href="https://issues.apache.org/jira/browse/CSV-327">CSV-327</a>.
970+
*/
971+
@Test
972+
void testGetRecordsMaxRowsWithRecordNumberOffset() throws IOException {
973+
try (CSVParser parser = CSVParser.builder()
974+
.setReader(new StringReader("a,b\nc,d\n"))
975+
.setFormat(CSVFormat.DEFAULT.builder().setMaxRows(1).get())
976+
.setRecordNumber(2)
977+
.get()) {
978+
final List<CSVRecord> records = parser.getRecords();
979+
assertEquals(1, records.size());
980+
assertEquals(2, records.get(0).getRecordNumber());
981+
assertValuesEquals(new String[] { "a", "b" }, records.get(0));
982+
}
983+
}
984+
968985
@Test
969986
void testGetRecordThreeBytesRead() throws Exception {
970987
final String code = "id,date,val5,val4\n" +

0 commit comments

Comments
 (0)