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
commit 9c17a19b775cdb60d3417cb4af74e046c85c39a1 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Sep 30 10:59:40 2023 -0400 Reuse Commons IO --- .../java/org/apache/commons/csv/CSVFormat.java | 8 +++-- .../java/org/apache/commons/csv/CSVPrinter.java | 34 ++++------------------ 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index c89d6d4e..6c504bc9 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -49,6 +49,7 @@ import java.util.Objects; import java.util.Set; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.function.Uncheck; /** * Specifies the format of a CSV file for parsing and writing. @@ -1577,15 +1578,16 @@ public final class CSVFormat implements Serializable { * @return the formatted values */ public String format(final Object... values) { + return Uncheck.get(() -> format_(values)); + } + + private String format_(final Object... values) throws IOException { final StringWriter out = new StringWriter(); try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) { csvPrinter.printRecord(values); final String res = out.toString(); final int len = recordSeparator != null ? res.length() - recordSeparator.length() : res.length(); return res.substring(0, len); - } catch (final IOException e) { - // should not happen because a StringWriter does not do IO. - throw new IllegalStateException(e); } } diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index 39ddb51d..6d8f8af8 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -31,6 +31,8 @@ import java.util.Arrays; import java.util.Objects; import java.util.stream.Stream; +import org.apache.commons.io.function.IOStream; + /** * Prints values in a {@link CSVFormat CSV format}. * @@ -70,19 +72,6 @@ import java.util.stream.Stream; */ public final class CSVPrinter implements Flushable, Closeable { - /** - * Throws the given throwable. - * - * @param <T> The throwable cast type. - * @param throwable The throwable to rethrow. - * @return nothing because we throw. - * @throws T Always thrown. - */ - @SuppressWarnings("unchecked") - private static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T { - throw (T) throwable; - } - /** The place that the values get written. */ private final Appendable appendable; @@ -308,14 +297,9 @@ public final class CSVPrinter implements Flushable, Closeable { * If an I/O error occurs * @since 1.10.0 */ + @SuppressWarnings("resource") // caller closes. public synchronized void printRecord(final Stream<?> values) throws IOException { - values.forEachOrdered(t -> { - try { - print(t); - } catch (final IOException e) { - throw rethrow(e); - } - }); + IOStream.adapt(values).forEachOrdered(this::print); println(); } @@ -496,14 +480,8 @@ public final class CSVPrinter implements Flushable, Closeable { * If an I/O error occurs * @since 1.10.0 */ - @SuppressWarnings("unused") // rethrow() throws IOException + @SuppressWarnings({ "resource" }) // Caller closes. public void printRecords(final Stream<?> values) throws IOException { - values.forEachOrdered(t -> { - try { - printRecordObject(t); - } catch (final IOException e) { - throw rethrow(e); - } - }); + IOStream.adapt(values).forEachOrdered(this::printRecordObject); } }