|
17 | 17 |
|
18 | 18 | package org.apache.commons.csv; |
19 | 19 |
|
| 20 | +import static org.apache.commons.csv.Token.Type.TOKEN; |
| 21 | + |
20 | 22 | import java.io.Closeable; |
21 | 23 | import java.io.File; |
22 | 24 | import java.io.FileInputStream; |
23 | 25 | import java.io.IOException; |
| 26 | +import java.io.InputStream; |
24 | 27 | import java.io.InputStreamReader; |
25 | 28 | import java.io.Reader; |
26 | 29 | import java.io.StringReader; |
| 30 | +import java.io.UnsupportedEncodingException; |
27 | 31 | import java.net.URL; |
28 | 32 | import java.nio.charset.Charset; |
29 | 33 | import java.util.ArrayList; |
|
35 | 39 | import java.util.NoSuchElementException; |
36 | 40 | import java.util.TreeMap; |
37 | 41 |
|
38 | | -import static org.apache.commons.csv.Token.Type.*; |
39 | | - |
40 | 42 | /** |
41 | 43 | * Parses CSV files according to the specified format. |
42 | 44 | * |
|
132 | 134 | */ |
133 | 135 | public final class CSVParser implements Iterable<CSVRecord>, Closeable { |
134 | 136 |
|
| 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 | + |
135 | 193 | /** |
136 | 194 | * Creates a parser for the given {@link File}. |
137 | 195 | * |
|
0 commit comments