Skip to content

Commit e100d62

Browse files
committed
Don't use deprecated code in examples
1 parent 8a827b2 commit e100d62

File tree

3 files changed

+133
-16
lines changed

3 files changed

+133
-16
lines changed

src/site/xdoc/user-guide.xml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,45 @@ for (CSVRecord record : records) {
7070
<a href="https://commons.apache.org/proper/commons-io/">Apache Commons IO</a>
7171
for example:
7272
</p>
73-
<source>final URL url = ...;
74-
try (final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
75-
final CSVParser parser = CSVFormat.EXCEL.builder()
76-
.setHeader()
77-
.build()
78-
.parse(reader)) {
73+
<source>
74+
try (final Reader reader = new InputStreamReader(BOMInputStream.builder()
75+
.setPath(path)
76+
.get(), "UTF-8");
77+
final CSVParser parser = CSVFormat.EXCEL.builder()
78+
.setHeader()
79+
.get()
80+
.parse(reader)) {
7981
for (final CSVRecord record : parser) {
80-
final String string = record.get("SomeColumn");
81-
...
82+
final String string = record.get("ColumnA");
83+
// ...
8284
}
8385
}
8486
</source>
8587
<p>
8688
You might find it handy to create something like this:
8789
</p>
88-
<source>/**
90+
<source>
91+
/**
8992
* Creates a reader capable of handling BOMs.
93+
*
94+
* @param path The path to read.
95+
* @return a new InputStreamReader for UTF-8 bytes.
96+
* @throws IOException if an I/O error occurs.
9097
*/
91-
public InputStreamReader newReader(final InputStream inputStream) {
92-
return new InputStreamReader(new BOMInputStream(inputStream), StandardCharsets.UTF_8);
98+
public InputStreamReader newReader(final Path path) throws IOException {
99+
return new InputStreamReader(BOMInputStream.builder()
100+
.setPath(path)
101+
.get(), StandardCharsets.UTF_8);
93102
}
94103
</source>
95104
</subsection>
96105
</section>
97-
98106
<section name="Working with headers">
99-
100107
Apache Commons CSV provides several ways to access record values.
101108
The simplest way is to access values by their index in the record.
102109
However, columns in CSV files often have a name, for example: ID, CustomerNo, Birthday, etc.
103110
The CSVFormat class provides an API for specifying these <i>header</i> names and CSVRecord on
104111
the other hand has methods to access values by their corresponding header name.
105-
106112
<subsection name="Accessing column values by index">
107113
To access a record value by index, no special configuration of the CSVFormat is necessary:
108114
<source>Reader in = new FileReader(&quot;path/to/file.csv&quot;);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.commons.csv;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
import java.io.IOException;
24+
import java.io.InputStreamReader;
25+
import java.io.Reader;
26+
import java.io.UnsupportedEncodingException;
27+
import java.nio.charset.StandardCharsets;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
31+
import org.apache.commons.io.input.BOMInputStream;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.io.TempDir;
34+
35+
/**
36+
* Tests for the user guide.
37+
*/
38+
public class UserGuideTest {
39+
40+
@TempDir
41+
Path tempDir;
42+
43+
/**
44+
* Creates a reader capable of handling BOMs.
45+
*
46+
* @param path The path to read.
47+
* @return a new InputStreamReader for UTF-8 bytes.
48+
* @throws IOException if an I/O error occurs.
49+
*/
50+
public InputStreamReader newReader(final Path path) throws IOException {
51+
return new InputStreamReader(BOMInputStream.builder()
52+
.setPath(path)
53+
.get(), StandardCharsets.UTF_8);
54+
}
55+
56+
@Test
57+
public void testBomFull() throws UnsupportedEncodingException, IOException {
58+
final Path path = tempDir.resolve("test1.csv");
59+
Files.copy(Utils.createUtf8Input("ColumnA, ColumnB, ColumnC\r\nA, B, C\r\n".getBytes(StandardCharsets.UTF_8), true), path);
60+
// @formatter:off
61+
try (final Reader reader = new InputStreamReader(BOMInputStream.builder()
62+
.setPath(path)
63+
.get(), "UTF-8");
64+
final CSVParser parser = CSVFormat.EXCEL.builder()
65+
.setHeader()
66+
.get()
67+
.parse(reader)) {
68+
// @formatter:off
69+
for (final CSVRecord record : parser) {
70+
final String string = record.get("ColumnA");
71+
assertEquals("A", string);
72+
}
73+
}
74+
}
75+
76+
@Test
77+
public void testBomUtil() throws UnsupportedEncodingException, IOException {
78+
final Path path = tempDir.resolve("test2.csv");
79+
Files.copy(Utils.createUtf8Input("ColumnA, ColumnB, ColumnC\r\nA, B, C\r\n".getBytes(StandardCharsets.UTF_8), true), path);
80+
try (final Reader reader = newReader(path);
81+
// @formatter:off
82+
final CSVParser parser = CSVFormat.EXCEL.builder()
83+
.setHeader()
84+
.get()
85+
.parse(reader)) {
86+
// @formatter:off
87+
for (final CSVRecord record : parser) {
88+
final String string = record.get("ColumnA");
89+
assertEquals("A", string);
90+
}
91+
}
92+
}
93+
94+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
2424

25+
import java.io.ByteArrayInputStream;
26+
import java.io.InputStream;
2527
import java.util.List;
2628

2729
/**
@@ -32,9 +34,9 @@ final class Utils {
3234
/**
3335
* Checks if the 2d array has the same contents as the list of records.
3436
*
35-
* @param message the message to be displayed
37+
* @param message the message to be displayed
3638
* @param expected the 2d array of expected results
37-
* @param actual the List of {@link CSVRecord} entries, each containing an array of values
39+
* @param actual the List of {@link CSVRecord} entries, each containing an array of values
3840
*/
3941
public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual) {
4042
final int expectedLength = expected.length;
@@ -44,6 +46,21 @@ public static void compare(final String message, final String[][] expected, fina
4446
}
4547
}
4648

49+
/**
50+
* Creates an input stream, with or without a BOM.
51+
*/
52+
static InputStream createUtf8Input(final byte[] baseData, final boolean addBom) {
53+
byte[] data = baseData;
54+
if (addBom) {
55+
data = new byte[baseData.length + 3];
56+
data[0] = (byte) 0xEF;
57+
data[1] = (byte) 0xBB;
58+
data[2] = (byte) 0xBF;
59+
System.arraycopy(baseData, 0, data, 3, baseData.length);
60+
}
61+
return new ByteArrayInputStream(data);
62+
}
63+
4764
private Utils() {
4865
}
4966
}

0 commit comments

Comments
 (0)