Delphi function comparing content of two TStream?

compare, delphi, delphi-xe, stream

Solution

Exactly, as Nickolay O. said you should read your stream in blocks and use CompareMem. Here is an example (including size test) ...

function IsIdenticalStreams(Source, Destination: TStream): boolean;
const Block_Size = 4096;

var Buffer_1: array[0..Block_Size-1] of byte;
    Buffer_2: array[0..Block_Size-1] of byte;
    Buffer_Length: integer;

begin
  Result := False;

  if Source.Size <> Destination.Size then
    Exit;

  while Source.Position < Source.Size do
    begin
      Buffer_Length := Source.Read(Buffer_1, Block_Size);
      Destination.Read(Buffer_2, Block_Size);

      if not CompareMem(@Buffer_1, @Buffer_2, Buffer_Length) then
        Exit;
    end;

  Result := True;
end;

Problem

I need to compare if two TStream descendant have the same content. The only interesting result for me is the boolean Yes / No. I'm going to code a simple loop checking byte after byte the streams content's. But I'm curious to know if there is an already existing function. I haven't found any inside DelphiXE or JCL/JVCL libs. Of course, the two streams have the same size !

Original source