[fpc-devel] Readonly Memory Stream?

Michael Van Canneyt michael at freepascal.org
Wed Feb 13 10:47:46 CET 2019



On Wed, 13 Feb 2019, LacaK wrote:

> 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.


More information about the fpc-devel mailing list