Created
January 8, 2015 15:43
-
-
Save DrOzturk/fb7d1179fb8234be6c9e to your computer and use it in GitHub Desktop.
Compare two streams for equality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StreamHelpers | |
{ | |
//http://stackoverflow.com/questions/968935/c-sharp-binary-file-compare | |
/// <summary> | |
/// Compares two streams, returns true if equal. | |
/// </summary> | |
/// <param name="expectedStream"></param> | |
/// <param name="actualStream"></param> | |
/// <returns></returns> | |
public static bool AssertEquals(this Stream expectedStream, Stream actualStream, string errorMessage) | |
{ | |
const int bufferSize = 2048; | |
byte[] buffer1 = new byte[bufferSize]; | |
byte[] buffer2 = new byte[bufferSize]; | |
int indexOfBuffer = 0; | |
while (true) | |
{ | |
int countExpected = expectedStream.Read(buffer1, 0, bufferSize); | |
int countActual = actualStream.Read(buffer2, 0, bufferSize); | |
if (countExpected == 0 && countActual ==0) // Streams ended | |
return true; | |
// You might replace the following with an efficient "memcmp" | |
if (!buffer1.SequenceEqual(buffer2)) | |
{ | |
Console.WriteLine("Mismatch in {0}th buffer of {1} bytes.", | |
indexOfBuffer, bufferSize); | |
return false; | |
} | |
indexOfBuffer++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment