X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FCsvWriter.java;h=c3b3a3ad7b43afa5989871fcaf0f223b1259a763;hb=00baea7c5c7bdddf6dd5aa1fae8edaf2ed7a6400;hp=97b5191035d83e41d1402b0eff0ff0595b74f03c;hpb=986233dff943b24e545caecaa4658639c36172eb;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/CsvWriter.java b/org.argeo.util/src/org/argeo/util/CsvWriter.java index 97b519103..c3b3a3ad7 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,25 @@ 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); + } + + /** + * Creates a CSV writer. + * + * @param out the stream to write to. Caller is responsible for closing it. + */ + public CsvWriter(Writer writer) { + this.out = writer; + } + /** * 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. @@ -45,7 +73,8 @@ public class CsvWriter { try { Iterator it = tokens.iterator(); while (it.hasNext()) { - writeToken(it.next().toString()); + Object obj = it.next(); + writeToken(obj != null ? obj.toString() : null); if (it.hasNext()) out.write(separator); } @@ -64,8 +93,7 @@ public class CsvWriter { try { for (int i = 0; i < tokens.length; i++) { if (tokens[i] == null) { - // TODO configure how to deal with null - writeToken(""); + writeToken(null); } else { writeToken(tokens[i].toString()); } @@ -80,6 +108,11 @@ public class CsvWriter { } protected void writeToken(String token) throws IOException { + if (token == null) { + // TODO configure how to deal with null + out.write(""); + return; + } // +2 for possible quotes, another +2 assuming there would be an already // quoted string where quotes needs to be duplicated // another +2 for safety