Skip to content

CsvBeanReader.read() does not reliably detect EOF #48

@atnak

Description

@atnak

CsvBeanReader.read() is documented to return null after the last item.

Instead, it fails attempting to parse an invalid line.

BeanListHandler.load() also suffers from this problem.

How to reproduce

public static class Foo {
    int x;
    public int getX() { return x; }
    public void setX(int x) { this.x = x; }
}

{
    Path file = Files.createTempFile("bean_test", ".csv");

    try (CsvBeanWriter<Foo> beanWriter = CsvBeanWriter.newInstance(
            new CsvWriter(Files.newBufferedWriter(file)), Foo.class)) {
        beanWriter.write(new Foo());
    }

    try (CsvBeanReader<Foo> beanReader = CsvBeanReader.newInstance(
            new CsvReader(Files.newBufferedReader(file)), Foo.class)) {
        System.out.println(beanReader.read().getX());
        System.out.println(beanReader.read());    # java.lang.NumberFormatException: For input string: ""
    }
}

Possible workaround

for (;;) {
    List<String> values = beanReader.readValues());
    if (values == null) {
        break; // EOF
    }

    if (values.size() == 1 && values.get(0).isEmpty()) {
        continue; // empty line
    }
    beanReader.toBean(values);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions