Skip to content

Commit 9daee90

Browse files
committed
Use try-with-resources.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1748094 13f79535-47bb-0310-9956-ffa450edef68
1 parent ed6adc7 commit 9daee90

12 files changed

Lines changed: 1139 additions & 1152 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ public boolean equals(final Object obj) {
609609
*/
610610
public String format(final Object... values) {
611611
final StringWriter out = new StringWriter();
612-
try {
613-
new CSVPrinter(out, this).printRecord(values);
612+
try (final CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
613+
csvPrinter.printRecord(values);
614614
return out.toString().trim();
615615
} catch (final IOException e) {
616616
// should not happen because a StringWriter does not do IO.

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ public void testCSVFile() throws Exception {
116116

117117
// Now parse the file and compare against the expected results
118118
// We use a buffered reader internally so no need to create one here.
119-
final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format);
120-
for (final CSVRecord record : parser) {
121-
String parsed = Arrays.toString(record.values());
122-
if (checkComments) {
123-
final String comment = record.getComment().replace("\n", "\\n");
124-
if (comment != null) {
125-
parsed += "#" + comment;
119+
try (final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format)) {
120+
for (final CSVRecord record : parser) {
121+
String parsed = Arrays.toString(record.values());
122+
if (checkComments) {
123+
final String comment = record.getComment().replace("\n", "\\n");
124+
if (comment != null) {
125+
parsed += "#" + comment;
126+
}
126127
}
128+
final int count = record.size();
129+
assertEquals(testName, readTestData(), count + ":" + parsed);
127130
}
128-
final int count = record.size();
129-
assertEquals(testName, readTestData(), count + ":" + parsed);
130131
}
131-
parser.close();
132132
}
133133

134134
@Test
@@ -160,18 +160,18 @@ public void testCSVUrl() throws Exception {
160160

161161
// Now parse the file and compare against the expected results
162162
final URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
163-
final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format);
164-
for (final CSVRecord record : parser) {
165-
String parsed = Arrays.toString(record.values());
166-
if (checkComments) {
167-
final String comment = record.getComment().replace("\n", "\\n");
168-
if (comment != null) {
169-
parsed += "#" + comment;
163+
try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) {
164+
for (final CSVRecord record : parser) {
165+
String parsed = Arrays.toString(record.values());
166+
if (checkComments) {
167+
final String comment = record.getComment().replace("\n", "\\n");
168+
if (comment != null) {
169+
parsed += "#" + comment;
170+
}
170171
}
172+
final int count = record.size();
173+
assertEquals(testName, readTestData(), count + ":" + parsed);
171174
}
172-
final int count = record.size();
173-
assertEquals(testName, readTestData(), count + ":" + parsed);
174175
}
175-
parser.close();
176176
}
177177
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,10 @@ public void testRFC4180() {
327327
public void testSerialization() throws Exception {
328328
final ByteArrayOutputStream out = new ByteArrayOutputStream();
329329

330-
final ObjectOutputStream oos = new ObjectOutputStream(out);
331-
oos.writeObject(CSVFormat.DEFAULT);
332-
oos.flush();
333-
oos.close();
330+
try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
331+
oos.writeObject(CSVFormat.DEFAULT);
332+
oos.flush();
333+
}
334334

335335
final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
336336
final CSVFormat format = (CSVFormat) in.readObject();

0 commit comments

Comments
 (0)