Skip to content

Commit 229b3b7

Browse files
committed
org.apache.commons.csv.CSVParser.parse(File, Charset, CSVFormat) is now like org.apache.commons.csv.CSVParser.parse(URL, Charset, CSVFormat): You must pass in a Charset.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1602901 13f79535-47bb-0310-9956-ffa450edef68
1 parent 49579ec commit 229b3b7

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
package org.apache.commons.csv;
1919

20-
import static org.apache.commons.csv.Token.Type.TOKEN;
21-
2220
import java.io.Closeable;
2321
import java.io.File;
22+
import java.io.FileInputStream;
2423
import java.io.FileReader;
2524
import java.io.IOException;
2625
import java.io.InputStreamReader;
@@ -37,6 +36,8 @@
3736
import java.util.Map;
3837
import java.util.NoSuchElementException;
3938

39+
import static org.apache.commons.csv.Token.Type.*;
40+
4041
/**
4142
* Parses CSV files according to the specified format.
4243
*
@@ -50,7 +51,7 @@
5051
* There are several static factory methods that can be used to create instances for various types of resources:
5152
* </p>
5253
* <ul>
53-
* <li>{@link #parse(java.io.File, CSVFormat)}</li>
54+
* <li>{@link #parse(java.io.File, Charset, CSVFormat)}</li>
5455
* <li>{@link #parse(String, CSVFormat)}</li>
5556
* <li>{@link #parse(java.net.URL, java.nio.charset.Charset, CSVFormat)}</li>
5657
* </ul>
@@ -142,6 +143,8 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
142143
*
143144
* @param file
144145
* a CSV file. Must not be null.
146+
* @param charset
147+
* A charset
145148
* @param format
146149
* the CSVFormat used for CSV parsing. Must not be null.
147150
* @return a new parser
@@ -150,11 +153,11 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
150153
* @throws IOException
151154
* If an I/O error occurs
152155
*/
153-
public static CSVParser parse(final File file, final CSVFormat format) throws IOException {
156+
public static CSVParser parse(final File file, Charset charset, final CSVFormat format) throws IOException {
154157
Assertions.notNull(file, "file");
155158
Assertions.notNull(format, "format");
156-
157-
return new CSVParser(new FileReader(file), format);
159+
// Use the default Charset explicitly
160+
return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format);
158161
}
159162

160163
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void testCSVFile() throws Exception {
112112

113113
// Now parse the file and compare against the expected results
114114
// We use a buffered reader internally so no need to create one here.
115-
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), format);
115+
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format);
116116
for(final CSVRecord record : parser) {
117117
String parsed = record.toString();
118118
if (checkComments) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,12 @@ public void testNoHeaderMap() throws Exception {
806806

807807
@Test(expected = IllegalArgumentException.class)
808808
public void testParseFileNullFormat() throws Exception {
809-
CSVParser.parse(new File(""), null);
809+
CSVParser.parse(new File(""), Charset.defaultCharset(), null);
810810
}
811811

812812
@Test(expected = IllegalArgumentException.class)
813813
public void testParseNullFileFormat() throws Exception {
814-
CSVParser.parse((File) null, CSVFormat.DEFAULT);
814+
CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT);
815815
}
816816

817817
@Test(expected = IllegalArgumentException.class)
@@ -821,7 +821,7 @@ public void testParseNullStringFormat() throws Exception {
821821

822822
@Test(expected = IllegalArgumentException.class)
823823
public void testParseNullUrlCharsetFormat() throws Exception {
824-
CSVParser.parse(null, Charset.defaultCharset(), CSVFormat.DEFAULT);
824+
CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT);
825825
}
826826

827827
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)