Jens Schumann hat Nachfahren von TFileStream und TMemoryStream geschrieben, mit denen es möglich ist, Strings als Stream abzuspeichern bzw. auszulesen.
Veröffentlich hat er seinen Code in
diesem Beitrag.
Delphi-Quellcode:
uses
Sysutils, Classes;
Type
TMemoryStreamExt = class(TMemoryStream)
public
procedure WriteStringToStream(aStr: string);
function ReadStringFromStream: string;
end;
TFileStreamExt = class(TFilestream)
public
procedure WriteStringToStream(aStr: string);
function ReadStringFromStream: string;
end;
implementation
{ TFileStreamExt }
procedure TFileStreamExt.WriteStringToStream(aStr: string);
var
aStrLen: Integer;
begin
aStrLen := Length(aStr);
WriteBuffer(aStrLen, SizeOf(Integer));
WriteBuffer(Pointer(aStr)^, aStrLen);
end;
function TFileStreamExt.ReadStringFromStream: string;
var
aStrLen: Integer;
begin
ReadBuffer(aStrLen, SizeOf(Integer));
SetLength(Result, aStrLen);
ReadBuffer(Pointer(Result)^, aStrLen);
end;
{ TMemoryStreamExt }
function TMemoryStreamExt.ReadStringFromStream: string;
var
aStrLen: Integer;
begin
ReadBuffer(aStrLen, SizeOf(Integer));
SetLength(Result, aStrLen);
ReadBuffer(Pointer(Result)^, aStrLen);
end;
procedure TMemoryStreamExt.WriteStringToStream(aStr: string);
var
aStrLen: Integer;
begin
aStrLen := Length(aStr);
WriteBuffer(aStrLen, SizeOf(Integer));
WriteBuffer(Pointer(aStr)^, aStrLen);
end;
end.