I'm confuse...what is the purpose of this method ?

I'm confuse...what is the purpose of this method ?

function TStream.ReadData(Buffer: Pointer; Count: Longint): Longint;
var
Buf: TBytes;
I: Integer;
begin
SetLength(Buf, Count);
Result := Read(Buf, 0, Count);
for I := 0 to Result - 1 do
PByteArray(Buffer)^[I] := Buf[I];
end;

Comments

  1. It reads data from a stream and returns the number of bytes read.
    Or do you mean "Why have this method when there is already TStream.Read which does the same with less overhead?"
    For that the answer is probably "backwards compatibility".

    ReplyDelete
  2. Thomas Mueller compatibility with what ?

    is there any difference with this implementation (that could be inlined)

    function TStream.ReadData(Buffer: Pointer; Count: Longint): Longint;
    begin
    Result := Read(Buffer^, Count);
    end;

    ReplyDelete
  3. There is one difference: Even if read overwrites the whole buffer passed to it, this won't affect the bytes at offsets > Result in the buffer passed to ReadData.

    ReplyDelete
  4. Thomas Mueller I think that is the case in both variants.

    The first version will ensure the original buffer is untouched in the case of an exception inside Read. The number of times that is relevant should be minuscule, however :).

    ReplyDelete
  5. Attila Kovacs You're damn right, it's in XE3 version, it has been fixed since then ! good !
    I work on some tweaks for the VCL and did apply that on a customer's project under XE3

    ReplyDelete

Post a Comment