Last active
March 16, 2021 22:28
-
-
Save sebingel/447e2b86be27f6172bfe395b52d05c96 to your computer and use it in GitHub Desktop.
Compare two streams bit by bit in C#
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
private bool StreamEquals(Stream a, Stream b) | |
{ | |
if (a == b) | |
{ | |
return true; | |
} | |
if (a == null || b == null) | |
{ | |
throw new ArgumentNullException(a == null ? "a" : "b"); | |
} | |
if (a.Length != b.Length) | |
{ | |
return false; | |
} | |
for (int i = 0; i < a.Length; i++) | |
{ | |
int aByte = a.ReadByte(); | |
int bByte = b.ReadByte(); | |
if (aByte.CompareTo(bByte) != 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} |
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 StreamExtensions | |
{ | |
public static bool StreamEquals(this Stream self, Stream other) | |
{ | |
if (self == other) | |
{ | |
return true; | |
} | |
if (self == null || other == null) | |
{ | |
throw new ArgumentNullException(self == null ? "self" : "other"); | |
} | |
if (self.Length != other.Length) | |
{ | |
return false; | |
} | |
for (int i = 0; i < self.Length; i++) | |
{ | |
int aByte = self.ReadByte(); | |
int bByte = other.ReadByte(); | |
if (aByte.CompareTo(bByte) != 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. You are absolutely right. Gonna change it right now.