Converting TMemoryStream to 'String' in Delphi 2009

后端 未结 7 944
南方客
南方客 2020-11-28 05:52

We had the following code prior to Delphi 2009:

function MemoryStreamToString(M : TMemoryStream): String;
var
    NewCapacity: Longint;
begin
    if (M.Size          


        
7条回答
  •  死守一世寂寞
    2020-11-28 06:20

    A "cleaner" way might be:

    function StreamToString(aStream: TStream): string;
    var
      SS: TStringStream;
    begin
      if aStream <> nil then
      begin
        SS := TStringStream.Create('');
        try
          SS.CopyFrom(aStream, 0);  // No need to position at 0 nor provide size
          Result := SS.DataString;
        finally
          SS.Free;
        end;
      end else
      begin
        Result := '';
      end;
    end;
    

提交回复
热议问题