Skip to content

Commit ac46f73

Browse files
committed
[CSV-189] CSVParser: Add factory method accepting InputStream.
1 parent 0d7c984 commit ac46f73

3 files changed

Lines changed: 92 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<body>
4141
<release version="1.5" date="2016-MM-DD" description="Bug fix release">
4242
<action issue="CSV-187" type="update" dev="ggregory" due-to="Gary Gregory">Update platform requirement from Java 6 to 7.</action>
43+
<action issue="CSV-189" type="add" dev="ggregory" due-to="Peter Holzwarth, Gary Gregory">CSVParser: Add factory method accepting InputStream.</action>
4344
<action issue="CSV-???" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(File, Charset)</action>
4445
<action issue="CSV-???" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(Path, Charset)</action>
4546
</release>

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Token.Type.TOKEN;
21+
2022
import java.io.Closeable;
2123
import java.io.File;
2224
import java.io.FileInputStream;
2325
import java.io.IOException;
26+
import java.io.InputStream;
2427
import java.io.InputStreamReader;
2528
import java.io.Reader;
2629
import java.io.StringReader;
30+
import java.io.UnsupportedEncodingException;
2731
import java.net.URL;
2832
import java.nio.charset.Charset;
2933
import java.util.ArrayList;
@@ -35,8 +39,6 @@
3539
import java.util.NoSuchElementException;
3640
import java.util.TreeMap;
3741

38-
import static org.apache.commons.csv.Token.Type.*;
39-
4042
/**
4143
* Parses CSV files according to the specified format.
4244
*
@@ -132,6 +134,62 @@
132134
*/
133135
public final class CSVParser implements Iterable<CSVRecord>, Closeable {
134136

137+
/**
138+
* Customized CSV parser using the given {@link CSVFormat}
139+
*
140+
* <p>
141+
* If you do not read all records from the given {@code reader}, you should
142+
* call {@link #close()} on the parser, unless you close the {@code reader}.
143+
* </p>
144+
*
145+
* @param reader
146+
* a Reader containing CSV-formatted input. Must not be null.
147+
* @param charsetName
148+
* The name of a supported {@link java.nio.charset.Charset
149+
* </code>charset<code>}
150+
* @param format
151+
* the CSVFormat used for CSV parsing. Must not be null.
152+
* @throws IllegalArgumentException
153+
* If the parameters of the format are inconsistent or if either
154+
* reader or format are null.
155+
* @throws UnsupportedEncodingException
156+
* If the named charset is not supported
157+
* @throws IOException
158+
* If there is a problem reading the header or skipping the
159+
* first record
160+
* @since 1.5
161+
*/
162+
@SuppressWarnings("resource")
163+
public static CSVParser parse(final InputStream inputStream, final String charset, final CSVFormat format) throws IOException {
164+
Assertions.notNull(inputStream, "inputStream");
165+
Assertions.notNull(format, "format");
166+
return parse(new InputStreamReader(inputStream, charset), format);
167+
}
168+
169+
/**
170+
* Customized CSV parser using the given {@link CSVFormat}
171+
*
172+
* <p>
173+
* If you do not read all records from the given {@code reader}, you should
174+
* call {@link #close()} on the parser, unless you close the {@code reader}.
175+
* </p>
176+
*
177+
* @param reader
178+
* a Reader containing CSV-formatted input. Must not be null.
179+
* @param format
180+
* the CSVFormat used for CSV parsing. Must not be null.
181+
* @throws IllegalArgumentException
182+
* If the parameters of the format are inconsistent or if either
183+
* reader or format are null.
184+
* @throws IOException
185+
* If there is a problem reading the header or skipping the
186+
* first record
187+
* @since 1.5
188+
*/
189+
public static CSVParser parse(Reader reader, final CSVFormat format) throws IOException {
190+
return new CSVParser(reader, format);
191+
}
192+
135193
/**
136194
* Creates a parser for the given {@link File}.
137195
*

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public class CSVParserTest {
7171
private static final String[][] RESULT = { { "a", "b", "c", "d" }, { "a", "b", "1 2" }, { "foo baar", "b", "" },
7272
{ "foo\n,,\n\",,\n\"", "d", "e" } };
7373

74+
private BOMInputStream createBOMInputStream(String resource) throws IOException {
75+
final URL url = ClassLoader.getSystemClassLoader().getResource(resource);
76+
return new BOMInputStream(url.openStream());
77+
}
78+
7479
@Test
7580
public void testBackslashEscaping() throws IOException {
7681

@@ -172,9 +177,8 @@ public void testBOM() throws IOException {
172177
}
173178

174179
@Test
175-
public void testBOMInputStream() throws IOException {
176-
final URL url = ClassLoader.getSystemClassLoader().getResource("CSVFileParser/bom.csv");
177-
try (final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
180+
public void testBOMInputStream_ParserWithReader() throws IOException {
181+
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), "UTF-8");
178182
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader())) {
179183
for (final CSVRecord record : parser) {
180184
final String string = record.get("Date");
@@ -184,6 +188,30 @@ public void testBOMInputStream() throws IOException {
184188
}
185189
}
186190

191+
@Test
192+
public void testBOMInputStream_parseWithReader() throws IOException {
193+
try (final Reader reader = new InputStreamReader(createBOMInputStream("CSVFileParser/bom.csv"), "UTF-8");
194+
final CSVParser parser = CSVParser.parse(reader, CSVFormat.EXCEL.withHeader())) {
195+
for (final CSVRecord record : parser) {
196+
final String string = record.get("Date");
197+
Assert.assertNotNull(string);
198+
// System.out.println("date: " + record.get("Date"));
199+
}
200+
}
201+
}
202+
203+
@Test
204+
public void testBOMInputStream_ParserWithInputStream() throws IOException {
205+
try (final BOMInputStream inputStream = createBOMInputStream("CSVFileParser/bom.csv");
206+
final CSVParser parser = CSVParser.parse(inputStream, "UTF-8", CSVFormat.EXCEL.withHeader())) {
207+
for (final CSVRecord record : parser) {
208+
final String string = record.get("Date");
209+
Assert.assertNotNull(string);
210+
// System.out.println("date: " + record.get("Date"));
211+
}
212+
}
213+
}
214+
187215
@Test
188216
public void testCarriageReturnEndings() throws IOException {
189217
final String code = "foo\rbaar,\rhello,world\r,kanu";

0 commit comments

Comments
 (0)