com.github.skjolber.unzip.csv.DefaultUnivocityCsvFileEntryHandler Maven / Gradle / Ivy
package com.github.skjolber.unzip.csv;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import com.github.skjolber.unzip.ChunkedFileEntryHandler;
import com.github.skjolber.unzip.FileChunkSplitter;
import com.github.skjolber.unzip.FileEntryChunkStreamHandler;
import com.github.skjolber.unzip.FileEntryHandler;
import com.github.skjolber.unzip.FileEntryStreamHandler;
import com.github.skjolber.unzip.NewlineChunkSplitter;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
/**
*
* CSV {@linkplain FileEntryHandler}. Expects that the header can be extracted from the first input, if not already specified.
*
*/
public class DefaultUnivocityCsvFileEntryHandler implements ChunkedFileEntryHandler {
protected class CsvFileEntryStreamHandler implements FileEntryStreamHandler {
protected final String name;
public CsvFileEntryStreamHandler(String name) {
super();
this.name = name;
}
@Override
public void handle(InputStream in, ThreadPoolExecutor executor, boolean consume) throws Exception {
CsvParser reader = createCsvParser(in);
String[] header = reader.parseNext();
for(int i = 0; i < header.length; i++) {
if(header[i] != null && header[i].trim().isEmpty()) {
header[i] = null;
}
}
CsvLineHandler