@@ -218,13 +218,16 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
218218 /** A record buffer for getRecord(). Grows as necessary and is reused. */
219219 private final List <String > record = new ArrayList <String >();
220220
221+ /**
222+ * The next record number to assign.
223+ */
221224 private long recordNumber ;
222225
223226 /**
224- * Lexer offset if the parser does not start parsing at the beginning of the source. Usually used in combination
227+ * Lexer offset when the parser does not start parsing at the beginning of the source. Usually used in combination
225228 * with {@link #setNextRecordNumber(long)}
226229 */
227- private long characterOffset ;
230+ private final long characterOffset ;
228231
229232 private final Token reusableToken = new Token ();
230233
@@ -246,12 +249,41 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
246249 * If there is a problem reading the header or skipping the first record
247250 */
248251 public CSVParser (final Reader reader , final CSVFormat format ) throws IOException {
252+ this (reader , format , 0 , 1 );
253+ }
254+
255+ /**
256+ * Customized CSV parser using the given {@link CSVFormat}
257+ *
258+ * <p>
259+ * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
260+ * unless you close the {@code reader}.
261+ * </p>
262+ *
263+ * @param reader
264+ * a Reader containing CSV-formatted input. Must not be null.
265+ * @param format
266+ * the CSVFormat used for CSV parsing. Must not be null.
267+ * @param characterOffset
268+ * Lexer offset when the parser does not start parsing at the beginning of the source.
269+ * @param recordNumber
270+ * The next record number to assign
271+ * @throws IllegalArgumentException
272+ * If the parameters of the format are inconsistent or if either reader or format are null.
273+ * @throws IOException
274+ * If there is a problem reading the header or skipping the first record
275+ * @since 1.1
276+ */
277+ public CSVParser (final Reader reader , final CSVFormat format , long characterOffset , long recordNumber )
278+ throws IOException {
249279 Assertions .notNull (reader , "reader" );
250280 Assertions .notNull (format , "format" );
251281
252282 this .format = format ;
253283 this .lexer = new Lexer (format , new ExtendedBufferedReader (reader ));
254284 this .headerMap = this .initializeHeader ();
285+ this .characterOffset = characterOffset ;
286+ this .recordNumber = recordNumber - 1 ;
255287 }
256288
257289 private void addRecordValue () {
@@ -301,43 +333,6 @@ public Map<String, Integer> getHeaderMap() {
301333 return this .headerMap == null ? null : new LinkedHashMap <String , Integer >(this .headerMap );
302334 }
303335
304- /**
305- * Sets the record number to be assigned to the next record read.
306- * <p>
307- * Use this if the reader is not positioned at the first record when you create the parser. For example, the first
308- * record read might be the 51st record in the source file.
309- * </p>
310- * <p>
311- * If you want the records to also have the correct character position referring to the underlying source, call
312- * {@link #setNextCharacterPosition(long)}.
313- * </p>
314- *
315- * @param nextRecordNumber
316- * the next record number
317- * @since 1.1
318- */
319- public void setNextRecordNumber (long nextRecordNumber ) {
320- this .recordNumber = nextRecordNumber - 1 ;
321- }
322-
323- /**
324- * Sets the current position in the source stream regardless of where the parser and lexer start reading.
325- * <p>
326- * For example: We open a file and seek to position 5434 in order to start reading at record 42. In order to have
327- * the parser assign the correct characterPosition to records, we call this method.
328- * </p>
329- * <p>
330- * If you want the records to also have the correct record numbers, call {@link #setNextRecordNumber(long)}
331- * </p>
332- *
333- * @param position
334- * the new character position
335- * @since 1.1
336- */
337- public void setNextCharacterPosition (long position ) {
338- this .characterOffset = position - lexer .getCharacterPosition ();
339- }
340-
341336 /**
342337 * Returns the current record number in the input stream.
343338 *
@@ -346,7 +341,7 @@ public void setNextCharacterPosition(long position) {
346341 * the line number.
347342 * </p>
348343 *
349- * @return current line number
344+ * @return current record number
350345 */
351346 public long getRecordNumber () {
352347 return this .recordNumber ;
0 commit comments