This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git
The following commit(s) were added to refs/heads/master by this push:
new 44b5e6c4 org.apache.commons.csv.CSVPrinter.printRecords(ResultSet) now
writes one record at a time using a lock
44b5e6c4 is described below
commit 44b5e6c4c178d1325f2df67467ba6248411f3d9d
Author: Gary Gregory <[email protected]>
AuthorDate: Wed May 7 07:56:21 2025 -0400
org.apache.commons.csv.CSVPrinter.printRecords(ResultSet) now writes one
record at a time using a lock
---
pom.xml | 6 ++++
src/changes/changes.xml | 1 +
.../java/org/apache/commons/csv/CSVPrinter.java | 41 ++++++++++++----------
3 files changed, 29 insertions(+), 19 deletions(-)
diff --git a/pom.xml b/pom.xml
index 109b1eee..785a4e96 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,6 +68,12 @@
<version>${commons.jmh.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.github.spotbugs</groupId>
+ <artifactId>spotbugs-annotations</artifactId>
+ <version>${commons.spotbugs.impl.version}</version>
+ <optional>true</optional>
+ </dependency>
</dependencies>
<scm>
<connection>scm:git:http://gitbox.apache.org/repos/asf/commons-csv.git</connection>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 752037d3..42d62262 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,7 @@
<!-- FIX -->
<action type="fix" issue="CSV-318" dev="ggregory" due-to="Joseph
Shraibman, Gary Gregory">CSVPrinter.printRecord(Stream) hangs if given a
parallel stream.</action>
<action type="fix" issue="CSV-318" dev="ggregory" due-to="Joseph
Shraibman, Gary Gregory">CSVPrinter now uses an internal lock instead of
synchronized methods.</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">org.apache.commons.csv.CSVPrinter.printRecords(ResultSet) now writes
one record at a time using a lock.</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump
commons-io:commons-io from 2.18.0 to 2.19.0.</action>
diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java
b/src/main/java/org/apache/commons/csv/CSVPrinter.java
index e7f6dab5..68d8d40e 100644
--- a/src/main/java/org/apache/commons/csv/CSVPrinter.java
+++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java
@@ -40,6 +40,8 @@ import java.util.stream.Stream;
import org.apache.commons.io.function.IOStream;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
/**
* Prints values in a {@link CSVFormat CSV format}.
*
@@ -151,14 +153,10 @@ public final class CSVPrinter implements Flushable,
Closeable {
* @throws IOException
* If an I/O error occurs
*/
+ @SuppressFBWarnings(value = "AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE",
justification = "https://github.com/spotbugs/spotbugs/issues/3428")
private void endOfRecord() throws IOException {
- lock.lock();
- try {
- println();
- recordCount++;
- } finally {
- lock.unlock();
- }
+ println();
+ recordCount++;
}
/**
@@ -494,21 +492,26 @@ public final class CSVPrinter implements Flushable,
Closeable {
public void printRecords(final ResultSet resultSet) throws SQLException,
IOException {
final int columnCount = resultSet.getMetaData().getColumnCount();
while (resultSet.next() && format.useRow(resultSet.getRow())) {
- for (int i = 1; i <= columnCount; i++) {
- final Object object = resultSet.getObject(i);
- if (object instanceof Clob) {
- try (Reader reader = ((Clob) object).getCharacterStream())
{
- print(reader);
+ lock.lock();
+ try {
+ for (int i = 1; i <= columnCount; i++) {
+ final Object object = resultSet.getObject(i);
+ if (object instanceof Clob) {
+ try (Reader reader = ((Clob)
object).getCharacterStream()) {
+ print(reader);
+ }
+ } else if (object instanceof Blob) {
+ try (InputStream inputStream = ((Blob)
object).getBinaryStream()) {
+ print(inputStream);
+ }
+ } else {
+ print(object);
}
- } else if (object instanceof Blob) {
- try (InputStream inputStream = ((Blob)
object).getBinaryStream()) {
- print(inputStream);
- }
- } else {
- print(object);
}
+ endOfRecord();
+ } finally {
+ lock.unlock();
}
- endOfRecord();
}
}