From 2ec4c994c0458ef893af9bd518849bec21b2dec4 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Thu, 8 Mar 2012 09:59:51 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/csv/CSVParser.java | 37 +++++++++---------- .../org/apache/commons/csv/CSVParserTest.java | 4 +- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index e980dbe7..0c4b332c 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -68,7 +68,7 @@ public class CSVParser implements Iterable { // the following objects are shared to reduce garbage - /** A record buffer for getLine(). Grows as necessary and is reused. */ + /** A record buffer for getRecord(). Grows as necessary and is reused. */ private final List record = new ArrayList(); private final Token reusableToken = new Token(); @@ -112,7 +112,7 @@ public class CSVParser implements Iterable { /** - * Parses the CSV according to the given format and returns the content + * Parses the CSV input according to the given format and returns the content * as an array of records (whereas records are arrays of single values). *

* The returned content starts at the current parse-position in the stream. @@ -122,26 +122,26 @@ public class CSVParser implements Iterable { */ public String[][] getRecords() throws IOException { List records = new ArrayList(); - String[] values; - String[][] ret = null; - while ((values = getLine()) != null) { - records.add(values); + String[] record; + while ((record = getRecord()) != null) { + records.add(record); } - if (records.size() > 0) { - ret = new String[records.size()][]; - records.toArray(ret); + + if (!records.isEmpty()) { + return records.toArray(new String[records.size()][]); + } else { + return null; } - return ret; } /** - * Parses from the current point in the stream til the end of the current line. + * Parses the next record from the current point in the stream. * - * @return array of values til end of line ('null' when end of file has been reached) + * @return the record as an array of values, or null if the end of the stream has been reached * @throws IOException on parse error or input read-failure */ - String[] getLine() throws IOException { - String[] ret = EMPTY_STRING_ARRAY; + String[] getRecord() throws IOException { + String[] result = EMPTY_STRING_ARRAY; record.clear(); while (true) { reusableToken.reset(); @@ -157,11 +157,10 @@ public class CSVParser implements Iterable { if (reusableToken.isReady) { record.add(reusableToken.content.toString()); } else { - ret = null; + result = null; } break; case INVALID: - default: // error: throw IOException throw new IOException("(line " + getLineNumber() + ") invalid parse sequence"); // unreachable: break; @@ -171,9 +170,9 @@ public class CSVParser implements Iterable { } } if (!record.isEmpty()) { - ret = record.toArray(new String[record.size()]); + result = record.toArray(new String[record.size()]); } - return ret; + return result; } /** @@ -209,7 +208,7 @@ public class CSVParser implements Iterable { private String[] getNextLine() { try { - return getLine(); + return getRecord(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index a62f41b4..89a57b70 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -54,10 +54,10 @@ public class CSVParserTest extends TestCase { public void testGetLine() throws IOException { CSVParser parser = new CSVParser(new StringReader(code)); for (String[] re : res) { - assertTrue(Arrays.equals(re, parser.getLine())); + assertTrue(Arrays.equals(re, parser.getRecord())); } - assertTrue(parser.getLine() == null); + assertTrue(parser.getRecord() == null); } public void testGetRecords() throws IOException {