Skip to content
Closed
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
40 changes: 32 additions & 8 deletions src/main/java/org/apache/commons/csv/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,7 @@ private void print(final Reader reader, final Appendable out, final boolean newR
out.append(getDelimiter());
}
if (isQuoteCharacterSet()) {
// the original object is needed so can check for Number
printWithQuotes(reader, out, newRecord);
printWithQuotes(reader, out);
} else if (isEscapeCharacterSet()) {
printWithEscapes(reader, out);
} else if (out instanceof Writer) {
Expand Down Expand Up @@ -1501,18 +1500,43 @@ private void printWithQuotes(final Object object, final CharSequence value, fina
*
* @throws IOException
*/
private void printWithQuotes(final Reader reader, final Appendable out, final boolean newRecord)
throws IOException {
final char quoteChar = getQuoteCharacter().charValue();
private void printWithQuotes(final Reader reader, final Appendable out) throws IOException {

if (getQuoteMode() == QuoteMode.NONE) {
printWithEscapes(reader, out);
return;
}

out.append(quoteChar);
IOUtils.copy(reader, out);
out.append(quoteChar);
int pos = 0;

final char quote = getQuoteCharacter().charValue();
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);

out.append(quote);

int c;
while (-1 != (c = reader.read())) {
builder.append((char) c);
if (c == quote) {
// write out segment up until this char
if (pos > 0) {
out.append(builder.substring(0, pos));
builder.setLength(0);
pos = -1;
}

out.append(quote);
out.append((char) c);
}
pos++;
}

// write last segment
if (pos > 0) {
out.append(builder.substring(0, pos));
}

out.append(quote);
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ private void setUpTable(final Connection connection) throws SQLException {
try (final Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255), TEXT CLOB)");
statement.execute("insert into TEST values(1, 'r1', 'long text 1')");
longText2 = StringUtils.repeat('a', (IOUtils.DEFAULT_BUFFER_SIZE * 2) + 1);
longText2 = StringUtils.repeat('a', IOUtils.DEFAULT_BUFFER_SIZE - 4);
longText2 += "\"\r\n\"a\"";
longText2 += StringUtils.repeat('a', IOUtils.DEFAULT_BUFFER_SIZE - 1);
statement.execute("insert into TEST values(2, 'r2', '" + longText2 + "')");
longText2 = longText2.replace("\"","\"\"");
}
}

Expand Down