@@ -410,7 +410,7 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
410410 this .format = format ;
411411 this .lexer = new Lexer (format , new ExtendedBufferedReader (reader ));
412412 this .csvRecordIterator = new CSVRecordIterator ();
413- Headers headers = createHeaderMapAndHeaderNames ();
413+ Headers headers = createHeaders ();
414414 this .headerMap = headers .headerMap ;
415415 this .headerNames = headers .headerNames ;
416416 this .characterOffset = characterOffset ;
@@ -446,8 +446,18 @@ private Map<String, Integer> createEmptyHeaderMap() {
446446 new LinkedHashMap <>();
447447 }
448448
449+ /**
450+ * Header information based on name and position.
451+ */
449452 private static final class Headers {
453+ /**
454+ * Header column positions (0-based)
455+ */
450456 final Map <String , Integer > headerMap ;
457+
458+ /**
459+ * Header names in column order
460+ */
451461 final List <String > headerNames ;
452462
453463 Headers (Map <String , Integer > headerMap , List <String > headerNames ) {
@@ -462,7 +472,7 @@ private static final class Headers {
462472 * @return null if the format has no header.
463473 * @throws IOException if there is a problem reading the header or skipping the first record
464474 */
465- private Headers createHeaderMapAndHeaderNames () throws IOException {
475+ private Headers createHeaders () throws IOException {
466476 Map <String , Integer > hdrMap = null ;
467477 List <String > headerNames = null ;
468478 final String [] formatHeader = this .format .getHeader ();
@@ -490,9 +500,10 @@ private Headers createHeaderMapAndHeaderNames() throws IOException {
490500 final boolean emptyHeader = header == null || header .trim ().isEmpty ();
491501 if (containsHeader ) {
492502 if (!emptyHeader && !this .format .getAllowDuplicateHeaderNames ()) {
493- throw new IllegalArgumentException ("The header contains a duplicate name: \" " + header
494- + "\" in " + Arrays .toString (headerRecord )
495- + ". If this is valid then use CSVFormat.withAllowDuplicateHeaderNames()." );
503+ throw new IllegalArgumentException (
504+ String .format ("The header contains a duplicate name: \" %s\" in %s."
505+ + " If this is valid then use CSVFormat.withAllowDuplicateHeaderNames()." ,
506+ header , Arrays .toString (headerRecord )));
496507 }
497508 if (emptyHeader && !this .format .getAllowMissingColumnNames ()) {
498509 throw new IllegalArgumentException (
@@ -502,7 +513,7 @@ private Headers createHeaderMapAndHeaderNames() throws IOException {
502513 if (header != null ) {
503514 hdrMap .put (header , Integer .valueOf (i ));
504515 if (headerNames == null ) {
505- headerNames = new ArrayList <>();
516+ headerNames = new ArrayList <>(headerRecord . length );
506517 }
507518 headerNames .add (header );
508519 }
0 commit comments