[DUG] Potential bug in XE2

Todd Martin todd.martin.nz at gmail.com
Sat Jun 30 21:54:51 NZST 2012


TStrings.SaveToStream calls Stream.WriteBuffer()
whereas
TStrings.LoadFromStream calls Stream.Read()

No buffering and no error message, if the buffer is not read in completely!

procedure TStrings.SaveToStream(Stream: TStream; Encoding: TEncoding);
var
  Buffer, Preamble: TBytes;
begin
  if Encoding = nil then
    Encoding := FDefaultEncoding;
  Buffer := Encoding.GetBytes(GetTextStr);
  if FWriteBOM then
  begin
    Preamble := Encoding.GetPreamble;
    if Length(Preamble) > 0 then
      Stream.WriteBuffer(Preamble[0], Length(Preamble));
  end;
  Stream.WriteBuffer(Buffer[0], Length(Buffer));
end;


procedure TStrings.LoadFromStream(Stream: TStream; Encoding: TEncoding);
var
  Size, PreambleSize: Integer;
  Buffer: TBytes;
begin
  BeginUpdate;
  try
    Size := Stream.Size - Stream.Position;
    SetLength(Buffer, Size);
    Stream.Read(Buffer[0], Size);
    PreambleSize:= TEncoding.GetBufferEncoding(Buffer, Encoding,
FDefaultEncoding);
    SetEncoding(Encoding); // Keep Encoding in case the stream is saved
    SetTextStr(Encoding.GetString(Buffer, PreambleSize, Length(Buffer) -
PreambleSize));
  finally
    EndUpdate;
  end;
end;




More information about the Delphi mailing list