private void seekBuffer() throws CsvFormatException { if (cellBeginPositions.remaining() < 2) { throw new CsvFormatException(new Status( Reason.TOO_SHORT_RECORD, path, currentPhysicalHeadLine, currentRecordNumber, cellBeginPositions.position() + 1, "more cells", "no more cells"), null); } lineBuffer.limit(cellBeginPositions.get(cellBeginPositions.position() + 1)); lineBuffer.position(cellBeginPositions.get()); }
/** * Creates a new instance. * @param status describes this exception * @param cause the original cause (nullable) * @throws IllegalArgumentException if some parameters were {@code null} */ public CsvFormatException(Status status, Throwable cause) { super(toMessage(status), cause); this.status = status; }
/** * test for invalid short values. * @throws Exception if failed */ @Test public void invalid_short() throws Exception { CsvParser parser = create(String.valueOf(Short.MAX_VALUE + 1)); assertThat(parser.next(), is(true)); try { parser.fill(new ShortOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid float values. * @throws Exception if failed */ @Test public void invalid_float() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new FloatOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid int values. * @throws Exception if failed */ @Test public void invalid_int() throws Exception { CsvParser parser = create(String.valueOf((long) Integer.MAX_VALUE + 1)); assertThat(parser.next(), is(true)); try { parser.fill(new IntOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@Override public void endRecord() throws CsvFormatException, IOException { if (cellBeginPositions.remaining() > 1) { seekBuffer(); throw new CsvFormatException(new Status( Reason.TOO_LONG_RECORD, path, currentPhysicalHeadLine, currentRecordNumber, cellBeginPositions.position(), CHAR_END_OF_RECORD, lineBuffer.toString()), null); } }
/** * test for invalid decimal values. * @throws Exception if failed */ @Test public void invalid_decimal() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DecimalOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@SuppressWarnings("deprecation") private void fill0(ShortOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { short value = Short.parseShort(lineBuffer.toString()); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "short value"), e); } } else { option.setNull(); } }
/** * test for invalid long values. * @throws Exception if failed */ @Test public void invalid_long() throws Exception { CsvParser parser = create(String.valueOf(Long.MAX_VALUE + "0")); assertThat(parser.next(), is(true)); try { parser.fill(new LongOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@SuppressWarnings("deprecation") private void fill0(LongOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { long value = Long.parseLong(lineBuffer.toString()); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "long value"), e); } } else { option.setNull(); } }
/** * test for invalid datetime values. * @throws Exception if failed */ @Test public void invalid_datetime() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DateTimeOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@SuppressWarnings("deprecation") private void fill0(FloatOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { float value = Float.parseFloat(lineBuffer.toString()); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "float value"), e); } } else { option.setNull(); } }
/** * test for invalid bytes. * @throws Exception if failed */ @Test public void invalid_byte() throws Exception { CsvParser parser = create(String.valueOf(Byte.MAX_VALUE + 1)); assertThat(parser.next(), is(true)); try { parser.fill(new ByteOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@SuppressWarnings("deprecation") private void fill0(ByteOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { byte value = Byte.parseByte(lineBuffer.toString()); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "byte value"), e); } } else { option.setNull(); } }
/** * test for invalid double values. * @throws Exception if failed */ @Test public void invalid_double() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DoubleOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@SuppressWarnings("deprecation") private void fill0(IntOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { int value = Integer.parseInt(lineBuffer.toString()); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "int value"), e); } } else { option.setNull(); } }
/** * test for invalid date values. * @throws Exception if failed */ @Test public void invalid_date() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DateOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
@SuppressWarnings("deprecation") private void fill0(DoubleOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { double value = Double.parseDouble(lineBuffer.toString()); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "double value"), e); } } else { option.setNull(); } }
/** * record is too long. * @throws Exception if failed */ @Test public void too_long_record() throws Exception { CsvParser parser = create("a,b,c"); assertThat(parser.next(), is(true)); try { assertFill(parser, "a"); assertFill(parser, "b"); parser.endRecord(); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.TOO_LONG_RECORD)); } assertThat(parser.next(), is(false)); }
@SuppressWarnings("deprecation") private void fill0(DecimalOption option, boolean doRecover) throws CsvFormatException { if (lineBuffer.hasRemaining()) { try { BigDecimal value = toBigDecimal(); option.modify(value); } catch (NumberFormatException e) { if (doRecover && trimWhitespaces()) { fill0(option, false); return; } throw new CsvFormatException(createStatusInLine(Reason.INVALID_CELL_FORMAT, "decimal value"), e); } } else { option.setNull(); } }