The Background:
I have the following WriteFileToStream function which is intended to complete a simple job: take the data from a file and copy it to a Stream.
I originally was using the Stream.CopyTo(Stream) method. However, after a long debugging process, I found that this was the cause of a 'corrupt data' error further in my processing pipeline.
Synopsis:
Using the Stream.CopyTo(Stream) method yields 65536 bytes of data and the stream does not process correctly.
Using the Stream.Write(...) method yields 45450 bytes of data and the stream processes correctly.
The Question:
Can anyone see why the following usage of CopyTo may have resulted in extraneous data being written to the stream?
PLEASE NOTE: The final code in WriteFileToStream was taken from answer to this question: Save and load MemoryStream to/from a file
public static void WriteFileToStream(string fileName, Stream outputStream)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
long fileLength = file.Length;
byte[] bytes = new byte[fileLength];
file.Read(bytes, 0, (int)fileLength);
outputStream.Write(bytes, 0, (int)fileLength);
file.Close();
outputStream.Close();
// This was corrupting the data - adding superflous bytes to the result...somehow.
//using (FileStream file = File.OpenRead(fileName))
//{
// //
// file.CopyTo(outputStream);
//}
}