I have the following code which works most of the time when saving a file from a memory stream pointing to a byte array to a network location.
using (var writer = new BinaryWriter(new FileStream(filePath, FileMode.Create)))
using (var reader = new BinaryReader(stream))
{
var chunkSize = 1024;
var chunkCount = (int)reader.BaseStream.Length / chunkSize;
var chunks = Enumerable.Range(0, chunkCount)
.Select(_ => reader.ReadBytes(chunkSize));
chunks.ForEach(c => writer.Write(c));
writer.Write(reader.ReadBytes((int)reader.BaseStream.Length % chunkSize));
writer.Close();
reader.Close();
}
Is there any possible way the above code may end up saving zero bytes file? Is it a bad idea to save to the network directly and not use a "copy from temp" method?