In a WCF service I have a FileStream that is supposed to create a huge file.
A procedure within the WCF service regularly receives a chunk of bytes that must be written to the FileStream. The FileStream uses the default buffer size.
My procedure receives bytes in chunks with the same size as the size of the FileStream's buffer, except maybe for the last chunk. The procedure writes the complete chunk to the FileStream. Therefore after most writes the FileStream's buffer is expected to be full.
Question: Is a full buffer automatically flushed or should I Flush whenever I think that the buffer is full?
If full buffers are automatically flushed then my Flush would do more harm than good, because it would Flush immediately after the buffer is already flushed.
Code is as follows:
async Task PersistData(byte[] receivedData)
{
// write all bytes in one write to the FileStream:
await stream.WriteAsync(receivedData, 0, receivedData.Length);
// This will have filled the buffer to the brim.
// Should I Flush or did the FileStream already Flush?
await stream.FlushAsync();
}