in c3r-sdk-core/src/main/java/com/amazonaws/c3r/io/CsvRowWriter.java [56:93]
private CsvRowWriter(
@NonNull final String targetName,
final String outputNullValue,
@NonNull final List<ColumnHeader> headers,
final Charset fileCharset) {
this.targetName = targetName;
final CsvWriterSettings writerSettings = new CsvWriterSettings();
this.headers = new ArrayList<>(headers);
writerSettings.setHeaders(this.headers.stream().map(ColumnHeader::toString).toArray(String[]::new));
// encode NULL as the user requests, or `,,` if not specified
writerSettings.setNullValue(Objects.requireNonNullElse(outputNullValue, ""));
if (outputNullValue == null || outputNullValue.isBlank()) {
// If NULL is being encoded as a blank, then use an empty string
// encoding which can be distinguished if needed.
writerSettings.setEmptyValue("\"\"");
} else {
// otherwise just write it out as the empty string (i.e., without quotes)
// like any other quoted values without whitespace inside
writerSettings.setEmptyValue("");
}
// Err on the side of safety w.r.t. quoting parsed content that contains any whitespace
// by not trimming said content _and_ by adding quotes if any whitespace characters are seen
// after parsing.
writerSettings.trimValues(false);
writerSettings.setQuotationTriggers(' ', '\t', '\n', '\r', '\f');
try {
writer = new CsvWriter(
new OutputStreamWriter(
new FileOutputStream(targetName),
Objects.requireNonNullElse(fileCharset, StandardCharsets.UTF_8)),
writerSettings);
} catch (FileNotFoundException e) {
throw new C3rRuntimeException("Unable to write to output CSV file " + targetName + ".", e);
}
writer.writeHeaders();
}