Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-02

Readonly Memory Stream?

4 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

LacaK

Hi,

sometimes I have to work with static fixed buffer in memory (for example returned from external API), which I would like to behave like stream.

Then I define:

type TStaticMemoryStream = class(TCustomMemoryStream) end;

and in code I use for example:

    MS := TStaticMemoryStream.Create;
    MS.SetPointer(buffer, bufferLen);
    MS.Position := 0;
    Bitmap.LoadFromStream(MS);
    MS.Free;

I know that it is only very few lines of code, but would it we worth to extend capability of TMemoryStream in RTL to support for example constructor like:

  TMemoryStream.Create(buffer, bufferLen);

which will instantiate memory stream on fixed memory block without possibility to Write?

Or introduce new TCustomMemoryStream descendant in RTL like my TStaticMemoryStream() ?

Only question...

TIA

-Laco.

Reply by email

Michael Van Canneyt
to LacaK
LacaK wrote 28 lines
Hi, sometimes I have to work with static fixed buffer in memory (for example returned from external API), which I would like to behave like stream. Then I define: type TStaticMemoryStream = class(TCustomMemoryStream) end; and in code I use for example:     MS := TStaticMemoryStream.Create;     MS.SetPointer(buffer, bufferLen);     MS.Position := 0;     Bitmap.LoadFromStream(MS);     MS.Free; I know that it is only very few lines of code, but would it we worth to extend capability of TMemoryStream in RTL to support for example constructor like:   TMemoryStream.Create(buffer, bufferLen); which will instantiate memory stream on fixed memory block without possibility to Write? Or introduce new TCustomMemoryStream descendant in RTL like my TStaticMemoryStream() ?

I think the latter approach is best. This could go in the streamex unit, it is made for such extensions.

Michael.

Reply by email

J. Gareth Moreton

Hi guys,

I too had to use such a stream.  Not so much that the stream was read-only, but the memory was already allocated for some reason.  In my case, I had a "TBufferStream", where my constructor had parameters to pass in the start address and the maximum length.
Given it seems to have many common use cases, I would say it's definitely a class worth considering.

Gareth aka. Kit

LacaK wrote 7 lines
and in code I use for example:     MS := TStaticMemoryStream.Create;     MS.SetPointer(buffer, bufferLen);     MS.Position := 0;     Bitmap.LoadFromStream(MS);     MS.Free;
 Btw, you can omit "MS.Position := 0;", since it is already zero for a new
stream.

Yuriy.

Reply by email