Menu

#61 Blocking on CsvToBean.iterator() if multiple errors on first line

Unstable (example)
closed
None
5
2021-03-16
2020-11-24
Marthym
No

When I create a CsvToBean iterator, with throw exception to false, if the first line of CSV file as more than one error, the CsvToBean.iterator() method stay blocked.

In CsvToBeanIterator the capacity of the thrownExceptionsQueue is 1. So, error must be consumed before the next error. But if you choose to avoid throw in order to get errors in other way, the second error of the line wait the first was consumed. The first was never consumed because invocation of
CsvToBean.iterator() was not finished.

Note that if you choose to throw exceptions, the throw append during iterator creation, not in the next call.

The only workaround I have is to override ExceptionHandler and discard all error after the first on the same line.

AtomicLong line = new AtomicLong(-1);
CsvToBean<MyBean> csvToBean = new CsvToBeanBuilder<MyBean>(inputStreamReader)
                .withMappingStrategy(strategy)
                .withIgnoreLeadingWhiteSpace(true)
                .withSeparator(CSV_SEPARATOR)
                .withType(CsvReview.class)
                .withThrowExceptions(false)
                .withExceptionHandler(e -> {
                    // Workaround
                    if (e.getLineNumber() > line.getAndSet(e.getLineNumber())) {
                        return e;
                    } else {
                        return null;
                    }
                })
                .build();
final Iterator<MyBean> csvReviewIterator = csvToBean.iterator();

Thanks for you attention
Frédéric

Discussion

  • Marthym

    Marthym - 2020-11-24

    I use version 5.3

     
  • Andrew Rucker Jones

    • assigned_to: Andrew Rucker Jones
     
  • Andrew Rucker Jones

    Oops. Sorry. I'll take a look at it.

     
  • Andrew Rucker Jones

    • status: open --> pending
     
  • Andrew Rucker Jones

    Fixed in the next version of opencsv.

     
  • Marthym

    Marthym - 2021-03-01

    Thx 👍

     
  • Andrew Rucker Jones

    • status: pending --> closed
     
  • Andrew Rucker Jones

    5.4 was released a couple of days ago. Sorry to leave you hanging like that.

     

Log in to post a comment.

Auth0 Logo