3030import java .util .zip .GZIPInputStream ;
3131
3232import org .apache .commons .csv .CSVFormat ;
33+ import org .apache .commons .csv .CSVParser ;
3334import org .apache .commons .csv .CSVRecord ;
3435import org .apache .commons .io .IOUtils ;
3536import org .junit .jupiter .api .BeforeAll ;
@@ -54,9 +55,10 @@ public static void setUpClass() throws FileNotFoundException, IOException {
5455 return ;
5556 }
5657 System .out .println ("Decompressing test fixture " + BIG_FILE + "..." );
57- try (final InputStream input = new GZIPInputStream (
58+ try (
59+ final InputStream input = new GZIPInputStream (
5860 new FileInputStream ("src/test/resources/perf/worldcitiespop.txt.gz" ));
59- final OutputStream output = new FileOutputStream (BIG_FILE )) {
61+ final OutputStream output = new FileOutputStream (BIG_FILE )) {
6062 IOUtils .copy (input , output );
6163 System .out .println (String .format ("Decompressed test fixture %s: %,d bytes." , BIG_FILE , BIG_FILE .length ()));
6264 }
@@ -66,14 +68,17 @@ private BufferedReader createBufferedReader() throws IOException {
6668 return new BufferedReader (new FileReader (BIG_FILE ));
6769 }
6870
69- private long parse (final Reader in , final boolean traverseColumns ) throws IOException {
70- final CSVFormat format = CSVFormat .DEFAULT .withIgnoreSurroundingSpaces ( false );
71+ private long parse (final Reader reader , final boolean traverseColumns ) throws IOException {
72+ final CSVFormat format = CSVFormat .DEFAULT .builder (). setIgnoreSurroundingSpaces ( false ). build ( );
7173 long recordCount = 0 ;
72- for (final CSVRecord record : format .parse (in )) {
73- recordCount ++;
74- if (traverseColumns ) {
75- for (@ SuppressWarnings ("unused" ) final String value : record ) {
76- // do nothing for now
74+ try (final CSVParser parser = format .parse (reader )) {
75+ for (final CSVRecord record : parser ) {
76+ recordCount ++;
77+ if (traverseColumns ) {
78+ for (@ SuppressWarnings ("unused" )
79+ final String value : record ) {
80+ // do nothing for now
81+ }
7782 }
7883 }
7984 }
@@ -97,7 +102,8 @@ public long testParseBigFile(final boolean traverseColumns) throws Exception {
97102 try (final BufferedReader reader = this .createBufferedReader ()) {
98103 final long count = this .parse (reader , traverseColumns );
99104 final long totalMillis = System .currentTimeMillis () - startMillis ;
100- this .println (String .format ("File parsed in %,d milliseconds with Commons CSV: %,d lines." , totalMillis , count ));
105+ this .println (
106+ String .format ("File parsed in %,d milliseconds with Commons CSV: %,d lines." , totalMillis , count ));
101107 return totalMillis ;
102108 }
103109 }
0 commit comments