Click here to Skip to main content
16,006,535 members
Home / Discussions / C#
   

C#

 
GeneralRe: Accessing Data with Web Forms Pin
Colin Angus Mackay5-Jul-04 12:22
Colin Angus Mackay5-Jul-04 12:22 
GeneralRe: Accessing Data with Web Forms Pin
Ian Bowler5-Jul-04 12:46
Ian Bowler5-Jul-04 12:46 
GeneralRe: Accessing Data with Web Forms Pin
Colin Angus Mackay5-Jul-04 13:03
Colin Angus Mackay5-Jul-04 13:03 
GeneralRe: Accessing Data with Web Forms Pin
Heath Stewart5-Jul-04 18:06
protectorHeath Stewart5-Jul-04 18:06 
GeneralSearch object in hierarchy object (class) Pin
god4k5-Jul-04 5:58
god4k5-Jul-04 5:58 
GeneralRe: Search object in hierarchy object (class) Pin
Heath Stewart5-Jul-04 10:08
protectorHeath Stewart5-Jul-04 10:08 
QuestionHow to compare MemoryStream? Pin
god4k5-Jul-04 5:57
god4k5-Jul-04 5:57 
AnswerRe: How to compare MemoryStream? Pin
Heath Stewart5-Jul-04 10:01
protectorHeath Stewart5-Jul-04 10:01 
Only primitive types and those that define the equality operators are comparable using ==, otherwise a simple reference (whether i and j referenece the same object) is performed. Classes may also override Object.Equals; if they don't, again simple reference equality is used.

Since MemoryStream nor its base class, Stream, define either of these, you must compare these byte-for-byte yourself:
public int Compare(MemoryStream a, MemoryStream b)
{
  if (a == null && b == null) return 0;
  else if (a == null || b == null) throw new ArgumentNullException(
    a == null ? "a" : "b");
  if (a.Length < b.Length) return -1;
  else if (a.Length > b.Length) return 1;
  byte buf;
  while ((buf = a.ReadByte()) >= 0)
  {
    int diff = buf.CompareTo(b.ReadByte());
    if (diff != 0) return diff;
  }
  return 0;
}
As with most comparisons in the BCL, 0 is returned if the two streams are equal. a negative or positive integer value is returned depending on the conditions, which should be obvious from the code above.

Note that this is only an example, but should give you an idea of how to compare the MemoryStreams.

You could also P/Invoke the memcmp function, although this will hinder the portability of your code but will be very fast.

As I covered a few days ago with the same question (hint: it's a good idea to try searching for previous questions before posting), you can P/Invoke memcmp like so:
public sealed class ByteComparer
{
  private ByteComparer();
  public static int Compare(byte[] a, byte[] b)
  {
    return memcmp(a, b, new IntPtr(a.Length)).ToInt32();
  }
  [DllImport("msvcrt.dll")]
  private static extern IntPtr memcmp(byte[] a, byte[] b, IntPtr count);
}
This does a comparison using the native memcmp API if you just pass two byte[] arrays (which you can get easily using MemoryStream.ToArray) into the static method Compare:
MemoryStream a = ...;
MemoryStream b = ...;
int diff = ByteComparer.Compare(a.ToArray(), b.ToArray());


 

Microsoft MVP, Visual C#
My Articles
GeneralRe: How to compare MemoryStream? Pin
Mike Dimmick5-Jul-04 13:44
Mike Dimmick5-Jul-04 13:44 
GeneralRe: How to compare MemoryStream? Pin
Heath Stewart5-Jul-04 17:58
protectorHeath Stewart5-Jul-04 17:58 
GeneralRe: How to compare MemoryStream? Pin
god4k5-Jul-04 14:30
god4k5-Jul-04 14:30 
AnswerRe: How to add custom properties in PropertyGrid at Runtime Pin
Heath Stewart5-Jul-04 5:11
protectorHeath Stewart5-Jul-04 5:11 
GeneralHowto Delete a file at run time!?! Pin
QzRz5-Jul-04 4:45
QzRz5-Jul-04 4:45 
GeneralRe: Howto Delete a file at run time!?! Pin
Heath Stewart5-Jul-04 4:50
protectorHeath Stewart5-Jul-04 4:50 
GeneralRe: Howto Delete a file at run time!?! Pin
QzRz5-Jul-04 5:09
QzRz5-Jul-04 5:09 
GeneralRe: Howto Delete a file at run time!?! Pin
Heath Stewart5-Jul-04 5:12
protectorHeath Stewart5-Jul-04 5:12 
GeneralRe: Howto Delete a file at run time!?! Pin
QzRz5-Jul-04 5:15
QzRz5-Jul-04 5:15 
GeneralRe: Howto Delete a file at run time!?! Pin
Heath Stewart5-Jul-04 5:19
protectorHeath Stewart5-Jul-04 5:19 
GeneralRe: Howto Delete a file at run time!?! Pin
QzRz5-Jul-04 5:25
QzRz5-Jul-04 5:25 
GeneralRe: Howto Delete a file at run time!?! Pin
Colin Angus Mackay5-Jul-04 5:57
Colin Angus Mackay5-Jul-04 5:57 
GeneralRe: Howto Delete a file at run time!?! Pin
QzRz5-Jul-04 9:53
QzRz5-Jul-04 9:53 
GeneralRe: Howto Delete a file at run time!?! Pin
Jeremy Falcon5-Jul-04 10:14
professionalJeremy Falcon5-Jul-04 10:14 
GeneralRe: Howto Delete a file at run time!?! Pin
Colin Angus Mackay5-Jul-04 11:06
Colin Angus Mackay5-Jul-04 11:06 
GeneralObject reference not set to an instance of an object Pin
rturner0035-Jul-04 4:34
rturner0035-Jul-04 4:34 
GeneralRe: Object reference not set to an instance of an object Pin
Colin Angus Mackay5-Jul-04 4:43
Colin Angus Mackay5-Jul-04 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.