-1

I'm using these implementations that I found on another post on SO. But when reading a string in sometimes, ReadStreamStr puts in extra characters into the string. So I'm posting my implementation here to have it checked and approved, and if there is a fault, corrected.

function ReadStreamStr(Stream: TStream): string;
{ returns a string from the stream }
var
  LenStr: Integer;
begin
  Result := '';
  { get length of string }
  LenStr := ReadStreamInt(Stream);
  { set string to get memory }
  SetLength(Result, LenStr);
  { read characters }
  Stream.Read(Result[1], LenStr);
end;

procedure WriteStreamStr(Stream: TStream; Str: string);
{ writes a string to the stream }
var
  StrLen: Integer;
begin
  { get length of string }
  StrLen := ByteLength(Str);
  { write length of string }
  WriteStreamInt(Stream, StrLen);
  if StrLen > 0 then
    { write characters }
    Stream.Write(Str[1], StrLen);
end;

Thank you.

2
  • 2
    I would personally use a pseudo-code like this.
    – TLama
    May 13, 2013 at 23:54
  • @TLama: There's something wrong with that code as well. its not working for me. Its reading beyond the end of the string. Using: Delphi XE2.
    – Steve F
    May 14, 2013 at 0:21

1 Answer 1

4

The Integer that is written in front of the characters is the number of bytes, but ReadStreamStr() is interpreting it as the number of characters instead. In D2007 and earlier, where string is Ansi, that works fine. But in D2009 and later, where string is Unicode, that does not work. Assuming you are using D2009+, that means ReadStreamStr() is allocating twice as much memory as it should be, but only filling in half of it, leave the other half uninitialized. You are not taking SizeOf(Char) into account when reading (but ByteLength() does when writing).

Try this:

function ReadStreamStr(Stream: TStream): string;
{ returns a string from the stream }
var
  LenStr: Integer;
  LeftOver: array[0..SizeOf(Char)-1] of Byte;
begin
  Result := '';
  { get length of string }
  LenStr := ReadStreamInt(Stream);
  { set string to get memory }
  SetLength(Result, LenStr div SizeOf(Char));
  if LenStr > 0 then begin
    { read characters }
    Stream.ReadBuffer(Result[1], LenStr);
    { just in case the length was not even }
    LenStr := LenStr mod SizeOf(Char);
    if LenStr > 0 then
      Stream.ReadBuffer(LeftOver[0], LenStr);
  end;
end;

Alternatively:

function ReadStreamStr(Stream: TStream): string;
{ returns a string from the stream }
var
  LenStr: Integer;
  Buf: TBytes;
begin
  Result := '';
  { get length of string }
  LenStr := ReadStreamInt(Stream);
  if LenStr > 0 then begin
    { get memory }
    SetLength(Buf, LenStr);
    { read characters }
    Stream.ReadBuffer(Buf[1], LenStr);
    { convert to string }
    Result := TEncoding.Unicode.GetString(Buf);
  end;
end;

Either way, keep in mind that if the stream data was written in D2007 or earlier, but is read in D2009 or later, then this code will not work as-is. You would have to go the TBytes approach, but use a more appropriate TEncoding for the decoding, such as TEncoding.Default.

Personally, I would have opted to instead read/write the strings as UTF-8 from the beginning:

function ReadStreamStr(Stream: TStream): UTF8String;
{ returns a string from the stream }
var
  LenStr: Integer;
begin
  Result := '';
  { get length of string }
  LenStr := ReadStreamInt(Stream);
  { set string to get memory }
  SetLength(Result, LenStr);
  if LenStr > 0 then
    { read characters }
    Stream.Read(PAnsiChar(Result)^, LenStr);
end;

procedure WriteStreamStr(Stream: TStream; const Str: UTF8tring);
{ writes a string to the stream }
var
  StrLen: Integer;
begin
  { get length of string }
  StrLen := Length(Str);
  { write length of string }
  WriteStreamInt(Stream, StrLen);
  if StrLen > 0 then
    { write characters }
    Stream.Write(PAnsiChar(Str)^, StrLen);
end;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.