This commit is contained in:
BillDowney 2020-04-12 17:12:25 +08:00
commit 59d652954b

View File

@ -171,23 +171,28 @@ public class OutputExecutor implements ShapeExecutor, SpiderListener {
String[] headers = data.keySet().toArray(new String[data.size()]); String[] headers = data.keySet().toArray(new String[data.size()]);
try { try {
if (printer == null) { if (printer == null) {
CSVFormat format = CSVFormat.DEFAULT.withHeader(headers); synchronized (cachePrinter) {
FileOutputStream os = new FileOutputStream(csvName); printer = cachePrinter.get(key);
String csvEncoding = node.getStringJsonValue(CSV_ENCODING); if (printer == null) {
if ("UTF-8BOM".equals(csvEncoding)) { CSVFormat format = CSVFormat.DEFAULT.withHeader(headers);
csvEncoding = csvEncoding.substring(0, 5); FileOutputStream os = new FileOutputStream(csvName);
byte[] bom = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; String csvEncoding = node.getStringJsonValue(CSV_ENCODING);
os.write(bom); if ("UTF-8BOM".equals(csvEncoding)) {
os.flush(); csvEncoding = csvEncoding.substring(0, 5);
byte[] bom = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
os.write(bom);
os.flush();
}
OutputStreamWriter osw = new OutputStreamWriter(os, csvEncoding);
printer = new CSVPrinter(osw, format);
cachePrinter.put(key, printer);
}
} }
OutputStreamWriter osw = new OutputStreamWriter(os, csvEncoding);
printer = new CSVPrinter(osw, format);
cachePrinter.put(key, printer);
} }
for (int i = 0; i < headers.length; i++) { for (int i = 0; i < headers.length; i++) {
records.add(data.get(headers[i]).toString()); records.add(data.get(headers[i]).toString());
} }
synchronized (printer) { synchronized (cachePrinter) {
printer.printRecord(records); printer.printRecord(records);
} }
} catch (IOException e) { } catch (IOException e) {