Skip to content

Commit ed49199

Browse files
committed
[CSV-234] Enable escaping of quotes in Clobs
1 parent e24dc31 commit ed49199

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,8 +1240,7 @@ private void print(final Reader reader, final Appendable out, final boolean newR
12401240
out.append(getDelimiter());
12411241
}
12421242
if (isQuoteCharacterSet()) {
1243-
// the original object is needed so can check for Number
1244-
printWithQuotes(reader, out, newRecord);
1243+
printWithQuotes(reader, out);
12451244
} else if (isEscapeCharacterSet()) {
12461245
printWithEscapes(reader, out);
12471246
} else if (out instanceof Writer) {
@@ -1501,18 +1500,43 @@ private void printWithQuotes(final Object object, final CharSequence value, fina
15011500
*
15021501
* @throws IOException
15031502
*/
1504-
private void printWithQuotes(final Reader reader, final Appendable out, final boolean newRecord)
1505-
throws IOException {
1506-
final char quoteChar = getQuoteCharacter().charValue();
1503+
private void printWithQuotes(final Reader reader, final Appendable out) throws IOException {
15071504

15081505
if (getQuoteMode() == QuoteMode.NONE) {
15091506
printWithEscapes(reader, out);
15101507
return;
15111508
}
15121509

1513-
out.append(quoteChar);
1514-
IOUtils.copy(reader, out);
1515-
out.append(quoteChar);
1510+
int pos = 0;
1511+
1512+
final char quote = getQuoteCharacter().charValue();
1513+
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
1514+
1515+
out.append(quote);
1516+
1517+
int c;
1518+
while (-1 != (c = reader.read())) {
1519+
builder.append((char) c);
1520+
if (c == quote) {
1521+
// write out segment up until this char
1522+
if (pos > 0) {
1523+
out.append(builder.substring(0, pos));
1524+
builder.setLength(0);
1525+
pos = -1;
1526+
}
1527+
1528+
out.append(quote);
1529+
out.append((char) c);
1530+
}
1531+
pos++;
1532+
}
1533+
1534+
// write last segment
1535+
if (pos > 0) {
1536+
out.append(builder.substring(0, pos));
1537+
}
1538+
1539+
out.append(quote);
15161540
}
15171541

15181542
@Override

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@ private void setUpTable(final Connection connection) throws SQLException {
219219
try (final Statement statement = connection.createStatement()) {
220220
statement.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255), TEXT CLOB)");
221221
statement.execute("insert into TEST values(1, 'r1', 'long text 1')");
222-
longText2 = StringUtils.repeat('a', (IOUtils.DEFAULT_BUFFER_SIZE * 2) + 1);
222+
longText2 = StringUtils.repeat('a', IOUtils.DEFAULT_BUFFER_SIZE - 4);
223+
longText2 += "\"\r\n\"a\"";
224+
longText2 += StringUtils.repeat('a', IOUtils.DEFAULT_BUFFER_SIZE - 1);
223225
statement.execute("insert into TEST values(2, 'r2', '" + longText2 + "')");
226+
longText2 = longText2.replace("\"","\"\"");
224227
}
225228
}
226229

0 commit comments

Comments
 (0)