Skip to content

Commit 7e648a6

Browse files
committed
CSVParser.getRecords() now throws UncheckedIOException instead of
IOException
1 parent 573eab7 commit 7e648a6

4 files changed

Lines changed: 13 additions & 16 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
<action issue="CSV-288" type="fix" dev="ggregory" due-to="Santhsoh, Angus">Fix for multi-char delimiter not working as expected #218.</action>
4646
<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>
4747
<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>
48-
<action type="fix" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 52 to 53.</action>
4948
<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>
49+
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.getRecords() now throws UncheckedIOException instead of IOException.</action>
5050
<!-- ADD -->
5151
<action issue="CSV-291" type="add" dev="ggregory" due-to="Gary Gregory">Make CSVRecord#values() public.</action>
5252
<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 @@
5555
<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>
5656
<action type="add" dev="ggregory">Add github/codeql-action.</action>
5757
<!-- UPDATE -->
58+
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 52 to 53.</action>
5859
<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>
5960
<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>
6061
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump actions/setup-java from 2 to 3.</action>

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.InputStreamReader;
2727
import java.io.Reader;
2828
import java.io.StringReader;
29+
import java.io.UncheckedIOException;
2930
import java.net.URL;
3031
import java.nio.charset.Charset;
3132
import java.nio.file.Files;
@@ -42,6 +43,7 @@
4243
import java.util.Spliterator;
4344
import java.util.Spliterators;
4445
import java.util.TreeMap;
46+
import java.util.stream.Collectors;
4547
import java.util.stream.Stream;
4648
import java.util.stream.StreamSupport;
4749

@@ -145,8 +147,7 @@ private CSVRecord getNextRecord() {
145147
try {
146148
return CSVParser.this.nextRecord();
147149
} catch (final IOException e) {
148-
throw new IllegalStateException(
149-
e.getClass().getSimpleName() + " reading next record: " + e.toString(), e);
150+
throw new UncheckedIOException(e.getClass().getSimpleName() + " reading next record: " + e.toString(), e);
150151
}
151152
}
152153

@@ -639,16 +640,11 @@ public long getRecordNumber() {
639640
* </p>
640641
*
641642
* @return list of {@link CSVRecord CSVRecords}, may be empty
642-
* @throws IOException
643+
* @throws UncheckedIOException
643644
* on parse error or input read-failure
644645
*/
645-
public List<CSVRecord> getRecords() throws IOException {
646-
CSVRecord rec;
647-
final List<CSVRecord> records = new ArrayList<>();
648-
while ((rec = this.nextRecord()) != null) {
649-
records.add(rec);
650-
}
651-
return records;
646+
public List<CSVRecord> getRecords() {
647+
return stream().collect(Collectors.toList());
652648
}
653649

654650
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public final class CSVPrinter implements Flushable, Closeable {
8282
private static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
8383
throw (T) throwable;
8484
}
85+
8586
/** The place that the values get written. */
8687
private final Appendable appendable;
8788

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.Reader;
3737
import java.io.StringReader;
3838
import java.io.StringWriter;
39+
import java.io.UncheckedIOException;
3940
import java.net.URL;
4041
import java.nio.charset.Charset;
4142
import java.nio.charset.StandardCharsets;
@@ -106,9 +107,9 @@ public class CSVParserTest {
106107
.setHeader("A", "B")
107108
.build();
108109

110+
@SuppressWarnings("resource") // caller releases
109111
private BOMInputStream createBOMInputStream(final String resource) throws IOException {
110-
final URL url = ClassLoader.getSystemClassLoader().getResource(resource);
111-
return new BOMInputStream(url.openStream());
112+
return new BOMInputStream(ClassLoader.getSystemClassLoader().getResource(resource).openStream());
112113
}
113114

114115
private void parseFully(final CSVParser parser) {
@@ -466,8 +467,6 @@ public void testExcelFormat2() throws Exception {
466467

467468
/**
468469
* Tests an exported Excel worksheet with a header row and rows that have more columns than the headers
469-
*
470-
* @throws Exception
471470
*/
472471
@Test
473472
public void testExcelHeaderCountLessThanData() throws Exception {
@@ -737,7 +736,7 @@ public void testGetRecords() throws IOException {
737736
public void testGetRecordsFromBrokenInputStream() throws IOException {
738737
@SuppressWarnings("resource") // We also get an exception on close, which is OK but can't assert in a try.
739738
final CSVParser parser = CSVParser.parse(new BrokenInputStream(), UTF_8, CSVFormat.DEFAULT);
740-
assertThrows(IOException.class, parser::getRecords);
739+
assertThrows(UncheckedIOException.class, parser::getRecords);
741740

742741
}
743742

0 commit comments

Comments
 (0)