Skip to content

Commit

Permalink
Fix writer flushing
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Mar 26, 2024
1 parent 88223fe commit 08dc604
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix writer flushing [#217](https://github.com/nbbrd/picocsv/issues/217)

## [2.3.0] - 2024-03-12

This is a feature release of **picocsv**.
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/nbbrd/picocsv/Csv.java
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ public void flush() throws IOException {
output.write(quote);
}
state = STATE_NO_FIELD;
output.flush();
}

/**
Expand Down Expand Up @@ -1349,7 +1350,7 @@ private int getQuoting(CharSequence field) {
private static final int STATE_SINGLE_EMPTY_FIELD = 1;
private static final int STATE_MULTI_FIELD = 2;

private static final class Output implements Closeable {
private static final class Output implements Closeable, Flushable {

private final java.io.Writer charWriter;
private final char[] buffer;
Expand All @@ -1363,15 +1364,15 @@ private Output(java.io.Writer charWriter, char[] charBuffer) {

public void write(char c) throws IOException {
if (length == buffer.length) {
flush();
flushBuffer();
}
buffer[length++] = c;
}

public void write(CharSequence chars) throws IOException {
int charsLength = chars.length();
if (length + charsLength >= buffer.length) {
flush();
flushBuffer();
if (charsLength >= buffer.length) {
charWriter.append(chars);
return;
Expand All @@ -1389,14 +1390,22 @@ public void write(CharSequence chars) throws IOException {

@Override
public void close() throws IOException {
flush();
flushBuffer();
charWriter.close();
}

private void flush() throws IOException {
private void flushBuffer() throws IOException {
charWriter.write(buffer, 0, length);
length = 0;
}

@Override
public void flush() throws IOException {
if (length != 0) {
flushBuffer();
}
charWriter.flush();
}
}

private static abstract class EndOfLineEncoder {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/nbbrd/picocsv/CsvWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ public void testOutputBuffer() throws IOException {
), Csv.WriterOptions.DEFAULT);
}

@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();

assertThat(buf.toString()).isEqualTo("foo,bar,baz\r\n");
}

private static Sample getOverflowSample(String... fields) {
return Sample
.builder()
Expand Down

0 comments on commit 08dc604

Please sign in to comment.