From 7f5853b50212f8f062a683dc590025d23e195449 Mon Sep 17 00:00:00 2001 From: Josh Meier Date: Wed, 21 Jan 2015 14:41:49 -0800 Subject: [PATCH] adding null checks to csv parsing and replacing nulls with empty strings: --- .../sforce/dataset/loader/WriterThread.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sforce/dataset/loader/WriterThread.java b/src/main/java/com/sforce/dataset/loader/WriterThread.java index fad2983..8f93f7e 100644 --- a/src/main/java/com/sforce/dataset/loader/WriterThread.java +++ b/src/main/java/com/sforce/dataset/loader/WriterThread.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.PrintStream; +import java.util.ArrayList; import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; @@ -58,11 +59,30 @@ public class WriterThread implements Runnable { this.logger = logger; } +/** + * Converts nulls to empty strings inside a string array so + * the csv encoder doesn't freak out when we run into a null + * + * @param input A String array + * @return a String array with no nulls + */ +private String[] convertNullToEmptyString(String[] input) { + ArrayList returnValue = new ArrayList(); + + for(String s : input) { + returnValue.add(s == null ? "" : s); + } + + return returnValue.toArray(new String[returnValue.size()]); +} + @SuppressWarnings("deprecation") public void run() { logger.println("Start: " + Thread.currentThread().getName()); try { - String[] row = queue.take(); + // The CSV encoder doesn't handle null values well so + // we need to swap them out with empty strings + String[] row = convertNullToEmptyString(queue.take()); while (row != null && row.length!=0) { try { @@ -87,7 +107,7 @@ public void run() { } //t.printStackTrace(); } - row = queue.take(); + row = convertNullToEmptyString(queue.take()); } }catch (Throwable t) { logger.println (Thread.currentThread().getName() + " " + t.getMessage());