Skip to content

Commit ab3d723

Browse files
committed
skip trailing delimiter on comment lines in printComment
1 parent 1fab7ca commit ab3d723

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void printComment(final String comment) throws IOException {
246246
}
247247
// falls-through: break intentionally excluded.
248248
case LF:
249-
println();
249+
printCommentSeparator();
250250
appendable.append(format.getCommentMarker().charValue()); // Explicit unboxing is intentional
251251
appendable.append(SP);
252252
break;
@@ -255,12 +255,26 @@ public void printComment(final String comment) throws IOException {
255255
break;
256256
}
257257
}
258-
println();
258+
printCommentSeparator();
259259
} finally {
260260
lock.unlock();
261261
}
262262
}
263263

264+
/**
265+
* Ends a comment line with the record separator only. A comment is not a record, so the trailing delimiter that
266+
* {@link #println()} writes must not follow it.
267+
*
268+
* @throws IOException If an I/O error occurs.
269+
*/
270+
private void printCommentSeparator() throws IOException {
271+
final String recordSeparator = format.getRecordSeparator();
272+
if (recordSeparator != null) {
273+
appendable.append(recordSeparator);
274+
}
275+
newRecord = true;
276+
}
277+
264278
/**
265279
* Prints headers for a result set based on its metadata.
266280
*

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,38 @@ void testMultiLineComment() throws IOException {
11071107
}
11081108
}
11091109

1110+
@Test
1111+
void testMultiLineCommentWithTrailingDelimiter() throws IOException {
1112+
// A comment is not a record, so the trailing delimiter must not follow comment lines, or it becomes
1113+
// part of the comment text on read back. The following data record still gets its trailing delimiter.
1114+
final CSVFormat format = CSVFormat.DEFAULT.builder().setCommentMarker('#').setTrailingDelimiter(true).get();
1115+
final StringWriter sw = new StringWriter();
1116+
try (CSVPrinter printer = new CSVPrinter(sw, format)) {
1117+
printer.printComment("This is a comment\non multiple lines");
1118+
printer.printRecord("A", "B");
1119+
}
1120+
final String string = sw.toString();
1121+
assertEquals("# This is a comment" + RECORD_SEPARATOR + "# on multiple lines" + RECORD_SEPARATOR + "A,B," + RECORD_SEPARATOR, string);
1122+
try (CSVParser parser = CSVParser.parse(string, format)) {
1123+
final List<CSVRecord> records = parser.getRecords();
1124+
assertEquals(1, records.size());
1125+
assertEquals("This is a comment\non multiple lines", records.get(0).getComment());
1126+
assertEquals("A", records.get(0).get(0));
1127+
assertEquals("B", records.get(0).get(1));
1128+
}
1129+
}
1130+
1131+
@Test
1132+
void testMultiLineCommentWithoutRecordSeparator() throws IOException {
1133+
// A null record separator writes nothing between comment lines, matching the record output path.
1134+
final CSVFormat format = CSVFormat.DEFAULT.builder().setCommentMarker('#').setRecordSeparator(null).get();
1135+
final StringWriter sw = new StringWriter();
1136+
try (CSVPrinter printer = new CSVPrinter(sw, format)) {
1137+
printer.printComment("a\nb");
1138+
}
1139+
assertEquals("# a# b", sw.toString());
1140+
}
1141+
11101142
@Test
11111143
void testMySqlNullOutput() throws IOException {
11121144
Object[] s = new String[] { "NULL", null };

0 commit comments

Comments
 (0)