[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.
This commit is contained in:
Gary Gregory 2016-07-30 16:24:12 -07:00
parent 6780f0d96c
commit 86ef75f808
2 changed files with 10 additions and 5 deletions

View File

@ -188,7 +188,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
* @since 1.5
*/
@SuppressWarnings("resource")
public static CSVParser parse(final InputStream inputStream, final String charset, final CSVFormat format) throws IOException {
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format) throws IOException {
Assertions.notNull(inputStream, "inputStream");
Assertions.notNull(format, "format");
return parse(new InputStreamReader(inputStream, charset), format);

View File

@ -38,6 +38,7 @@ import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -60,6 +61,10 @@ import org.junit.Test;
*/
public class CSVParserTest {
private static final Charset UTF_8 = StandardCharsets.UTF_8;
private static final String UTF_8_NAME = UTF_8.name();
private static final String CSV_INPUT = "a,b,c,d\n" + " a , b , 1 2 \n" + "\"foo baar\", b,\n"
// + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
+ " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
@ -167,7 +172,7 @@ public class CSVParserTest {
@Ignore("CSV-107")
public void testBOM() throws IOException {
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
try (final CSVParser parser = CSVParser.parse(url, Charset.forName("UTF-8"), CSVFormat.EXCEL.withHeader())) {
try (final CSVParser parser = CSVParser.parse(url, Charset.forName(UTF_8_NAME), CSVFormat.EXCEL.withHeader())) {
for (final CSVRecord record : parser) {
final String string = record.get("Date");
Assert.assertNotNull(string);
@ -178,7 +183,7 @@ public class CSVParserTest {
@Test
public void testBOMInputStream_ParserWithReader() throws IOException {
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), "UTF-8");
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), UTF_8_NAME);
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) {
for (final CSVRecord record : parser) {
final String string = record.get("Date");
@ -190,7 +195,7 @@ public class CSVParserTest {
@Test
public void testBOMInputStream_parseWithReader() throws IOException {
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), "UTF-8");
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), UTF_8_NAME);
final CSVParser parser = CSVParser.parse(reader, CSVFormat.EXCEL.withHeader())) {
for (final CSVRecord record : parser) {
final String string = record.get("Date");
@ -203,7 +208,7 @@ public class CSVParserTest {
@Test
public void testBOMInputStream_ParserWithInputStream() throws IOException {
try (final BOMInputStream inputStream = createBOMInputStream("CSVFileParser/bom.csv");
final CSVParser parser = CSVParser.parse(inputStream, "UTF-8", CSVFormat.EXCEL.withHeader())) {
final CSVParser parser = CSVParser.parse(inputStream, UTF_8, CSVFormat.EXCEL.withHeader())) {
for (final CSVRecord record : parser) {
final String string = record.get("Date");
Assert.assertNotNull(string);