Delphi函数比较两个TStream的内容? [英] Delphi function comparing content of two TStream?

查看:292
本文介绍了Delphi函数比较两个TStream的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个 TStream 后代是否具有相同的内容
对我唯一有趣的结果是布尔是/否。

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.

但我是好奇知道是否有一个已经存在的函数。我没有找到任何内部DelphiXE或JCL / JVCL库。

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 !

推荐答案

正如Nickolay O.所说,你应该在块中读取你的流并使用CompareMem。这里是一个例子(包括大小测试)...

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;

这篇关于Delphi函数比较两个TStream的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆