From 7e648a6e86dc35ccb6a29e9068341e54dd9e0fc8 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 12 Sep 2022 10:53:09 -0700 Subject: [PATCH] CSVParser.getRecords() now throws UncheckedIOException instead of IOException --- src/changes/changes.xml | 3 ++- .../java/org/apache/commons/csv/CSVParser.java | 16 ++++++---------- .../java/org/apache/commons/csv/CSVPrinter.java | 1 + .../org/apache/commons/csv/CSVParserTest.java | 9 ++++----- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a2c56117..b3311e21 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -45,8 +45,8 @@ Fix for multi-char delimiter not working as expected #218. CSVRecord.get(Enum) should use Enum.name() instead of Enum.toString(). Allow org.apache.commons.csv.IOUtils.copy(Reader, Appendable, CharBuffer) to compile on Java 11 and run on Java 8. - Bump commons-parent from 52 to 53. CSVRecord.toList() does not give write access to the new List. + CSVParser.getRecords() now throws UncheckedIOException instead of IOException. Make CSVRecord#values() public. Add DuplicateHeaderMode for flexibility with header strictness. #114. @@ -55,6 +55,7 @@ Add accessors for header/trailer comments #257. Add github/codeql-action. + Bump commons-parent from 52 to 53. Bump actions/cache from 2.1.6 to 3.0.8 #196, #233, #243. Bump actions/checkout from 2.3.4 to 3.0.2 #188, #195, #220. Bump actions/setup-java from 2 to 3. diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index ad428d2f..bff55a00 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.io.UncheckedIOException; import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Files; @@ -42,6 +43,7 @@ import java.util.Objects; import java.util.Spliterator; import java.util.Spliterators; import java.util.TreeMap; +import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -145,8 +147,7 @@ public final class CSVParser implements Iterable, Closeable { try { return CSVParser.this.nextRecord(); } catch (final IOException e) { - throw new IllegalStateException( - e.getClass().getSimpleName() + " reading next record: " + e.toString(), e); + throw new UncheckedIOException(e.getClass().getSimpleName() + " reading next record: " + e.toString(), e); } } @@ -639,16 +640,11 @@ public final class CSVParser implements Iterable, Closeable { *

* * @return list of {@link CSVRecord CSVRecords}, may be empty - * @throws IOException + * @throws UncheckedIOException * on parse error or input read-failure */ - public List getRecords() throws IOException { - CSVRecord rec; - final List records = new ArrayList<>(); - while ((rec = this.nextRecord()) != null) { - records.add(rec); - } - return records; + public List getRecords() { + return stream().collect(Collectors.toList()); } /** diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index a7c62da1..89a90e3a 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -82,6 +82,7 @@ public final class CSVPrinter implements Flushable, Closeable { private static RuntimeException rethrow(final Throwable throwable) throws T { throw (T) throwable; } + /** The place that the values get written. */ private final Appendable appendable; diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 25855ca3..07616800 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -36,6 +36,7 @@ import java.io.PipedWriter; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; +import java.io.UncheckedIOException; import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -106,9 +107,9 @@ public class CSVParserTest { .setHeader("A", "B") .build(); + @SuppressWarnings("resource") // caller releases private BOMInputStream createBOMInputStream(final String resource) throws IOException { - final URL url = ClassLoader.getSystemClassLoader().getResource(resource); - return new BOMInputStream(url.openStream()); + return new BOMInputStream(ClassLoader.getSystemClassLoader().getResource(resource).openStream()); } private void parseFully(final CSVParser parser) { @@ -466,8 +467,6 @@ public class CSVParserTest { /** * Tests an exported Excel worksheet with a header row and rows that have more columns than the headers - * - * @throws Exception */ @Test public void testExcelHeaderCountLessThanData() throws Exception { @@ -737,7 +736,7 @@ public class CSVParserTest { public void testGetRecordsFromBrokenInputStream() throws IOException { @SuppressWarnings("resource") // We also get an exception on close, which is OK but can't assert in a try. final CSVParser parser = CSVParser.parse(new BrokenInputStream(), UTF_8, CSVFormat.DEFAULT); - assertThrows(IOException.class, parser::getRecords); + assertThrows(UncheckedIOException.class, parser::getRecords); }