Robert W. <Ro*****@discussions.microsoft.com> wrote:
I followed your advice and wrote my own file comparison checker. I'll show
the code below as it may help out someone else. One concern I had was
reading byte by byte but the speed seems to be very fast. Here's the code:
A few things:
1) I'd use a using statement to close the streams even if an exception
occurs
2) It would definitely be faster to use chunked reading - but if it's
fast enough, keeping with simple code is definitely a good thing :)
3) You're actually comparing the files byte by byte, not character by
character. That's probably okay for you, but it's worth being aware
of.
I wrote the following code a while ago, but it may be helpful to you:
const int BufferLength = 32768;
static bool CompareStreams (Stream s1, Stream s2)
{
if (s1==null || s2==null)
{
throw new ArgumentNullException
("Streams to compare must both be non-null");
}
// A buffer for each stream
byte[] buffer1 = new byte[BufferLength];
byte[] buffer2 = new byte[BufferLength];
// Number of bytes valid within each buffer
int buffer1Valid=0;
int buffer2Valid=0;
// Index within the buffer for each stream
int buffer1Index=0;
int buffer2Index=0;
while (true)
{
// Read any more data if we need to
if (buffer1Index==buffer1Valid)
{
buffer1Valid = s1.Read(buffer1, 0, BufferLength);
buffer1Index=0;
}
if (buffer2Index==buffer2Valid)
{
buffer2Valid = s2.Read(buffer2, 0, BufferLength);
buffer2Index=0;
}
// We've read to the end of both streams simultaneously
if (buffer1Valid==0 && buffer2Valid==0)
{
return true;
}
// We've read to the end of one stream but not the other
if (buffer1Valid==0 || buffer2Valid==0)
{
return false;
}
if (buffer1[buffer1Index] != buffer2[buffer2Index])
{
return false;
}
buffer1Index++;
buffer2Index++;
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too