Skip to content

Commit 66da39d

Browse files
committed
Better resource management.
1 parent d1602ac commit 66da39d

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/test/java/org/apache/commons/csv/perf/PerformanceTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.zip.GZIPInputStream;
3131

3232
import org.apache.commons.csv.CSVFormat;
33+
import org.apache.commons.csv.CSVParser;
3334
import org.apache.commons.csv.CSVRecord;
3435
import org.apache.commons.io.IOUtils;
3536
import 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

Comments
 (0)