Skip to content

Commit 5e90310

Browse files
committed
Add optional flush after write in ByteStreamFileWriter
1 parent 3d9aff9 commit 5e90310

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

metafacture-io/src/main/java/org/metafacture/io/ByteStreamFileWriter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ByteStreamFileWriter extends DefaultObjectReceiver<byte[]> {
2424

2525
private Supplier<File> fileNameSupplier;
2626
private boolean appendIfFileExists;
27+
private boolean flushAfterWrite;
2728

2829
private OutputStream outputStream;
2930

@@ -62,6 +63,23 @@ public void setAppendIfFileExists(boolean appendIfFileExists) {
6263
this.appendIfFileExists = appendIfFileExists;
6364
}
6465

66+
/**
67+
* Controls whether the output stream is flushed after each write
68+
* operation in {@link #process(byte[])}.
69+
* <p>
70+
* The default value is {@code false}.
71+
* <p>
72+
* This property can be changed anytime during processing. It becomes
73+
* effective on the next invocation of {@link #process(byte[])}.
74+
*
75+
* @param flushAfterWrite true if the output stream should be flushed
76+
* after every write.
77+
*/
78+
public void setFlushAfterWrite(boolean flushAfterWrite) {
79+
80+
this.flushAfterWrite = flushAfterWrite;
81+
}
82+
6583
/**
6684
* Writes {@code bytes} to file.
6785
*
@@ -74,6 +92,9 @@ public void process(final byte[] bytes) {
7492
ensureOpenStream();
7593
try {
7694
outputStream.write(bytes);
95+
if (flushAfterWrite) {
96+
outputStream.flush();
97+
}
7798

7899
} catch (IOException e) {
79100
throw new WriteFailed("Error while writing bytes to output stream", e);

0 commit comments

Comments
 (0)