|
31 | 31 | import java.util.Objects; |
32 | 32 | import java.util.stream.Stream; |
33 | 33 |
|
| 34 | +import org.apache.commons.io.function.IOStream; |
| 35 | + |
34 | 36 | /** |
35 | 37 | * Prints values in a {@link CSVFormat CSV format}. |
36 | 38 | * |
|
70 | 72 | */ |
71 | 73 | public final class CSVPrinter implements Flushable, Closeable { |
72 | 74 |
|
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 | | - |
86 | 75 | /** The place that the values get written. */ |
87 | 76 | private final Appendable appendable; |
88 | 77 |
|
@@ -308,14 +297,9 @@ public void printRecord(final Object... values) throws IOException { |
308 | 297 | * If an I/O error occurs |
309 | 298 | * @since 1.10.0 |
310 | 299 | */ |
| 300 | + @SuppressWarnings("resource") // caller closes. |
311 | 301 | 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); |
319 | 303 | println(); |
320 | 304 | } |
321 | 305 |
|
@@ -496,14 +480,8 @@ public void printRecords(final ResultSet resultSet, final boolean printHeader) t |
496 | 480 | * If an I/O error occurs |
497 | 481 | * @since 1.10.0 |
498 | 482 | */ |
499 | | - @SuppressWarnings("unused") // rethrow() throws IOException |
| 483 | + @SuppressWarnings({ "resource" }) // Caller closes. |
500 | 484 | 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); |
508 | 486 | } |
509 | 487 | } |
0 commit comments