Skip to content

Commit

Permalink
add multiple flushing capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Sep 19, 2024
1 parent 419778f commit 0f6bf93
Show file tree
Hide file tree
Showing 16 changed files with 416 additions and 134 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

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

## [3.2.0] - 2024-06-15
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package example;

import java.io.StringWriter;

import de.siegmar.fastcsv.writer.CsvWriter;

/**
Expand All @@ -10,13 +8,10 @@
public class ExampleCsvWriterWithComments {

public static void main(final String[] args) {
final StringWriter sw = new StringWriter();
CsvWriter.builder().build(sw)
CsvWriter.builder().toConsole()
.writeComment("A comment can be placed\nanywhere")
.writeRecord("field 1", "field 2", "field 3\n#with a line break")
.writeComment("in the CSV file");

System.out.println(sw);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package example;

import java.io.IOException;
import java.io.StringWriter;

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

public static void main(final String[] args) throws IOException {
final StringWriter out = new StringWriter();

try (CsvReader<NamedCsvRecord> reader = CsvReader.builder().ofNamedCsvRecord(DATA);
CsvWriter writer = CsvWriter.builder().build(out)) {
CsvWriter writer = CsvWriter.builder().toConsole()) {

// transform firstname, initial, lastname to lastname, firstname
writer.writeRecord("lastname", "firstname");
Expand All @@ -28,8 +25,6 @@ public static void main(final String[] args) throws IOException {
writer.writeRecord(lastname, firstname);
}
}

System.out.println(out);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package example;

import java.io.StringWriter;

import de.siegmar.fastcsv.writer.CsvWriter;
import de.siegmar.fastcsv.writer.LineDelimiter;

Expand All @@ -11,20 +9,16 @@
public class ExampleCsvWriterWithNonStandardControlCharacters {

public static void main(final String[] args) {
final StringWriter sw = new StringWriter();

// The default configuration uses a comma as field separator,
// a double quote as quote character and
// a CRLF as line delimiter.
CsvWriter.builder()
.fieldSeparator(';')
.quoteCharacter('\'')
.lineDelimiter(LineDelimiter.LF)
.build(sw)
.toConsole()
.writeRecord("header1", "header2")
.writeRecord("value1", "value;2");

System.out.println(sw);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package example;

import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import de.siegmar.fastcsv.writer.CsvWriter;
import de.siegmar.fastcsv.writer.QuoteStrategies;
import de.siegmar.fastcsv.writer.QuoteStrategy;
Expand All @@ -13,33 +10,29 @@
public class ExampleCsvWriterWithQuoteStrategy {

public static void main(final String[] args) {
final PrintWriter pw = new PrintWriter(System.out, false, StandardCharsets.UTF_8);

pw.println("Quote always");
System.out.println("Quote always");
CsvWriter.builder()
.quoteStrategy(QuoteStrategies.ALWAYS)
.build(pw)
.toConsole()
.writeRecord("value1", "", null, "value,4");

pw.println("Quote non-empty");
System.out.println("Quote non-empty");
CsvWriter.builder()
.quoteStrategy(QuoteStrategies.NON_EMPTY)
.build(pw)
.toConsole()
.writeRecord("value1", "", null, "value,4");

pw.println("Quote empty");
System.out.println("Quote empty");
CsvWriter.builder()
.quoteStrategy(QuoteStrategies.EMPTY)
.build(pw)
.toConsole()
.writeRecord("value1", "", null, "value,4");

pw.println("Quote custom");
System.out.println("Quote custom");
CsvWriter.builder()
.quoteStrategy(customQuote())
.build(pw)
.toConsole()
.writeRecord("value1", "", null, "value,4");

pw.flush();
}

// A quote strategy can be used to force quote fields that would otherwise not be quoted.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package example;

import java.io.StringWriter;

import de.siegmar.fastcsv.writer.CsvWriter;

/**
Expand All @@ -10,12 +8,9 @@
public class ExampleCsvWriterWithSingleFields {

public static void main(final String[] args) {
final StringWriter sw = new StringWriter();
CsvWriter.builder().build(sw)
CsvWriter.builder().toConsole()
.writeRecord("header1", "header2")
.writeRecord().writeField("value1").writeField("value2").endRecord();

System.out.println(sw);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static void main(final String[] args) {
.writeRecord("header1", "header2")
.writeRecord("value1", "value2");

System.out.println(sw);
final String csv = sw.toString();
System.out.println(csv);
}

}
40 changes: 40 additions & 0 deletions lib/src/intTest/java/blackbox/writer/ConsoleWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package blackbox.writer;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import de.siegmar.fastcsv.writer.CsvWriter;

class ConsoleWriterTest {

@SuppressWarnings("checkstyle:RegexpMultiline")
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream capturedOut = new ByteArrayOutputStream();

@BeforeEach
public void setUp() {
System.setOut(new PrintStream(capturedOut, true, StandardCharsets.UTF_8));
}

@AfterEach
public void tearDown() {
System.setOut(standardOut);
}

@Test
void console() {
CsvWriter.builder().toConsole()
.writeRecord("foo", "bar");

assertThat(capturedOut).asString()
.isEqualTo("foo,bar\r\n");
}

}
41 changes: 40 additions & 1 deletion lib/src/intTest/java/blackbox/writer/CsvWriterTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package blackbox.writer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -260,13 +263,49 @@ void disableBuffer() {
assertThat(stringWriter).asString().isEqualTo("foo,bar\n");
}

// autoFlush

@Test
void noAutoFlush() {
final CsvWriter csvWriter = CsvWriter.builder().build(flushFailWriter());
assertThatCode(() -> csvWriter.writeRecord("foo"))
.doesNotThrowAnyException();
}

@Test
void manualFlush(@TempDir final Path tempDir) throws IOException {
final Path file = tempDir.resolve("fastcsv.csv");
CsvWriter.builder().build(file)
.writeRecord("foo")
.flush();

assertThat(Files.readString(file))
.isEqualTo("foo\r\n");
}

@Test
void autoFlush() {
final CsvWriter csvWriter = CsvWriter.builder().autoFlush(true).build(flushFailWriter());
assertThatThrownBy(() -> csvWriter.writeRecord("foo"))
.isInstanceOf(UnsupportedOperationException.class);
}

private static FilterWriter flushFailWriter() {
return new FilterWriter(FilterWriter.nullWriter()) {
@Override
public void flush() {
throw new UnsupportedOperationException();
}
};
}

// toString()

@Test
void builderToString() {
assertThat(crw).asString()
.isEqualTo("CsvWriterBuilder[fieldSeparator=,, quoteCharacter=\", "
+ "commentCharacter=#, quoteStrategy=null, lineDelimiter=\n, bufferSize=8192]");
+ "commentCharacter=#, quoteStrategy=null, lineDelimiter=\n, bufferSize=8192, autoFlush=false]");
}

@Test
Expand Down
Loading

0 comments on commit 0f6bf93

Please sign in to comment.