From 229b3b7691f06577c473a287b29b18c8a9398ce8 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Mon, 16 Jun 2014 15:39:52 +0000 Subject: [PATCH] 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 --- .../java/org/apache/commons/csv/CSVParser.java | 15 +++++++++------ .../org/apache/commons/csv/CSVFileParserTest.java | 2 +- .../org/apache/commons/csv/CSVParserTest.java | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index c2965920..d53a08fa 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -17,10 +17,9 @@ package org.apache.commons.csv; -import static org.apache.commons.csv.Token.Type.TOKEN; - import java.io.Closeable; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; @@ -37,6 +36,8 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import static org.apache.commons.csv.Token.Type.*; + /** * Parses CSV files according to the specified format. * @@ -50,7 +51,7 @@ import java.util.NoSuchElementException; * There are several static factory methods that can be used to create instances for various types of resources: *

* @@ -142,6 +143,8 @@ public final class CSVParser implements Iterable, Closeable { * * @param file * a CSV file. Must not be null. + * @param charset + * A charset * @param format * the CSVFormat used for CSV parsing. Must not be null. * @return a new parser @@ -150,11 +153,11 @@ public final class CSVParser implements Iterable, Closeable { * @throws IOException * If an I/O error occurs */ - public static CSVParser parse(final File file, final CSVFormat format) throws IOException { + public static CSVParser parse(final File file, Charset charset, final CSVFormat format) throws IOException { Assertions.notNull(file, "file"); Assertions.notNull(format, "format"); - - return new CSVParser(new FileReader(file), format); + // Use the default Charset explicitly + return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format); } /** diff --git a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java index e7c12e23..3251072c 100644 --- a/src/test/java/org/apache/commons/csv/CSVFileParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFileParserTest.java @@ -112,7 +112,7 @@ public class CSVFileParserTest { // Now parse the file and compare against the expected results // We use a buffered reader internally so no need to create one here. - final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), format); + final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format); for(final CSVRecord record : parser) { String parsed = record.toString(); if (checkComments) { diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 1fc58670..46a8ce46 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -806,12 +806,12 @@ public class CSVParserTest { @Test(expected = IllegalArgumentException.class) public void testParseFileNullFormat() throws Exception { - CSVParser.parse(new File(""), null); + CSVParser.parse(new File(""), Charset.defaultCharset(), null); } @Test(expected = IllegalArgumentException.class) public void testParseNullFileFormat() throws Exception { - CSVParser.parse((File) null, CSVFormat.DEFAULT); + CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT); } @Test(expected = IllegalArgumentException.class) @@ -821,7 +821,7 @@ public class CSVParserTest { @Test(expected = IllegalArgumentException.class) public void testParseNullUrlCharsetFormat() throws Exception { - CSVParser.parse(null, Charset.defaultCharset(), CSVFormat.DEFAULT); + CSVParser.parse((File) null, Charset.defaultCharset(), CSVFormat.DEFAULT); } @Test(expected = IllegalArgumentException.class)