@Override public ByteBuffer write(List<Object> data, ByteBuffer buffer) { try { StringWriter writer = new StringWriter(); CSVPrinter printer = new CSVPrinter(writer, CSVFormat.RFC4180); for (Object o : data) { printer.print(o); } //since using StringWriter, we do not need to close it. return ByteBuffer.wrap(writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException(e); } } }
public void setOutput(PrintStream output){ final OutputStreamWriter streamWriter = new OutputStreamWriter(output); try { printer = new CSVPrinter(streamWriter, csvFormat); } catch (IOException e) { throw new ProcessException("Failed to create CSV Printer.", e); } }
private static String doJoinDelimited(Iterable<?> elements, CSVFormat format) { StringWriter out = new StringWriter(); try (CSVPrinter printer = new CSVPrinter(out, format)) { for (Object element : elements) { printer.print(element); } printer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return out.toString(); }
@Override public byte[] serialize(final String topic, final GenericRow genericRow) { if (genericRow == null) { return null; } try { final StringWriter stringWriter = new StringWriter(); final CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSVFormat.DEFAULT); csvPrinter.printRecord(genericRow.getColumns()); final String result = stringWriter.toString(); return result.substring(0, result.length() - 2).getBytes(StandardCharsets.UTF_8); } catch (final Exception e) { throw new SerializationException("Error serializing CSV message", e); } }
public WriteCSVResult(final CSVFormat csvFormat, final RecordSchema recordSchema, final SchemaAccessWriter schemaWriter, final OutputStream out, final String dateFormat, final String timeFormat, final String timestampFormat, final boolean includeHeaderLine, final String charSet) throws IOException { super(out); this.recordSchema = recordSchema; this.schemaWriter = schemaWriter; this.dateFormat = dateFormat; this.timeFormat = timeFormat; this.timestampFormat = timestampFormat; this.includeHeaderLine = includeHeaderLine; final CSVFormat formatWithHeader = csvFormat.withSkipHeaderRecord(true); final OutputStreamWriter streamWriter = new OutputStreamWriter(out, charSet); printer = new CSVPrinter(streamWriter, formatWithHeader); fieldValues = new Object[recordSchema.getFieldCount()]; }
@BeforeClass public void setUp() throws Exception { FileUtils.forceMkdir(TEMP_DIR); try (FileWriter fileWriter = new FileWriter(DATA_FILE); CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader(COLUMNS))) { for (Object[] record : RECORDS) { csvPrinter.printRecord(record[0], StringUtils.join((int[]) record[1], CSVRecordReaderConfig.DEFAULT_MULTI_VALUE_DELIMITER)); } } }
@Override protected void open(String header) throws IOException { // Check if already so we only open one writer if (csvPrinter != null) { return; } csvPrinter = new CSVPrinter(new PrintWriter(resultFileName), CSVFormat.DEFAULT); Object[] records = header.split(PherfConstants.RESULT_FILE_DELIMETER); csvPrinter.printRecord(records); isClosed = false; } }
@Test public void testCloseWithCsvFormatAutoFlushOn() throws IOException { // System.out.println("start method"); try (final Writer writer = mock(Writer.class)) { final CSVFormat csvFormat = CSVFormat.DEFAULT.withAutoFlush(true); try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) { } verify(writer, times(1)).flush(); verify(writer, times(1)).close(); }}
@Test public void testPrinter1() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { printer.printRecord("a", "b"); assertEquals("a,b" + recordSeparator, sw.toString()); } }
@Test public void testDontQuoteEuroFirstChar() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) { printer.printRecord(EURO_CH, "Deux"); assertEquals(EURO_CH + ",Deux" + recordSeparator, sw.toString()); } }
@Test public void testPrintNullValues() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { printer.printRecord("a", null, "b"); assertEquals("a,,b" + recordSeparator, sw.toString()); } }
@Test public void testQuoteNonNumeric() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC))) { printer.printRecord("a", "b\nc", Integer.valueOf(1)); assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString()); } }
@Test public void testEscapeBackslash4() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(QUOTE_CH))) { printer.print("\\\\"); } assertEquals("\\\\", sw.toString()); }
@Test public void testEscapeNull4() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) { printer.print("\\\\"); } assertEquals("\\\\", sw.toString()); }
@Test public void testEscapeNull5() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) { printer.print("\\\\"); } assertEquals("\\\\", sw.toString()); }
@Test public void testTrimOnOneColumn() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim())) { printer.print(" A "); assertEquals("A", sw.toString()); } }
@Test public void testSingleLineComment() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentMarker('#'))) { printer.printComment("This is a comment"); assertEquals("# This is a comment" + recordSeparator, sw.toString()); } }
@Test public void testEOLQuoted() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote('\''))) { printer.print("a\rb\nc"); printer.print("x\by\fz"); assertEquals("'a\rb\nc',x\by\fz", sw.toString()); } }
@Test public void testDelimeterQuoteNONE() throws IOException { final StringWriter sw = new StringWriter(); final CSVFormat format = CSVFormat.DEFAULT.withEscape('!').withQuoteMode(QuoteMode.NONE); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { printer.print("a,b,c"); printer.print("xyz"); assertEquals("a!,b!,c,xyz", sw.toString()); } }
@Test public void testHeader() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3"))) { printer.printRecord("a", "b", "c"); printer.printRecord("x", "y", "z"); assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString()); } }