Sure, or provide both? Gary
On Jul 30, 2016 11:13 AM, "Benedikt Ritter" <brit...@apache.org> wrote: > I think it would be better to use a Charset object as parameter instead of > a String. > > WDYT? > > <ggreg...@apache.org> schrieb am Sa., 30. Juli 2016 um 19:57: > > > Repository: commons-csv > > Updated Branches: > > refs/heads/master 0d7c984c6 -> ac46f73b2 > > > > > > [CSV-189] CSVParser: Add factory method accepting InputStream. > > > > Project: http://git-wip-us.apache.org/repos/asf/commons-csv/repo > > Commit: > http://git-wip-us.apache.org/repos/asf/commons-csv/commit/ac46f73b > > Tree: http://git-wip-us.apache.org/repos/asf/commons-csv/tree/ac46f73b > > Diff: http://git-wip-us.apache.org/repos/asf/commons-csv/diff/ac46f73b > > > > Branch: refs/heads/master > > Commit: ac46f73b259412122680f217020ad473dc6e8781 > > Parents: 0d7c984 > > Author: Gary Gregory <ggreg...@apache.org> > > Authored: Sat Jul 30 10:57:13 2016 -0700 > > Committer: Gary Gregory <ggreg...@apache.org> > > Committed: Sat Jul 30 10:57:13 2016 -0700 > > > > ---------------------------------------------------------------------- > > src/changes/changes.xml | 1 + > > .../java/org/apache/commons/csv/CSVParser.java | 62 > +++++++++++++++++++- > > .../org/apache/commons/csv/CSVParserTest.java | 34 ++++++++++- > > 3 files changed, 92 insertions(+), 5 deletions(-) > > ---------------------------------------------------------------------- > > > > > > > > > http://git-wip-us.apache.org/repos/asf/commons-csv/blob/ac46f73b/src/changes/changes.xml > > ---------------------------------------------------------------------- > > diff --git a/src/changes/changes.xml b/src/changes/changes.xml > > index 4960ea6..a8e9a75 100644 > > --- a/src/changes/changes.xml > > +++ b/src/changes/changes.xml > > @@ -40,6 +40,7 @@ > > <body> > > <release version="1.5" date="2016-MM-DD" description="Bug fix > > release"> > > <action issue="CSV-187" type="update" dev="ggregory" due-to="Gary > > Gregory">Update platform requirement from Java 6 to 7.</action> > > + <action issue="CSV-189" type="add" dev="ggregory" due-to="Peter > > Holzwarth, Gary Gregory">CSVParser: Add factory method accepting > > InputStream.</action> > > <action issue="CSV-???" type="add" dev="ggregory" due-to="Gary > > Gregory">Add convenience API CSVFormat.print(File, Charset)</action> > > <action issue="CSV-???" type="add" dev="ggregory" due-to="Gary > > Gregory">Add convenience API CSVFormat.print(Path, Charset)</action> > > </release> > > > > > > > http://git-wip-us.apache.org/repos/asf/commons-csv/blob/ac46f73b/src/main/java/org/apache/commons/csv/CSVParser.java > > ---------------------------------------------------------------------- > > diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java > > b/src/main/java/org/apache/commons/csv/CSVParser.java > > index dc1684c..c718521 100644 > > --- a/src/main/java/org/apache/commons/csv/CSVParser.java > > +++ b/src/main/java/org/apache/commons/csv/CSVParser.java > > @@ -17,13 +17,17 @@ > > > > 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.IOException; > > +import java.io.InputStream; > > import java.io.InputStreamReader; > > import java.io.Reader; > > import java.io.StringReader; > > +import java.io.UnsupportedEncodingException; > > import java.net.URL; > > import java.nio.charset.Charset; > > import java.util.ArrayList; > > @@ -35,8 +39,6 @@ import java.util.Map; > > import java.util.NoSuchElementException; > > import java.util.TreeMap; > > > > -import static org.apache.commons.csv.Token.Type.*; > > - > > /** > > * Parses CSV files according to the specified format. > > * > > @@ -133,6 +135,62 @@ import static org.apache.commons.csv.Token.Type.*; > > public final class CSVParser implements Iterable<CSVRecord>, Closeable { > > > > /** > > + * Customized CSV parser using the given {@link CSVFormat} > > + * > > + * <p> > > + * If you do not read all records from the given {@code reader}, you > > should > > + * call {@link #close()} on the parser, unless you close the {@code > > reader}. > > + * </p> > > + * > > + * @param reader > > + * a Reader containing CSV-formatted input. Must not be > > null. > > + * @param charsetName > > + * The name of a supported {@link > java.nio.charset.Charset > > + * </code>charset<code>} > > + * @param format > > + * the CSVFormat used for CSV parsing. Must not be null. > > + * @throws IllegalArgumentException > > + * If the parameters of the format are inconsistent or > if > > either > > + * reader or format are null. > > + * @throws UnsupportedEncodingException > > + * If the named charset is not supported > > + * @throws IOException > > + * If there is a problem reading the header or skipping > > the > > + * first record > > + * @since 1.5 > > + */ > > + @SuppressWarnings("resource") > > + public static CSVParser parse(final InputStream inputStream, final > > String charset, final CSVFormat format) throws IOException { > > + Assertions.notNull(inputStream, "inputStream"); > > + Assertions.notNull(format, "format"); > > + return parse(new InputStreamReader(inputStream, charset), > format); > > + } > > + > > + /** > > + * Customized CSV parser using the given {@link CSVFormat} > > + * > > + * <p> > > + * If you do not read all records from the given {@code reader}, you > > should > > + * call {@link #close()} on the parser, unless you close the {@code > > reader}. > > + * </p> > > + * > > + * @param reader > > + * a Reader containing CSV-formatted input. Must not be > > null. > > + * @param format > > + * the CSVFormat used for CSV parsing. Must not be null. > > + * @throws IllegalArgumentException > > + * If the parameters of the format are inconsistent or > if > > either > > + * reader or format are null. > > + * @throws IOException > > + * If there is a problem reading the header or skipping > > the > > + * first record > > + * @since 1.5 > > + */ > > + public static CSVParser parse(Reader reader, final CSVFormat format) > > throws IOException { > > + return new CSVParser(reader, format); > > + } > > + > > + /** > > * Creates a parser for the given {@link File}. > > * > > * <p><strong>Note:</strong> This method internally creates a > > FileReader using > > > > > > > http://git-wip-us.apache.org/repos/asf/commons-csv/blob/ac46f73b/src/test/java/org/apache/commons/csv/CSVParserTest.java > > ---------------------------------------------------------------------- > > diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java > > b/src/test/java/org/apache/commons/csv/CSVParserTest.java > > index 021279c..6fc8186 100644 > > --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java > > +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java > > @@ -71,6 +71,11 @@ public class CSVParserTest { > > private static final String[][] RESULT = { { "a", "b", "c", "d" }, { > > "a", "b", "1 2" }, { "foo baar", "b", "" }, > > { "foo\n,,\n\",,\n\"", "d", "e" } }; > > > > + private BOMInputStream createBOMInputStream(String resource) throws > > IOException { > > + final URL url = > > ClassLoader.getSystemClassLoader().getResource(resource); > > + return new BOMInputStream(url.openStream()); > > + } > > + > > @Test > > public void testBackslashEscaping() throws IOException { > > > > @@ -172,9 +177,8 @@ public class CSVParserTest { > > } > > > > @Test > > - public void testBOMInputStream() throws IOException { > > - final URL url = > > ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv"); > > - try (final Reader reader = new InputStreamReader(new > > BOMInputStream(url.openStream()), "UTF-8"); > > + public void testBOMInputStream_ParserWithReader() throws > IOException { > > + try (final Reader reader = new > > InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), > "UTF-8"); > > final CSVParser parser = new CSVParser(reader, > > CSVFormat.EXCEL.withHeader())) { > > for (final CSVRecord record : parser) { > > final String string = record.get("Date"); > > @@ -185,6 +189,30 @@ public class CSVParserTest { > > } > > > > @Test > > + public void testBOMInputStream_parseWithReader() throws IOException > { > > + try (final Reader reader = new > > InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), > "UTF-8"); > > + final CSVParser parser = CSVParser.parse(reader, > > CSVFormat.EXCEL.withHeader())) { > > + for (final CSVRecord record : parser) { > > + final String string = record.get("Date"); > > + Assert.assertNotNull(string); > > + // System.out.println("date: " + record.get("Date")); > > + } > > + } > > + } > > + > > + @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())) { > > + for (final CSVRecord record : parser) { > > + final String string = record.get("Date"); > > + Assert.assertNotNull(string); > > + // System.out.println("date: " + record.get("Date")); > > + } > > + } > > + } > > + > > + @Test > > public void testCarriageReturnEndings() throws IOException { > > final String code = "foo\rbaar,\rhello,world\r,kanu"; > > try (final CSVParser parser = CSVParser.parse(code, > > CSVFormat.DEFAULT)) { > > > > >