private CsvParser create(String content) { CsvConfiguration conf = createConfiguration(); return new CsvParser( new ByteArrayInputStream(content.getBytes(conf.getCharset())), testName.getMethodName(), conf); }
/** * Creates a new instance. * @param stream the source stream * @param path the source path * @param config current configuration * @throws IllegalArgumentException if some parameters were {@code null} */ public CsvParser(InputStream stream, String path, CsvConfiguration config) { if (stream == null) { throw new IllegalArgumentException("stream must not be null"); //$NON-NLS-1$ } if (config == null) { throw new IllegalArgumentException("config must not be null"); //$NON-NLS-1$ } this.reader = new InputStreamReader(stream, config.getCharset()); this.path = path; this.separator = config.getSeparatorChar(); this.trueFormat = config.getTrueFormat(); this.dateFormat = DateFormatter.newInstance(config.getDateFormat()); this.dateTimeFormat = DateTimeFormatter.newInstance(config.getDateTimeFormat()); this.headerCellsFormat = config.getHeaderCells(); this.forceConsumeHeader = config.isForceConsumeHeader(); this.allowLineBreakInValue = config.isLineBreakInValue(); readerBuffer.clear(); readerBuffer.flip(); }
private String[][] parse(int columns, String string) { CsvConfiguration conf = new CsvConfiguration( CsvConfiguration.DEFAULT_CHARSET, CsvConfiguration.DEFAULT_HEADER_CELLS, CsvConfiguration.DEFAULT_TRUE_FORMAT, CsvConfiguration.DEFAULT_FALSE_FORMAT, CsvConfiguration.DEFAULT_DATE_FORMAT, CsvConfiguration.DEFAULT_DATE_TIME_FORMAT); List<String[]> results = new ArrayList<>(); ByteArrayInputStream input = new ByteArrayInputStream(string.getBytes(conf.getCharset())); try (CsvParser parser = new CsvParser(input, string, conf)) { StringOption buffer = new StringOption(); while (parser.next()) { String[] line = new String[columns]; for (int i = 0; i < columns; i++) { parser.fill(buffer); line[i] = buffer.or((String) null); } parser.endRecord(); results.add(line); } } catch (Exception e) { throw new AssertionError(e); } return results.toArray(new String[results.size()][]); }
/** * Creates a new instance. * @param stream the target stream * @param path the destination path * @param config current configuration * @throws IllegalArgumentException if some parameters were {@code null} */ public CsvEmitter(OutputStream stream, String path, CsvConfiguration config) { if (stream == null) { throw new IllegalArgumentException("stream must not be null"); //$NON-NLS-1$ } if (config == null) { throw new IllegalArgumentException("config must not be null"); //$NON-NLS-1$ } this.writer = new OutputStreamWriter(stream, config.getCharset()); this.separator = config.getSeparatorChar(); this.escapePattern = Pattern.compile( "[" + ESCAPE + separator + LINE_DELIMITER + "]"); //$NON-NLS-1$ //$NON-NLS-2$ this.trueFormat = escape(config.getTrueFormat()); this.escapeTrue = hasEscapeTarget(config.getTrueFormat()); this.falseFormat = escape(config.getFalseFormat()); this.escapeFalse = hasEscapeTarget(config.getFalseFormat()); this.dateFormat = DateFormatter.newInstance(config.getDateFormat()); this.escapeDate = hasMetaCharacter(dateFormat.getPattern()); this.dateTimeFormat = DateTimeFormatter.newInstance(config.getDateTimeFormat()); this.escapeDateTime = hasMetaCharacter(dateTimeFormat.getPattern()); this.headerCellsFormat = config.getHeaderCells(); this.forceEscapeMask = buildQuoteMask(config.getForceQuoteColumns()); this.columnIndex = 0; }
/** * Simple stress test for {@link DecimalOption} type. * @throws Exception if failed */ @Test public void stress_decimal() throws Exception { int count = 5000000; CsvConfiguration conf = createConfiguration(); try (RCReader reader = new RCReader("3.141592\r\n".getBytes(conf.getCharset()), count); CsvParser parser = new CsvParser(reader, "testing", conf)) { int rows = 0; DecimalOption date = new DecimalOption(); while (parser.next()) { parser.fill(date); parser.endRecord(); if (rows == 0) { assertThat(date, is(new DecimalOption(new BigDecimal("3.141592")))); } rows++; } assertThat(rows, is(count)); } }
/** * Simple stress test for {@link Date} type. * @throws Exception if failed */ @Test public void stress_date() throws Exception { int count = 5000000; CsvConfiguration conf = createConfiguration(); try (RCReader reader = new RCReader("1999-12-31\r\n".getBytes(conf.getCharset()), count); CsvParser parser = new CsvParser(reader, "testing", conf)) { int rows = 0; DateOption date = new DateOption(); while (parser.next()) { parser.fill(date); parser.endRecord(); if (rows == 0) { assertThat(date, is(new DateOption(new Date(1999, 12, 31)))); } rows++; } parser.close(); assertThat(rows, is(count)); } }
/** * Simple stress test for {@link DateTime} type. * @throws Exception if failed */ @Test public void stress_datetime() throws Exception { int count = 5000000; CsvConfiguration conf = createConfiguration(); try (RCReader reader = new RCReader("1999-12-31 01:23:45\r\n".getBytes(conf.getCharset()), count); CsvParser parser = new CsvParser(reader, "testing", conf)) { int rows = 0; DateTimeOption date = new DateTimeOption(); while (parser.next()) { parser.fill(date); parser.endRecord(); if (rows == 0) { assertThat(date, is(new DateTimeOption(new DateTime(1999, 12, 31, 1, 23, 45)))); } rows++; } parser.close(); assertThat(rows, is(count)); } }