CSVParser.getRecords() now throws UncheckedIOException instead of

IOException
This commit is contained in:
Gary Gregory 2022-09-12 10:53:09 -07:00
parent 573eab776c
commit 7e648a6e86
4 changed files with 13 additions and 16 deletions

View File

@ -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>

View File

@ -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());
}
/**

View File

@ -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;

View File

@ -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);
}