mirror of
https://github.com/apache/commons-csv.git
synced 2025-03-03 15:29:06 +00:00
Allow a caller to close the parser before reading all records and free resources. The parser and lexer now implement java.io.Closeable.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508475 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ee7335a2fc
commit
7af334d7d5
src/main/java/org/apache/commons/csv
@ -19,6 +19,7 @@ package org.apache.commons.csv;
|
||||
|
||||
import static org.apache.commons.csv.Token.Type.TOKEN;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
@ -80,7 +81,7 @@ import java.util.NoSuchElementException;
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CSVParser implements Iterable<CSVRecord> {
|
||||
public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
|
||||
private final Lexer lexer;
|
||||
private final Map<String, Integer> headerMap;
|
||||
@ -233,6 +234,15 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||
record.add(input.equalsIgnoreCase(nullString) ? null : input);
|
||||
}}
|
||||
|
||||
/**
|
||||
* Closes resources.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if (lexer != null) {
|
||||
lexer.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the CSV input according to the given format and returns the content as an array of {@link CSVRecord}
|
||||
* entries.
|
||||
@ -326,4 +336,5 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import static org.apache.commons.csv.Constants.LF;
|
||||
import static org.apache.commons.csv.Constants.TAB;
|
||||
import static org.apache.commons.csv.Constants.UNDEFINED;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
@ -32,7 +33,7 @@ import java.io.IOException;
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
abstract class Lexer {
|
||||
abstract class Lexer implements Closeable {
|
||||
|
||||
/**
|
||||
* Constant char to use for disabling comments, escapes and encapsulation. The value -2 is used because it
|
||||
@ -191,7 +192,15 @@ abstract class Lexer {
|
||||
return c == delimiter ||
|
||||
c == escape ||
|
||||
c == quoteChar ||
|
||||
c == commmentStart
|
||||
;
|
||||
c == commmentStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes resources.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user