Skip to content

Commit d93c494

Browse files
committed
CSVParser.parse(*) methods with a null Charset maps to
Charset.defaultCharset() Javadoc
1 parent c36d6cd commit d93c494

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(File, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action>
5050
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(Path, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action>
5151
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(InputStream, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action>
52+
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(*) methods with a null Charset maps to Charset.defaultCharset().</action>
5253
<!-- ADD -->
5354
<action type="add" dev="ggregory" due-to="Gary Gregory">Define and use Maven property commons.jmh.version.</action>
5455
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.Builder.setMaxRows(long).</action>

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.stream.Stream;
5050
import java.util.stream.StreamSupport;
5151

52+
import org.apache.commons.io.Charsets;
5253
import org.apache.commons.io.build.AbstractStreamBuilder;
5354
import org.apache.commons.io.function.Uncheck;
5455

@@ -305,15 +306,16 @@ public static Builder builder() {
305306
* @param file
306307
* a CSV file. Must not be null.
307308
* @param charset
308-
* The Charset to decode the given file.
309+
* The Charset to decode the given file, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
309310
* @param format
310311
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
311312
* @return a new parser
312313
* @throws IllegalArgumentException
313-
* If the parameters of the format are inconsistent or if either file or format are null.
314+
* If the parameters of the format are inconsistent.
314315
* @throws IOException
315316
* If an I/O error occurs
316-
* @throws CSVException Thrown on invalid input.
317+
* @throws CSVException Thrown on invalid CSV input data.
318+
* @throws NullPointerException if {@code file} is {@code null}.
317319
*/
318320
public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException {
319321
Objects.requireNonNull(file, "file");
@@ -331,20 +333,20 @@ public static CSVParser parse(final File file, final Charset charset, final CSVF
331333
* @param inputStream
332334
* an InputStream containing CSV-formatted input, {@code null} maps to {@link CSVFormat#DEFAULT}.
333335
* @param charset
334-
* The Charset to decode the given file.
336+
* The Charset to decode the given file, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
335337
* @param format
336338
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
337339
* @return a new CSVParser configured with the given reader and format.
338340
* @throws IllegalArgumentException
339341
* If the parameters of the format are inconsistent or if either reader or format are null.
340342
* @throws IOException
341343
* If there is a problem reading the header or skipping the first record
342-
* @throws CSVException Thrown on invalid input.
344+
* @throws CSVException Thrown on invalid CSV input data.
343345
* @since 1.5
344346
*/
345347
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
346348
throws IOException {
347-
return parse(new InputStreamReader(inputStream, charset), format);
349+
return parse(new InputStreamReader(inputStream, Charsets.toCharset(charset)), format);
348350
}
349351

350352
/**
@@ -353,15 +355,16 @@ public static CSVParser parse(final InputStream inputStream, final Charset chars
353355
* @param path
354356
* a CSV file. Must not be null.
355357
* @param charset
356-
* The Charset to decode the given file.
358+
* The Charset to decode the given file, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
357359
* @param format
358360
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
359361
* @return a new parser
360362
* @throws IllegalArgumentException
361-
* If the parameters of the format are inconsistent or if either file or format are null.
363+
* If the parameters of the format are inconsistent.
362364
* @throws IOException
363365
* If an I/O error occurs
364-
* @throws CSVException Thrown on invalid input.
366+
* @throws CSVException Thrown on invalid CSV input data.
367+
* @throws NullPointerException if {@code path} is {@code null}.
365368
* @since 1.5
366369
*/
367370
@SuppressWarnings("resource")
@@ -387,7 +390,7 @@ public static CSVParser parse(final Path path, final Charset charset, final CSVF
387390
* If the parameters of the format are inconsistent or if either reader or format are null.
388391
* @throws IOException
389392
* If there is a problem reading the header or skipping the first record
390-
* @throws CSVException Thrown on invalid input.
393+
* @throws CSVException Thrown on invalid CSV input data.
391394
* @since 1.5
392395
*/
393396
public static CSVParser parse(final Reader reader, final CSVFormat format) throws IOException {
@@ -403,10 +406,11 @@ public static CSVParser parse(final Reader reader, final CSVFormat format) throw
403406
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
404407
* @return a new parser
405408
* @throws IllegalArgumentException
406-
* If the parameters of the format are inconsistent or if either string or format are null.
409+
* If the parameters of the format are inconsistent.
407410
* @throws IOException
408411
* If an I/O error occurs
409-
* @throws CSVException Thrown on invalid input.
412+
* @throws CSVException Thrown on invalid CSV input data.
413+
* @throws NullPointerException if {@code string} is {@code null}.
410414
*/
411415
public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
412416
Objects.requireNonNull(string, "string");
@@ -424,15 +428,16 @@ public static CSVParser parse(final String string, final CSVFormat format) throw
424428
* @param url
425429
* a URL. Must not be null.
426430
* @param charset
427-
* the charset for the resource. Must not be null.
431+
* the charset for the resource, {@code null} maps to the {@link Charset#defaultCharset() default Charset}.
428432
* @param format
429433
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
430434
* @return a new parser
431435
* @throws IllegalArgumentException
432-
* If the parameters of the format are inconsistent or if either url, charset or format are null.
436+
* If the parameters of the format are inconsistent.
433437
* @throws IOException
434438
* If an I/O error occurs
435-
* @throws CSVException Thrown on invalid input.
439+
* @throws CSVException Thrown on invalid CSV input data.
440+
* @throws NullPointerException if {@code url} is {@code null}.
436441
*/
437442
@SuppressWarnings("resource")
438443
public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException {
@@ -484,7 +489,7 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
484489
* If the parameters of the format are inconsistent or if either reader or format are null.
485490
* @throws IOException
486491
* If there is a problem reading the header or skipping the first record
487-
* @throws CSVException Thrown on invalid input.
492+
* @throws CSVException Thrown on invalid CSV input data.
488493
* @deprecated Will be removed in the next major version, use {@link Builder#get()}.
489494
*/
490495
@Deprecated
@@ -517,10 +522,9 @@ public CSVParser(final Reader reader, final CSVFormat format) throws IOException
517522
* @deprecated Will be private in the next major version, use {@link Builder#get()}.
518523
*/
519524
@Deprecated
520-
public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber)
521-
throws IOException {
522-
this(reader, format, characterOffset, recordNumber, null, false);
523-
}
525+
public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber) throws IOException {
526+
this(reader, format, characterOffset, recordNumber, null, false);
527+
}
524528

525529
/**
526530
* Constructs a new instance using the given {@link CSVFormat}
@@ -546,7 +550,7 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
546550
* If the parameters of the format are inconsistent or if either the reader or format is null.
547551
* @throws IOException
548552
* If there is a problem reading the header or skipping the first record.
549-
* @throws CSVException Thrown on invalid input.
553+
* @throws CSVException Thrown on invalid CSV input data.
550554
*/
551555
private CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber,
552556
final Charset charset, final boolean trackBytes)
@@ -875,7 +879,7 @@ public Iterator<CSVRecord> iterator() {
875879
*
876880
* @return the record as an array of values, or {@code null} if the end of the stream has been reached.
877881
* @throws IOException on parse error or input read-failure.
878-
* @throws CSVException on invalid input.
882+
* @throws CSVException on invalid CSV input data.
879883
*/
880884
CSVRecord nextRecord() throws IOException {
881885
CSVRecord result = null;

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,7 @@ public void testNotValueCSV() throws IOException {
13581358

13591359
@Test
13601360
public void testParse() throws Exception {
1361-
final ClassLoader loader = ClassLoader.getSystemClassLoader();
1362-
final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv");
1361+
final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/test.csv");
13631362
final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader("A", "B", "C", "D").get();
13641363
final Charset charset = StandardCharsets.UTF_8;
13651364
// Reader
@@ -1472,8 +1471,12 @@ public void testParsePathCharsetNullFormat() throws IOException {
14721471
}
14731472

14741473
@Test
1475-
public void testParserUrlNullCharsetFormat() {
1476-
assertThrows(NullPointerException.class, () -> CSVParser.parse(new URL("https://commons.apache.org"), null, CSVFormat.DEFAULT));
1474+
public void testParserUrlNullCharsetFormat() throws IOException {
1475+
final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/test.csv");
1476+
try (CSVParser parser = CSVParser.parse(url, null, CSVFormat.DEFAULT)) {
1477+
// null maps to DEFAULT.
1478+
parseFully(parser);
1479+
}
14771480
}
14781481

14791482
@Test
@@ -1492,8 +1495,7 @@ public void testParseStringNullFormat() throws IOException {
14921495

14931496
@Test
14941497
public void testParseUrlCharsetNullFormat() throws IOException {
1495-
final ClassLoader loader = ClassLoader.getSystemClassLoader();
1496-
final URL url = loader.getResource("org/apache/commons/csv/CSVFileParser/test.csv");
1498+
final URL url = ClassLoader.getSystemClassLoader().getResource("org/apache/commons/csv/CSVFileParser/test.csv");
14971499
try (CSVParser parser = CSVParser.parse(url, Charset.defaultCharset(), null)) {
14981500
// null maps to DEFAULT.
14991501
parseFully(parser);

0 commit comments

Comments
 (0)