Skip to content

Commit 9c17a19

Browse files
committed
Reuse Commons IO
1 parent 04b2773 commit 9c17a19

2 files changed

Lines changed: 11 additions & 31 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Set;
5050

5151
import org.apache.commons.io.IOUtils;
52+
import org.apache.commons.io.function.Uncheck;
5253

5354
/**
5455
* Specifies the format of a CSV file for parsing and writing.
@@ -1577,15 +1578,16 @@ public boolean equals(final Object obj) {
15771578
* @return the formatted values
15781579
*/
15791580
public String format(final Object... values) {
1581+
return Uncheck.get(() -> format_(values));
1582+
}
1583+
1584+
private String format_(final Object... values) throws IOException {
15801585
final StringWriter out = new StringWriter();
15811586
try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
15821587
csvPrinter.printRecord(values);
15831588
final String res = out.toString();
15841589
final int len = recordSeparator != null ? res.length() - recordSeparator.length() : res.length();
15851590
return res.substring(0, len);
1586-
} catch (final IOException e) {
1587-
// should not happen because a StringWriter does not do IO.
1588-
throw new IllegalStateException(e);
15891591
}
15901592
}
15911593

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

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.Objects;
3232
import java.util.stream.Stream;
3333

34+
import org.apache.commons.io.function.IOStream;
35+
3436
/**
3537
* Prints values in a {@link CSVFormat CSV format}.
3638
*
@@ -70,19 +72,6 @@
7072
*/
7173
public final class CSVPrinter implements Flushable, Closeable {
7274

73-
/**
74-
* Throws the given throwable.
75-
*
76-
* @param <T> The throwable cast type.
77-
* @param throwable The throwable to rethrow.
78-
* @return nothing because we throw.
79-
* @throws T Always thrown.
80-
*/
81-
@SuppressWarnings("unchecked")
82-
private static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
83-
throw (T) throwable;
84-
}
85-
8675
/** The place that the values get written. */
8776
private final Appendable appendable;
8877

@@ -308,14 +297,9 @@ public void printRecord(final Object... values) throws IOException {
308297
* If an I/O error occurs
309298
* @since 1.10.0
310299
*/
300+
@SuppressWarnings("resource") // caller closes.
311301
public synchronized void printRecord(final Stream<?> values) throws IOException {
312-
values.forEachOrdered(t -> {
313-
try {
314-
print(t);
315-
} catch (final IOException e) {
316-
throw rethrow(e);
317-
}
318-
});
302+
IOStream.adapt(values).forEachOrdered(this::print);
319303
println();
320304
}
321305

@@ -496,14 +480,8 @@ public void printRecords(final ResultSet resultSet, final boolean printHeader) t
496480
* If an I/O error occurs
497481
* @since 1.10.0
498482
*/
499-
@SuppressWarnings("unused") // rethrow() throws IOException
483+
@SuppressWarnings({ "resource" }) // Caller closes.
500484
public void printRecords(final Stream<?> values) throws IOException {
501-
values.forEachOrdered(t -> {
502-
try {
503-
printRecordObject(t);
504-
} catch (final IOException e) {
505-
throw rethrow(e);
506-
}
507-
});
485+
IOStream.adapt(values).forEachOrdered(this::printRecordObject);
508486
}
509487
}

0 commit comments

Comments
 (0)