在Delphi 2009中将TMemoryStream转换为“String”

在Delphi 2009之前我们有以下代码:

<span style="font: 10pt Courier New;"><span class="pas1-reservedword">function</span><span class="pas1-space"> </span><span class="pas1-identifier">MemoryStreamToString(M:</span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStream):</span><span class="pas1-space"> </span><span class="pas1-reservedword">String</span><span class="pas1-symbol">; </span><span class="pas1-reservedword">var </span><span class="pas1-space"> </span><span class="pas1-identifier">NewCapacity:</span><span class="pas1-space"> </span><span class="pas1-identifier">Longint; </span><span class="pas1-reservedword">begin </span><span class="pas1-space"> </span><span class="pas1-reservedword">if</span><span class="pas1-space"> </span><span class="pas1-symbol">(M.Size</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-number">0)</span><span class="pas1-space"> </span><span class="pas1-reservedword">or</span><span class="pas1-space"> </span><span class="pas1-symbol">(M.Memory</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-reservedword">nil</span><span class="pas1-symbol">)</span><span class="pas1-space"> </span><span class="pas1-reservedword">then </span><span class="pas1-space"> </span><span class="pas1-identifier">Result:=</span><span class="pas1-space"> </span><span class="pas1-string">'' </span><span class="pas1-space"> </span><span class="pas1-reservedword">else </span><span class="pas1-space"> </span><span class="pas1-reservedword">begin </span><span class="pas1-space"> </span><span class="pas1-reservedword">if</span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStreamProtected(M).Capacity</span><span class="pas1-space"> </span><span class="pas1-symbol">=</span><span class="pas1-space"> </span><span class="pas1-identifier">M.Size</span><span class="pas1-space"> </span><span class="pas1-reservedword">then </span><span class="pas1-space"> </span><span class="pas1-reservedword">begin </span><span class="pas1-space"> </span><span class="pas1-identifier">NewCapacity:=</span><span class="pas1-space"> </span><span class="pas1-identifier">M.Size+1; </span><span class="pas1-space"> </span><span class="pas1-identifier">TMemoryStreamProtected(M).Realloc(NewCapacity); </span><span class="pas1-space"> </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">; </span><span class="pas1-space"> </span><span class="pas1-identifier">NullString(M.Memory^)[M.Size]:=</span><span class="pas1-space"> </span><span class="pas1-character">#0; </span><span class="pas1-space"> </span><span class="pas1-identifier">Result:=</span><span class="pas1-space"> </span><span class="pas1-identifier">StrPas(M.Memory); </span><span class="pas1-space"> </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">; </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">; </span></span> 

我们如何将这个代码转换成现在支持Unicode的Delphi 2009?

你拥有的代码是不必要的复杂的,即使是旧的Delphi版本。 为什么要获取stream的string版本迫使stream的内存重新分配?

 function MemoryStreamToString(M: TMemoryStream): string; begin SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char)); end; 

这适用于所有delphi版本,而不仅仅是delphi2009年。它的工作原理,当stream是空的没有任何特殊情况。 SetString是一个低估function。

如果您的切换到Delphi 2009的stream的内容不会更改为Unicode,那么您应该使用此function:

 function MemoryStreamToString(M: TMemoryStream): AnsiString; begin SetString(Result, PAnsiChar(M.Memory), M.Size); end; 

这相当于您的原始代码,但跳过了特殊情况。

或者,也许你可以重构你的代码直接使用直接TStringStream? 你可以使用它来代替TMemoryStream(它们有相同的接口),你可以通过调用myString:= myStringStream.DataString;

“更清洁”的方式可能是:

 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; 

我用:

 function StreamToString(const Stream: TStream; const Encoding: TEncoding): string; var StringBytes: TBytes; begin Stream.Position := 0; SetLength(StringBytes, Stream.Size); Stream.ReadBuffer(StringBytes, Stream.Size); Result := Encoding.GetString(StringBytes); end; 

它已经使用Delphi XE7进行了testing。

我还没有升级,但我的理解是:

 NewCapacity := (M.Size + 1) * SizeOf(Char);