Why does the code below not produce an identical output file to the input file?
The idea is to buffer a part of the file in and write it out from a smaller buffer (this is a small program reproducing an error in a larger project where I am streaming a file). When these files are checked in a hex editor comparison tool the output file differs somewhat.
fsIn = new FileStream("c:\\wmvs\\Wildlife.wmv", FileMode.Open, FileAccess.Read);
fsOut = new FileStream("c:\\Users\\public\\documents\\compare\\out.wmv", FileMode.Create, FileAccess.Write);
bData = new byte[fsIn.Length / 10];
bOut = new byte[524288];
fsIn.Read(bData, 0, bData.Length);
bool bGo = true;
while (bGo)
{
if (nWrittenOut == bData.Length)
{
fsIn.Read(bData, 0, bData.Length);
}
if (nWrittenOut + bOut.Length >= bData.Length)
{
Array.Clear(bOut, 0, bOut.Length);
int nWhatsLeft = bData.Length - nWrittenOut;
Array.Copy(bData, nWrittenOut, bOut, 0, nWhatsLeft);
fsIn.Read(bData, 0, bData.Length);
nWrittenOut = 0;
int nBufPos = nWhatsLeft;
nWhatsLeft = bOut.Length - nWhatsLeft;
Array.Copy(bData, nWrittenOut, bOut, nBufPos, nWhatsLeft);
nWrittenOut += bOut.Length;
}
else
{
Array.Copy(bData, nWrittenOut, bOut, 0, bOut.Length);
nWrittenOut += bOut.Length;
}
fsOut.Write(bOut, 0, bOut.Length);
fsOut.Flush();
if (fsOut.Position >= fsIn.Length)
bGo = false;
}
}
I have tried all the below answers and nothing works. It must be my logic in the code. However I cannot see the problem ???? It seems I am missing a whole chunk in the output file eqivalent to the length of bOut.