Improve CSV parser and writer.
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 28 Oct 2020 12:05:54 +0000 (13:05 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 28 Oct 2020 12:05:54 +0000 (13:05 +0100)
org.argeo.util/src/org/argeo/util/CsvParser.java
org.argeo.util/src/org/argeo/util/CsvWriter.java

index 2774170595f3b8b46c9cb6ec8e140c1e7f35baf4..b903f77226457bbab9ded55926899677e98d34f2 100644 (file)
@@ -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<String> 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<String> tokens, StringBuffer currStr, Boolean wasInquote) {
-               // List<String> tokens = new ArrayList<String>();
-
-               // 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
index 97b5191035d83e41d1402b0eff0ff0595b74f03c..41ea65dd4903a88af162c640475160929d2c5210 100644 (file)
@@ -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.