Skip to content

Commit 0f6bf93

Browse files
committed
add multiple flushing capabilities
1 parent 419778f commit 0f6bf93

16 files changed

+416
-134
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
- Nothing yet
8+
### Added
9+
- Implement `Flushable` interface for `CsvWriter` to allow flushing the underlying writer
10+
- Implement `autoFlush` option for `CsvWriter` to automatically flush the writer after writing a record
11+
- Implement `toConsole` method for `CsvWriter` to write records to the console
912

1013
## [3.2.0] - 2024-06-15
1114
### Added
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package example;
22

3-
import java.io.StringWriter;
4-
53
import de.siegmar.fastcsv.writer.CsvWriter;
64

75
/**
@@ -10,13 +8,10 @@
108
public class ExampleCsvWriterWithComments {
119

1210
public static void main(final String[] args) {
13-
final StringWriter sw = new StringWriter();
14-
CsvWriter.builder().build(sw)
11+
CsvWriter.builder().toConsole()
1512
.writeComment("A comment can be placed\nanywhere")
1613
.writeRecord("field 1", "field 2", "field 3\n#with a line break")
1714
.writeComment("in the CSV file");
18-
19-
System.out.println(sw);
2015
}
2116

2217
}

example/src/main/java/example/ExampleCsvWriterWithDataTransformation.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package example;
22

33
import java.io.IOException;
4-
import java.io.StringWriter;
54

65
import de.siegmar.fastcsv.reader.CsvReader;
76
import de.siegmar.fastcsv.reader.NamedCsvRecord;
@@ -15,10 +14,8 @@ public class ExampleCsvWriterWithDataTransformation {
1514
private static final String DATA = "firstname,initial,lastname,age\njohn,h.,smith";
1615

1716
public static void main(final String[] args) throws IOException {
18-
final StringWriter out = new StringWriter();
19-
2017
try (CsvReader<NamedCsvRecord> reader = CsvReader.builder().ofNamedCsvRecord(DATA);
21-
CsvWriter writer = CsvWriter.builder().build(out)) {
18+
CsvWriter writer = CsvWriter.builder().toConsole()) {
2219

2320
// transform firstname, initial, lastname to lastname, firstname
2421
writer.writeRecord("lastname", "firstname");
@@ -28,8 +25,6 @@ public static void main(final String[] args) throws IOException {
2825
writer.writeRecord(lastname, firstname);
2926
}
3027
}
31-
32-
System.out.println(out);
3328
}
3429

3530
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package example;
22

3-
import java.io.StringWriter;
4-
53
import de.siegmar.fastcsv.writer.CsvWriter;
64
import de.siegmar.fastcsv.writer.LineDelimiter;
75

@@ -11,20 +9,16 @@
119
public class ExampleCsvWriterWithNonStandardControlCharacters {
1210

1311
public static void main(final String[] args) {
14-
final StringWriter sw = new StringWriter();
15-
1612
// The default configuration uses a comma as field separator,
1713
// a double quote as quote character and
1814
// a CRLF as line delimiter.
1915
CsvWriter.builder()
2016
.fieldSeparator(';')
2117
.quoteCharacter('\'')
2218
.lineDelimiter(LineDelimiter.LF)
23-
.build(sw)
19+
.toConsole()
2420
.writeRecord("header1", "header2")
2521
.writeRecord("value1", "value;2");
26-
27-
System.out.println(sw);
2822
}
2923

3024
}

example/src/main/java/example/ExampleCsvWriterWithQuoteStrategy.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package example;
22

3-
import java.io.PrintWriter;
4-
import java.nio.charset.StandardCharsets;
5-
63
import de.siegmar.fastcsv.writer.CsvWriter;
74
import de.siegmar.fastcsv.writer.QuoteStrategies;
85
import de.siegmar.fastcsv.writer.QuoteStrategy;
@@ -13,33 +10,29 @@
1310
public class ExampleCsvWriterWithQuoteStrategy {
1411

1512
public static void main(final String[] args) {
16-
final PrintWriter pw = new PrintWriter(System.out, false, StandardCharsets.UTF_8);
17-
18-
pw.println("Quote always");
13+
System.out.println("Quote always");
1914
CsvWriter.builder()
2015
.quoteStrategy(QuoteStrategies.ALWAYS)
21-
.build(pw)
16+
.toConsole()
2217
.writeRecord("value1", "", null, "value,4");
2318

24-
pw.println("Quote non-empty");
19+
System.out.println("Quote non-empty");
2520
CsvWriter.builder()
2621
.quoteStrategy(QuoteStrategies.NON_EMPTY)
27-
.build(pw)
22+
.toConsole()
2823
.writeRecord("value1", "", null, "value,4");
2924

30-
pw.println("Quote empty");
25+
System.out.println("Quote empty");
3126
CsvWriter.builder()
3227
.quoteStrategy(QuoteStrategies.EMPTY)
33-
.build(pw)
28+
.toConsole()
3429
.writeRecord("value1", "", null, "value,4");
3530

36-
pw.println("Quote custom");
31+
System.out.println("Quote custom");
3732
CsvWriter.builder()
3833
.quoteStrategy(customQuote())
39-
.build(pw)
34+
.toConsole()
4035
.writeRecord("value1", "", null, "value,4");
41-
42-
pw.flush();
4336
}
4437

4538
// A quote strategy can be used to force quote fields that would otherwise not be quoted.
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package example;
22

3-
import java.io.StringWriter;
4-
53
import de.siegmar.fastcsv.writer.CsvWriter;
64

75
/**
@@ -10,12 +8,9 @@
108
public class ExampleCsvWriterWithSingleFields {
119

1210
public static void main(final String[] args) {
13-
final StringWriter sw = new StringWriter();
14-
CsvWriter.builder().build(sw)
11+
CsvWriter.builder().toConsole()
1512
.writeRecord("header1", "header2")
1613
.writeRecord().writeField("value1").writeField("value2").endRecord();
17-
18-
System.out.println(sw);
1914
}
2015

2116
}

example/src/main/java/example/ExampleCsvWriterWithStringOutput.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public static void main(final String[] args) {
1515
.writeRecord("header1", "header2")
1616
.writeRecord("value1", "value2");
1717

18-
System.out.println(sw);
18+
final String csv = sw.toString();
19+
System.out.println(csv);
1920
}
2021

2122
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package blackbox.writer;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
import java.nio.charset.StandardCharsets;
8+
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
import de.siegmar.fastcsv.writer.CsvWriter;
14+
15+
class ConsoleWriterTest {
16+
17+
@SuppressWarnings("checkstyle:RegexpMultiline")
18+
private final PrintStream standardOut = System.out;
19+
private final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();
20+
21+
@BeforeEach
22+
public void setUp() {
23+
System.setOut(new PrintStream(capturedOut, true, StandardCharsets.UTF_8));
24+
}
25+
26+
@AfterEach
27+
public void tearDown() {
28+
System.setOut(standardOut);
29+
}
30+
31+
@Test
32+
void console() {
33+
CsvWriter.builder().toConsole()
34+
.writeRecord("foo", "bar");
35+
36+
assertThat(capturedOut).asString()
37+
.isEqualTo("foo,bar\r\n");
38+
}
39+
40+
}

lib/src/intTest/java/blackbox/writer/CsvWriterTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package blackbox.writer;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatCode;
45
import static org.assertj.core.api.Assertions.assertThatThrownBy;
56

7+
import java.io.FilterWriter;
68
import java.io.IOException;
79
import java.io.StringWriter;
810
import java.io.UncheckedIOException;
11+
import java.nio.file.Files;
912
import java.nio.file.Path;
1013
import java.util.ArrayList;
1114
import java.util.List;
@@ -260,13 +263,49 @@ void disableBuffer() {
260263
assertThat(stringWriter).asString().isEqualTo("foo,bar\n");
261264
}
262265

266+
// autoFlush
267+
268+
@Test
269+
void noAutoFlush() {
270+
final CsvWriter csvWriter = CsvWriter.builder().build(flushFailWriter());
271+
assertThatCode(() -> csvWriter.writeRecord("foo"))
272+
.doesNotThrowAnyException();
273+
}
274+
275+
@Test
276+
void manualFlush(@TempDir final Path tempDir) throws IOException {
277+
final Path file = tempDir.resolve("fastcsv.csv");
278+
CsvWriter.builder().build(file)
279+
.writeRecord("foo")
280+
.flush();
281+
282+
assertThat(Files.readString(file))
283+
.isEqualTo("foo\r\n");
284+
}
285+
286+
@Test
287+
void autoFlush() {
288+
final CsvWriter csvWriter = CsvWriter.builder().autoFlush(true).build(flushFailWriter());
289+
assertThatThrownBy(() -> csvWriter.writeRecord("foo"))
290+
.isInstanceOf(UnsupportedOperationException.class);
291+
}
292+
293+
private static FilterWriter flushFailWriter() {
294+
return new FilterWriter(FilterWriter.nullWriter()) {
295+
@Override
296+
public void flush() {
297+
throw new UnsupportedOperationException();
298+
}
299+
};
300+
}
301+
263302
// toString()
264303

265304
@Test
266305
void builderToString() {
267306
assertThat(crw).asString()
268307
.isEqualTo("CsvWriterBuilder[fieldSeparator=,, quoteCharacter=\", "
269-
+ "commentCharacter=#, quoteStrategy=null, lineDelimiter=\n, bufferSize=8192]");
308+
+ "commentCharacter=#, quoteStrategy=null, lineDelimiter=\n, bufferSize=8192, autoFlush=false]");
270309
}
271310

272311
@Test

0 commit comments

Comments
 (0)