Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converting nulls to blank strings when doing CSV parsing #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/main/java/com/sforce/dataset/loader/WriterThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,11 +61,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<String> returnValue = new ArrayList<String>();

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
{
Expand All @@ -91,7 +111,7 @@ public void run() {
}
//t.printStackTrace();
}
row = queue.take();
row = convertNullToEmptyString(queue.take());
}
}catch (Throwable t) {
logger.println (Thread.currentThread().getName() + " " + t.getMessage());
Expand Down