From 390800f28802a5c9e020e5aa901c67d5a9a618c2 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Tue, 30 Jul 2013 19:32:40 +0000 Subject: [PATCH] Make org.apache.commons.csv.CSVFormat.getHeader() public and make it return a clone. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508585 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/csv/CSVFormat.java | 9 +++++++-- .../org/apache/commons/csv/CSVParser.java | 7 ++++--- .../org/apache/commons/csv/CSVFormatTest.java | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 3fac998e..a8129ed8 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -334,8 +334,13 @@ public class CSVFormat implements Serializable { return escape; } - String[] getHeader() { - return header; + /** + * Returns a copy of the header array. + * + * @return a copy of the header array + */ + public String[] getHeader() { + return header != null ? header.clone() : null; } /** diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index d6c81638..91265343 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -321,18 +321,19 @@ public class CSVParser implements Iterable, Closeable { */ private Map initializeHeader() throws IOException { Map hdrMap = null; - if (this.format.getHeader() != null) { + String[] formatHeader = this.format.getHeader(); + if (formatHeader != null) { hdrMap = new LinkedHashMap(); String[] header = null; - if (this.format.getHeader().length == 0) { + if (formatHeader.length == 0) { // read the header from the first line of the file final CSVRecord record = this.nextRecord(); if (record != null) { header = record.values(); } } else { - header = this.format.getHeader(); + header = formatHeader; } // build the name to index mappings diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index fa38a5b2..2d07dea7 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.Arrays; import org.junit.Test; @@ -224,9 +225,27 @@ public class CSVFormatTest { @Test public void testWithHeader() throws Exception { String[] header = new String[]{"one", "two", "three"}; + // withHeader() makes a copy of the header array. CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header); assertArrayEquals(header, formatWithHeader.getHeader()); assertNotSame(header, formatWithHeader.getHeader()); + header[0] = "A"; + header[1] = "B"; + header[2] = "C"; + assertFalse(Arrays.equals(formatWithHeader.getHeader(), header)); + } + + @Test + public void testGetHeader() throws Exception { + String[] header = new String[]{"one", "two", "three"}; + CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header); + // getHeader() makes a copy of the header array. + String[] headerCopy = formatWithHeader.getHeader(); + headerCopy[0] = "A"; + headerCopy[1] = "B"; + headerCopy[2] = "C"; + assertFalse(Arrays.equals(formatWithHeader.getHeader(), headerCopy)); + assertNotSame(formatWithHeader.getHeader(), headerCopy); } @Test