Skip to content

Commit 2ec4c99

Browse files
committed
Renamed CSVParser.getLine() into getRecord() to avoid confusions since a record can span several lines
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1298333 13f79535-47bb-0310-9956-ffa450edef68
1 parent a65806a commit 2ec4c99

2 files changed

Lines changed: 20 additions & 21 deletions

File tree

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class CSVParser implements Iterable<String[]> {
6868

6969
// the following objects are shared to reduce garbage
7070

71-
/** A record buffer for getLine(). Grows as necessary and is reused. */
71+
/** A record buffer for getRecord(). Grows as necessary and is reused. */
7272
private final List<String> record = new ArrayList<String>();
7373
private final Token reusableToken = new Token();
7474

@@ -112,7 +112,7 @@ public CSVParser(String input, CSVFormat format) {
112112

113113

114114
/**
115-
* Parses the CSV according to the given format and returns the content
115+
* Parses the CSV input according to the given format and returns the content
116116
* as an array of records (whereas records are arrays of single values).
117117
* <p/>
118118
* The returned content starts at the current parse-position in the stream.
@@ -122,26 +122,26 @@ public CSVParser(String input, CSVFormat format) {
122122
*/
123123
public String[][] getRecords() throws IOException {
124124
List<String[]> records = new ArrayList<String[]>();
125-
String[] values;
126-
String[][] ret = null;
127-
while ((values = getLine()) != null) {
128-
records.add(values);
125+
String[] record;
126+
while ((record = getRecord()) != null) {
127+
records.add(record);
129128
}
130-
if (records.size() > 0) {
131-
ret = new String[records.size()][];
132-
records.toArray(ret);
129+
130+
if (!records.isEmpty()) {
131+
return records.toArray(new String[records.size()][]);
132+
} else {
133+
return null;
133134
}
134-
return ret;
135135
}
136136

137137
/**
138-
* Parses from the current point in the stream til the end of the current line.
138+
* Parses the next record from the current point in the stream.
139139
*
140-
* @return array of values til end of line ('null' when end of file has been reached)
140+
* @return the record as an array of values, or <tt>null</tt> if the end of the stream has been reached
141141
* @throws IOException on parse error or input read-failure
142142
*/
143-
String[] getLine() throws IOException {
144-
String[] ret = EMPTY_STRING_ARRAY;
143+
String[] getRecord() throws IOException {
144+
String[] result = EMPTY_STRING_ARRAY;
145145
record.clear();
146146
while (true) {
147147
reusableToken.reset();
@@ -157,11 +157,10 @@ String[] getLine() throws IOException {
157157
if (reusableToken.isReady) {
158158
record.add(reusableToken.content.toString());
159159
} else {
160-
ret = null;
160+
result = null;
161161
}
162162
break;
163163
case INVALID:
164-
default:
165164
// error: throw IOException
166165
throw new IOException("(line " + getLineNumber() + ") invalid parse sequence");
167166
// unreachable: break;
@@ -171,9 +170,9 @@ String[] getLine() throws IOException {
171170
}
172171
}
173172
if (!record.isEmpty()) {
174-
ret = record.toArray(new String[record.size()]);
173+
result = record.toArray(new String[record.size()]);
175174
}
176-
return ret;
175+
return result;
177176
}
178177

179178
/**
@@ -209,7 +208,7 @@ public String[] next() {
209208

210209
private String[] getNextLine() {
211210
try {
212-
return getLine();
211+
return getRecord();
213212
} catch (IOException e) {
214213
throw new RuntimeException(e);
215214
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public class CSVParserTest extends TestCase {
5454
public void testGetLine() throws IOException {
5555
CSVParser parser = new CSVParser(new StringReader(code));
5656
for (String[] re : res) {
57-
assertTrue(Arrays.equals(re, parser.getLine()));
57+
assertTrue(Arrays.equals(re, parser.getRecord()));
5858
}
5959

60-
assertTrue(parser.getLine() == null);
60+
assertTrue(parser.getRecord() == null);
6161
}
6262

6363
public void testGetRecords() throws IOException {

0 commit comments

Comments
 (0)