Skip to content

Commit 138a0b5

Browse files
committed
CSVPrinter now uses an internal lock instead of synchronized methods
1 parent 241a9e4 commit 138a0b5

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,25 @@ public long getRecordCount() {
204204
public void print(final Object value) throws IOException {
205205
lock.lock();
206206
try {
207-
format.print(value, appendable, newRecord);
208-
newRecord = false;
207+
printRaw(value);
209208
} finally {
210209
lock.unlock();
211210
}
212211
}
213212

213+
/**
214+
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
215+
*
216+
* @param value
217+
* value to be output.
218+
* @throws IOException
219+
* If an I/O error occurs
220+
*/
221+
private void printRaw(final Object value) throws IOException {
222+
format.print(value, appendable, newRecord);
223+
newRecord = false;
224+
}
225+
214226
/**
215227
* Prints a comment on a new line among the delimiter-separated values.
216228
*
@@ -352,17 +364,17 @@ public void printRecord(final Object... values) throws IOException {
352364
* separator to the output after printing the record, so there is no need to call {@link #println()}.
353365
* </p>
354366
*
355-
* @param values
367+
* @param stream
356368
* values to output.
357369
* @throws IOException
358370
* If an I/O error occurs
359371
* @since 1.10.0
360372
*/
361373
@SuppressWarnings("resource") // caller closes.
362-
public void printRecord(final Stream<?> values) throws IOException {
374+
public void printRecord(final Stream<?> stream) throws IOException {
363375
lock.lock();
364376
try {
365-
IOStream.adapt(values).forEachOrdered(this::print);
377+
IOStream.adapt(stream).forEachOrdered(stream.isParallel() ? this::printRaw : this::print);
366378
endOfRecord();
367379
} finally {
368380
lock.unlock();

0 commit comments

Comments
 (0)