Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

MD5 How-to: How To Change the Read Buffer Size

Applies to: ~>1.0

TPJMD5 uses a buffer when reading data from a stream or a file. By default the buffer has the size specified by the TPJMD5.DefReadBufferSize constant. You can change the buffer size by setting the TPJMD5.ReadBufferSize property.

Here’s an example function that gets the MD5 hash of a file using a specified buffer size:

function MD5OfFile(const FileName: string;
  const BufSize: Integer): TPJMD5Digest;
var
  MD5: TPJMD5;
begin
  MD5 := TPJMD5.Create;
  try
    MD5.ReadBufferSize := BufSize;
    MD5.ProcessFile(FileName);
    Result := MD5.Digest;
  finally
    MD5.Free;
  end;
end;

And here’s a similar function that uses one of the TStream overloads of TPJMD5.Process to get the MD5 hash of the whole of a stream using a given buffer size:

function MD5OfStream(const Stm: TStream;
  const BufSize: Integer): TPJMD5Digest;
var
  MD5: TPJMD5;
begin
  MD5 := TPJMD5.Create;
  try
    MD5.ReadBufferSize := BufSize;
    Stm.Position := 0;
    MD5.Process(Stm);
    Result := MD5.Digest;
  finally
    MD5.Free;
  end;
end;

Because you need a TPJMD5 instance in order to access the TPJMD5.ReadBufferSize property you can’t change the buffer size when using the TPJMD5.CalculateFile class method or the TStream overload of TPJMD5.Calculate.

See Also