Skip to content

Commit 390800f

Browse files
committed
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
1 parent 6999746 commit 390800f

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,13 @@ public Character getEscape() {
334334
return escape;
335335
}
336336

337-
String[] getHeader() {
338-
return header;
337+
/**
338+
* Returns a copy of the header array.
339+
*
340+
* @return a copy of the header array
341+
*/
342+
public String[] getHeader() {
343+
return header != null ? header.clone() : null;
339344
}
340345

341346
/**

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,18 +321,19 @@ public List<CSVRecord> getRecords() throws IOException {
321321
*/
322322
private Map<String, Integer> initializeHeader() throws IOException {
323323
Map<String, Integer> hdrMap = null;
324-
if (this.format.getHeader() != null) {
324+
String[] formatHeader = this.format.getHeader();
325+
if (formatHeader != null) {
325326
hdrMap = new LinkedHashMap<String, Integer>();
326327

327328
String[] header = null;
328-
if (this.format.getHeader().length == 0) {
329+
if (formatHeader.length == 0) {
329330
// read the header from the first line of the file
330331
final CSVRecord record = this.nextRecord();
331332
if (record != null) {
332333
header = record.values();
333334
}
334335
} else {
335-
header = this.format.getHeader();
336+
header = formatHeader;
336337
}
337338

338339
// build the name to index mappings

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.ByteArrayOutputStream;
3232
import java.io.ObjectInputStream;
3333
import java.io.ObjectOutputStream;
34+
import java.util.Arrays;
3435

3536
import org.junit.Test;
3637

@@ -224,9 +225,27 @@ public void testWithEscape() throws Exception {
224225
@Test
225226
public void testWithHeader() throws Exception {
226227
String[] header = new String[]{"one", "two", "three"};
228+
// withHeader() makes a copy of the header array.
227229
CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
228230
assertArrayEquals(header, formatWithHeader.getHeader());
229231
assertNotSame(header, formatWithHeader.getHeader());
232+
header[0] = "A";
233+
header[1] = "B";
234+
header[2] = "C";
235+
assertFalse(Arrays.equals(formatWithHeader.getHeader(), header));
236+
}
237+
238+
@Test
239+
public void testGetHeader() throws Exception {
240+
String[] header = new String[]{"one", "two", "three"};
241+
CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
242+
// getHeader() makes a copy of the header array.
243+
String[] headerCopy = formatWithHeader.getHeader();
244+
headerCopy[0] = "A";
245+
headerCopy[1] = "B";
246+
headerCopy[2] = "C";
247+
assertFalse(Arrays.equals(formatWithHeader.getHeader(), headerCopy));
248+
assertNotSame(formatWithHeader.getHeader(), headerCopy);
230249
}
231250

232251
@Test

0 commit comments

Comments
 (0)