Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-322">CSVFormat.Builder.setQuote() does not refresh quotedNullString (#2447).</action>
<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>
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-325">CSVParser applies characterOffset to bytePosition (#604).</action>
<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>
<!-- 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>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/apache/commons/csv/CSVParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public Builder setTrackBytes(final boolean trackBytes) {

final class CSVRecordIterator implements Iterator<CSVRecord> {
private CSVRecord current;
private long recordCount;

/**
* Gets the next record or null at the end of stream or max rows read.
Expand All @@ -247,8 +248,11 @@ final class CSVRecordIterator implements Iterator<CSVRecord> {
*/
private CSVRecord getNextRecord() {
CSVRecord record = null;
if (format.useRow(recordNumber + 1)) {
if (format.useRow(recordCount + 1)) {
record = Uncheck.get(CSVParser.this::nextRecord);
if (record != null) {
recordCount++;
}
}
return record;
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,23 @@ void testGetRecordsMaxRows(final long maxRows) throws IOException {
}
}

/**
* Tests <a href="https://issues.apache.org/jira/browse/CSV-327">CSV-327</a>.
*/
@Test
void testGetRecordsMaxRowsWithRecordNumberOffset() throws IOException {
try (CSVParser parser = CSVParser.builder()
.setReader(new StringReader("a,b\nc,d\n"))
.setFormat(CSVFormat.DEFAULT.builder().setMaxRows(1).get())
.setRecordNumber(2)
.get()) {
final List<CSVRecord> records = parser.getRecords();
assertEquals(1, records.size());
assertEquals(2, records.get(0).getRecordNumber());
assertValuesEquals(new String[] { "a", "b" }, records.get(0));
}
}

@Test
void testGetRecordThreeBytesRead() throws Exception {
final String code = "id,date,val5,val4\n" +
Expand Down