File tree Expand file tree Collapse file tree
main/java/org/apache/commons/csv
test/java/org/apache/commons/csv Expand file tree Collapse file tree Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 3131import java .io .ByteArrayOutputStream ;
3232import java .io .ObjectInputStream ;
3333import java .io .ObjectOutputStream ;
34+ import java .util .Arrays ;
3435
3536import 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
You can’t perform that action at this time.
0 commit comments