We have this old code in our repo:
bool BufferByteCompareFiles(string filePath1, string filePath2)
{
int bufferCapacity = 0x400000;
var firstFile = File.OpenRead(filePath1);
var secondFile = File.OpenRead(filePath2);
using var firstStream = new BufferedStream(firstFile);
using var secondStream = new BufferedStream(secondFile);
if (firstFile.Length != secondFile.Length)
return false;
var firstBuffer = new byte[bufferCapacity];
var secondBuffer = new byte[bufferCapacity];
int bytesReadFirst;
int bytesReadSecond;
do
{
bytesReadFirst = firstStream.Read(firstBuffer, 0, firstBuffer.Length);
bytesReadSecond = secondStream.Read(secondBuffer, 0, secondBuffer.Length);
if (bytesReadFirst != bytesReadSecond || !CompareByteArrays(firstBuffer, secondBuffer))
return false;
} while (bytesReadFirst > 0 && bytesReadSecond > 0);
return true;
}
static bool CompareByteArrays(byte[] first, byte[] second)
=> first.Length == second.Length && Interop.memcmp(first, second, first.Length) == 0;
and the part that I don't understand is why check the byte array lengths in CompareByteArrays when already there is an if statement checking if bytesReadFirst != bytesReadSecond and those ints are the amount of bytes read by BufferedStream.Read().
Am I missing something or can the whole first.Length == second.Length be omitted?