From 29ee5c278237ba3e652a46a6eae5c8cb0d4920e0 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 12 Nov 2020 14:04:26 +0100 Subject: [PATCH] Make CSV writer more robust. --- org.argeo.core/src/org/argeo/util/CsvWriter.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/org.argeo.core/src/org/argeo/util/CsvWriter.java b/org.argeo.core/src/org/argeo/util/CsvWriter.java index a4fe15bfa..c3b3a3ad7 100644 --- a/org.argeo.core/src/org/argeo/util/CsvWriter.java +++ b/org.argeo.core/src/org/argeo/util/CsvWriter.java @@ -59,7 +59,7 @@ 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. */ public CsvWriter(Writer writer) { this.out = writer; @@ -73,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); } @@ -92,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()); } @@ -108,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 -- 2.30.2