Lazarus

Programming => General => Topic started by: Handoko on July 14, 2020, 08:11:45 pm

Title: [SOLVED] TMemoryStream.ReadAnsiString Error
Post by: Handoko on July 14, 2020, 08:11:45 pm
Did I do it wrong?

Why the first attempt to convert TMemoryStream's data to string is okay but on the second attempt I got an EReadError exception 'Stream read error' message.


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. { TForm1 }
  27.  
  28. procedure TForm1.Button1Click(Sender: TObject);
  29. const
  30.   Data = 'Testing';
  31. var
  32.   aMemoryStream: TMemoryStream;
  33.   aString:       string;
  34.   i:             Integer;
  35. begin
  36.   aMemoryStream := TMemoryStream.Create;
  37.   for i := 1 to Data.Length do
  38.     aMemoryStream.WriteByte(Ord(Data[i]));
  39.  
  40.   // First read attempt - OK
  41.   aString := '';
  42.   aMemoryStream.Position := 0;
  43.   for i := 1 to aMemoryStream.Size do
  44.     aString := aString + Chr(aMemoryStream.ReadByte);
  45.   ShowMessage(aString);
  46.  
  47.   // Second read attempt - FAILED
  48.   aMemoryStream.Position := 0;
  49.   aString := aMemoryStream.ReadAnsiString;
  50.   ShowMessage(aString);
  51.  
  52.   aMemoryStream.Free
  53. end;
  54.  
  55. end.

Tested on Lazarus 2.0.10 Linux 64-bit.
Title: Re: TMemoryStream.ReadAnsiString Error
Post by: cai on July 14, 2020, 08:22:47 pm
I think to call ReadAnsiString, you need to call WriteAnsiString First.
if you look in ReadAnsiString source code, you will see it need to read size first, there is no correct size in you stream cause you just WriteByte
Title: Re: TMemoryStream.ReadAnsiString Error
Post by: howardpc on July 14, 2020, 08:26:31 pm
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   Data = 'Testing';
  4. var
  5.   aMemoryStream: TMemoryStream;
  6.   aString:       string;
  7.   i:             Integer;
  8. begin
  9.   aMemoryStream := TMemoryStream.Create;
  10.   for i := 1 to Data.Length do
  11.     aMemoryStream.WriteByte(Ord(Data[i]));
  12.  
  13.   aString := '';
  14.   aMemoryStream.Position := 0;
  15.   for i := 1 to aMemoryStream.Size do
  16.     aString := aString + Chr(aMemoryStream.ReadByte);
  17.   ShowMessage(aString);
  18.  
  19.   aMemoryStream.Clear;
  20.   aMemoryStream.WriteAnsiString(Data);
  21.   aMemoryStream.Position := 0;
  22.   aString := aMemoryStream.ReadAnsiString;
  23.   ShowMessage(aString);
  24.  
  25.   aMemoryStream.Free
  26. end;
                         
Title: Re: TMemoryStream.ReadAnsiString Error
Post by: Handoko on July 14, 2020, 08:32:42 pm
cai, you're right. And thanks howardpc. Now, I have better understanding about streams.

I was writing code to read data from the web and I need to do some processings before showing it on the screen. I know how to do it now.
Title: Re: [SOLVED] TMemoryStream.ReadAnsiString Error
Post by: TRon on July 14, 2020, 08:49:01 pm
@Handoko:
Alternatively, and probably depending on your needs/requirements, you could perhaps also make use of TStringStream https://www.freepascal.org/docs-html/rtl/classes/tstringstream.html

It is a descendant of TMemoryStream so you would also still be able to manipulate the memory.
Title: Re: [SOLVED] TMemoryStream.ReadAnsiString Error
Post by: Handoko on July 15, 2020, 04:51:54 am
Yes, you're right. TStringStream has DataString property, which make it easier to use.

Thank you for the suggestion.
Title: Re: TMemoryStream.ReadAnsiString Error
Post by: PascalDragon on July 15, 2020, 09:47:50 am
I think to call ReadAnsiString, you need to call WriteAnsiString First.

Correct. ReadAnsiString expects a specific format inside the stream, namely the length followed by the string data and that is created by WriteAnsiString. To read plain string data you need to use the other Read methods and manually preallocate enough space.
Title: Re: [SOLVED] TMemoryStream.ReadAnsiString Error
Post by: rvk on July 15, 2020, 02:56:27 pm
Yes, if you have a choice, TStringStream is easiest.

If you don't have a choice you can also access TMemoryStream.Memory directly.

Code: Pascal  [Select][+][-]
  1. function MemoryStreamToString(M: TMemoryStream): string;
  2. begin
  3.   SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char));
  4. end;
TinyPortal © 2005-2018