Skip to content

Commit

Permalink
Add optional flush after write in ByteStreamFileWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
cboehme committed Dec 12, 2018
1 parent 3d9aff9 commit 5e90310
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ByteStreamFileWriter extends DefaultObjectReceiver<byte[]> {

private Supplier<File> fileNameSupplier;
private boolean appendIfFileExists;
private boolean flushAfterWrite;

private OutputStream outputStream;

Expand Down Expand Up @@ -62,6 +63,23 @@ public void setAppendIfFileExists(boolean appendIfFileExists) {
this.appendIfFileExists = appendIfFileExists;
}

/**
* Controls whether the output stream is flushed after each write
* operation in {@link #process(byte[])}.
* <p>
* The default value is {@code false}.
* <p>
* This property can be changed anytime during processing. It becomes
* effective on the next invocation of {@link #process(byte[])}.
*
* @param flushAfterWrite true if the output stream should be flushed
* after every write.
*/
public void setFlushAfterWrite(boolean flushAfterWrite) {

this.flushAfterWrite = flushAfterWrite;
}

/**
* Writes {@code bytes} to file.
*
Expand All @@ -74,6 +92,9 @@ public void process(final byte[] bytes) {
ensureOpenStream();
try {
outputStream.write(bytes);
if (flushAfterWrite) {
outputStream.flush();
}

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

0 comments on commit 5e90310

Please sign in to comment.