You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/09/12 17:53:14 UTC

[commons-csv] branch master updated: CSVParser.getRecords() now throws UncheckedIOException instead of IOException

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 7e648a6e CSVParser.getRecords() now throws UncheckedIOException instead of IOException
7e648a6e is described below

commit 7e648a6e86dc35ccb6a29e9068341e54dd9e0fc8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Sep 12 10:53:09 2022 -0700

    CSVParser.getRecords() now throws UncheckedIOException instead of
    IOException
---
 src/changes/changes.xml                                 |  3 ++-
 src/main/java/org/apache/commons/csv/CSVParser.java     | 16 ++++++----------
 src/main/java/org/apache/commons/csv/CSVPrinter.java    |  1 +
 src/test/java/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 @@
       <action issue="CSV-288" type="fix" dev="ggregory" due-to="Santhsoh, Angus">Fix for multi-char delimiter not working as expected #218.</action>
       <action issue="CSV-269" type="fix" dev="ggregory" due-to="Auke te Winkel, Gary Gregory">CSVRecord.get(Enum) should use Enum.name() instead of Enum.toString().</action>
       <action                 type="fix" dev="ggregory" due-to="Gary Gregory">Allow org.apache.commons.csv.IOUtils.copy(Reader, Appendable, CharBuffer) to compile on Java 11 and run on Java 8.</action>
-      <action                 type="fix" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 52 to 53.</action>
       <action issue="CSV-300" type="fix" dev="ggregory" due-to="Markus Spann, Gary Gregory">CSVRecord.toList() does not give write access to the new List.</action>
+      <action                 type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.getRecords() now throws UncheckedIOException instead of IOException.</action>
       <!-- ADD -->
       <action issue="CSV-291" type="add" dev="ggregory" due-to="Gary Gregory">Make CSVRecord#values() public.</action>
       <action issue="CSV-264" type="add" dev="ggregory" due-to="Sagar Tiwari, Seth Falco, Alex Herbert, Gary Gregory">Add DuplicateHeaderMode for flexibility with header strictness. #114.</action>
@@ -55,6 +55,7 @@
       <action issue="CSV-304" type="add" dev="ggregory" due-to="Peter Hull, Bruno P. Kinoshita, Gary Gregory">Add accessors for header/trailer comments #257.</action>
       <action type="add" dev="ggregory">Add github/codeql-action.</action>
       <!-- UPDATE -->
+      <action                 type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 52 to 53.</action>
       <action                 type="update" dev="kinow" due-to="Dependabot, Gary Gregory">Bump actions/cache from 2.1.6 to 3.0.8 #196, #233, #243.</action>
       <action                 type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2.3.4 to 3.0.2 #188, #195, #220.</action>
       <action                 type="update" dev="ggregory" due-to="Gary Gregory">Bump actions/setup-java from 2 to 3.</action>
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<CSVRecord>, 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<CSVRecord>, Closeable {
      * </p>
      *
      * @return list of {@link CSVRecord CSVRecords}, may be empty
-     * @throws IOException
+     * @throws UncheckedIOException
      *             on parse error or input read-failure
      */
-    public List<CSVRecord> getRecords() throws IOException {
-        CSVRecord rec;
-        final List<CSVRecord> records = new ArrayList<>();
-        while ((rec = this.nextRecord()) != null) {
-            records.add(rec);
-        }
-        return records;
+    public List<CSVRecord> 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 <T extends Throwable> 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);
 
     }