From: Mathieu Baudier Date: Wed, 28 Oct 2020 12:05:54 +0000 (+0100) Subject: Improve CSV parser and writer. X-Git-Tag: argeo-commons-2.1.89~49 X-Git-Url: http://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=1ceeaa36a22d5bcb9ad3c577e2e729b1ff31ae2c Improve CSV parser and writer. --- diff --git a/org.argeo.util/src/org/argeo/util/CsvParser.java b/org.argeo.util/src/org/argeo/util/CsvParser.java index 277417059..b903f7722 100644 --- a/org.argeo.util/src/org/argeo/util/CsvParser.java +++ b/org.argeo.util/src/org/argeo/util/CsvParser.java @@ -38,14 +38,25 @@ public abstract class CsvParser { /** * Parses the CSV file (stream is closed at the end) + * + * @param in the stream to parse + * + * @deprecated Use {@link #parse(InputStream, Charset)} instead. */ + @Deprecated public synchronized void parse(InputStream in) { parse(in, (Charset) null); } /** * Parses the CSV file (stream is closed at the end) + * + * @param in the stream to parse + * @param encoding the encoding to use. + * + * @deprecated Use {@link #parse(InputStream, Charset)} instead. */ + @Deprecated public synchronized void parse(InputStream in, String encoding) { Reader reader; if (encoding == null) @@ -61,6 +72,9 @@ public abstract class CsvParser { /** * Parses the CSV file (stream is closed at the end) + * + * @param in the stream to parse + * @param charset the charset to use */ public synchronized void parse(InputStream in, Charset charset) { Reader reader; @@ -73,10 +87,12 @@ public abstract class CsvParser { /** * Parses the CSV file (stream is closed at the end) + * + * @param reader the reader to use (it will be buffered) */ - public synchronized void parse(Reader r) { + public synchronized void parse(Reader reader) { Integer lineCount = 0; - try (BufferedReader bufferedReader = new BufferedReader(r)) { + try (BufferedReader bufferedReader = new BufferedReader(reader)) { List header = null; if (!noHeader) { String headerStr = bufferedReader.readLine(); @@ -145,23 +161,19 @@ public abstract class CsvParser { * @return whether to continue parsing this line */ protected Boolean parseLine(String str, List tokens, StringBuffer currStr, Boolean wasInquote) { - // List tokens = new ArrayList(); - - // System.out.println("#LINE: " + str); - if (wasInquote) currStr.append('\n'); char[] arr = str.toCharArray(); boolean inQuote = wasInquote; - // StringBuffer currStr = new StringBuffer(""); for (int i = 0; i < arr.length; i++) { char c = arr[i]; if (c == separator) { if (!inQuote) { tokens.add(currStr.toString()); - // System.out.println("# TOKEN: " + currStr); - currStr.delete(0, currStr.length()); +// currStr.delete(0, currStr.length()); + currStr.setLength(0); + currStr.trimToSize(); } else { // we don't remove separator that are in a quoted substring // System.out diff --git a/org.argeo.util/src/org/argeo/util/CsvWriter.java b/org.argeo.util/src/org/argeo/util/CsvWriter.java index 97b519103..41ea65dd4 100644 --- a/org.argeo.util/src/org/argeo/util/CsvWriter.java +++ b/org.argeo.util/src/org/argeo/util/CsvWriter.java @@ -5,6 +5,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; @@ -19,7 +20,11 @@ public class CsvWriter { * Creates a CSV writer. * * @param out the stream to write to. Caller is responsible for closing it. + * + * @deprecated Use {@link #CsvWriter(OutputStream, Charset)} instead. + * */ + @Deprecated public CsvWriter(OutputStream out) { this.out = new OutputStreamWriter(out); } @@ -27,8 +32,12 @@ public class CsvWriter { /** * Creates a CSV writer. * - * @param out the stream to write to. Caller is responsible for closing it. + * @param out the stream to write to. Caller is responsible for closing it. + * @param encoding the encoding to use. + * + * @deprecated Use {@link #CsvWriter(OutputStream, Charset)} instead. */ + @Deprecated public CsvWriter(OutputStream out, String encoding) { try { this.out = new OutputStreamWriter(out, encoding); @@ -37,6 +46,16 @@ public class CsvWriter { } } + /** + * Creates a CSV writer. + * + * @param out the stream to write to. Caller is responsible for closing it. + * @param charset the charset to use + */ + public CsvWriter(OutputStream out, Charset charset) { + this.out = new OutputStreamWriter(out, charset); + } + /** * Write a CSV line. Also used to write a header if needed (this is transparent * for the CSV writer): simply call it first, before writing the lines.