Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flushing doesn't work #217

Closed
tomwhoiscontrary opened this issue Mar 26, 2024 · 2 comments
Closed

Flushing doesn't work #217

tomwhoiscontrary opened this issue Mar 26, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@tomwhoiscontrary
Copy link

tomwhoiscontrary commented Mar 26, 2024

I would expect this test to pass:

@Test
void flushesALineToTheUnderlyingWriter() throws IOException {
    StringWriter buf = new StringWriter();

    Csv.Writer csvWriter = Csv.Writer.of(Csv.Format.RFC4180, Csv.WriterOptions.DEFAULT, buf);
    csvWriter.writeField("foo");
    csvWriter.writeField("bar");
    csvWriter.writeField("baz");
    csvWriter.writeEndOfLine();
    csvWriter.flush();

    Assertions.assertEquals("foo,bar,baz\n", buf.toString());
}

But it does not.

To actually flush data to the writer, Csv.Writer.Output::flush must be called. But this is only called when the buffer is full, or the output is closed. In particular, Csv.Writer::flush does not call it.

I would also expect this to pass if the StringWriter was wrapped in a BufferedWriter with a very large buffer. That is, Csv.Writer::flush should call Writer::flush, not just write the contents of the internal buffer.

I have a long-running process which slowly writes to a CSV-formatted file. That file is consumed by other processes. It would be very useful to be able to ensure that lines are actually written to the file long before it is closed.

The workaround is to use a disposable Csv.Writer for each line - closing it at the end of the line flushes the buffer.

@charphi charphi added the bug Something isn't working label Mar 26, 2024
@charphi
Copy link
Member

charphi commented Mar 26, 2024

You are right; that's a bug and it will be fixed in next release.

@charphi
Copy link
Member

charphi commented Mar 26, 2024

Done in 08dc604

@charphi charphi closed this as completed Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants