Skip to content

Commit 86ef75f

Browse files
committed
[CSV-192] Add convenience API CSVParser.parse(Path, Charset, CSVFormat).
Adjust API to be Charset-based instead of String (charset name), just like it says in the Jira title.
1 parent 6780f0d commit 86ef75f

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static CSVParser parse(final File file, final Charset charset, final CSVF
188188
* @since 1.5
189189
*/
190190
@SuppressWarnings("resource")
191-
public static CSVParser parse(final InputStream inputStream, final String charset, final CSVFormat format) throws IOException {
191+
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format) throws IOException {
192192
Assertions.notNull(inputStream, "inputStream");
193193
Assertions.notNull(format, "format");
194194
return parse(new InputStreamReader(inputStream, charset), format);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.io.StringWriter;
3939
import java.net.URL;
4040
import java.nio.charset.Charset;
41+
import java.nio.charset.StandardCharsets;
4142
import java.util.ArrayList;
4243
import java.util.Iterator;
4344
import java.util.List;
@@ -60,6 +61,10 @@
6061
*/
6162
public class CSVParserTest {
6263

64+
private static final Charset UTF_8 = StandardCharsets.UTF_8;
65+
66+
private static final String UTF_8_NAME = UTF_8.name();
67+
6368
private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n"
6469
// + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
6570
+ " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
@@ -167,7 +172,7 @@ public void testBackslashEscapingOld() throws IOException {
167172
@Ignore("CSV-107")
168173
public void testBOM() throws IOException {
169174
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
170-
try (final CSVParser parser = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.EXCEL.withHeader())) {
175+
try (final CSVParser parser = CSVParser.parse(url, Charset.forName(UTF_8_NAME), CSVFormat.EXCEL.withHeader())) {
171176
for (final CSVRecord record : parser) {
172177
final String string = record.get("Date");
173178
Assert.assertNotNull(string);
@@ -178,7 +183,7 @@ public void testBOM() throws IOException {
178183

179184
@Test
180185
public void testBOMInputStream_ParserWithReader() throws IOException {
181-
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), "UTF-8");
186+
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), UTF_8_NAME);
182187
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) {
183188
for (final CSVRecord record : parser) {
184189
final String string = record.get("Date");
@@ -190,7 +195,7 @@ public void testBOMInputStream_ParserWithReader() throws IOException {
190195

191196
@Test
192197
public void testBOMInputStream_parseWithReader() throws IOException {
193-
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), "UTF-8");
198+
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), UTF_8_NAME);
194199
final CSVParser parser = CSVParser.parse(reader, CSVFormat.EXCEL.withHeader())) {
195200
for (final CSVRecord record : parser) {
196201
final String string = record.get("Date");
@@ -203,7 +208,7 @@ public void testBOMInputStream_parseWithReader() throws IOException {
203208
@Test
204209
public void testBOMInputStream_ParserWithInputStream() throws IOException {
205210
try (final BOMInputStream inputStream = createBOMInputStream("CSVFileParser/bom.csv");
206-
final CSVParser parser = CSVParser.parse(inputStream, "UTF-8", CSVFormat.EXCEL.withHeader())) {
211+
final CSVParser parser = CSVParser.parse(inputStream, UTF_8, CSVFormat.EXCEL.withHeader())) {
207212
for (final CSVRecord record : parser) {
208213
final String string = record.get("Date");
209214
Assert.assertNotNull(string);

0 commit comments

Comments
 (0)