Hallo,
in grauer Vorzeit habe ich mir für das Lesen und Schreiben von Strings in einen Stream zwei Nachfahren gebastelt.
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.
In der richtigen Reihenfolge muss natürlich auch hier gelesen werden.