From Clomosy Docs
TclMemoryStream is a stream that stores its data in dynamic memory.
Use TclMemoryStream to store data in a dynamic memory buffer that is enhanced with file-like access capabilities. TclMemoryStream provides the general I/O capabilities of a stream object while introducing methods and properties to manage a dynamic memory buffer.
Memory streams are useful as intermediary objects that can hold information as well as read it from or write it to another storage medium. They provide a useful format for comparing the contents of streams, or for manipulating data that is stored in a less accessible medium.
Pros:
- Fast Access: Memory access is faster than file I/O operations.
- Easy Manipulation: Ideal for temporary data storage and manipulation.
- No Disk I/O: Since there is no need for disk access, it is faster for operations that require frequent reading/writing.
Cons:
- Memory Usage: Limited by the amount of system memory available. Big data can consume significant memory.
- Temporary Storage: Since data is stored in volatile memory, it is lost when the application is closed.
| Feature | Use of | Definition |
|---|---|---|
| TCLMemoryStream | MemStream: TCLMemoryStream; | A variable belonging to the TCLMemoryStream class is created. |
| Create | MemStream = TCLMemoryStream.Create; | A new TCLMemoryStream component is added to the form. |
| AsBase64 | noteMemo.Lines.Text = MemStream.AsBase64; | Returns base64 string data. |
| Size | MemStream.Size | Determines or returns the size of the memory stream in bytes. |
| Clear | MemStream.Clear; | Clears the stream and resets its size. |
| Free | MemStream.Free; | It releases the memory occupied and destroys the object. |
Example
This example demonstrates decoding and displaying a Base64 encoded image on a form with an image and a memo component. The noteMemoOnClick procedure converts the Base64 encoded text in the memo to a memory stream, and then uses this stream to load the image into the SourceImg component.
var
Form1:TCLForm;
SourceImg : TclImage;
noteMemo : TclMemo;
btn1 : TclButton;
void noteMemoOnClick;
var
LMemStream:TCLMemoryStream;
{
LMemStream = TCLMemoryStream.Create;
LMemStream = Clomosy.Base64ToStream(noteMemo.Lines.Text);
SourceImg.Bitmap.LoadFromStream(LMemStream);
}
{
Form1 = TCLForm.Create(Self);
Form1.AddAssetFromUrl('https://clomosy.com/educa/ok.png');
noteMemo = Form1.AddNewMemo(Form1,'noteMemo','---');
noteMemo.Align = alMostTop;
noteMemo.Height = 200;
noteMemo.StyledSettings = ssFamily;
noteMemo.TextSettings.WordWrap = True;
noteMemo.Lines.Text = Clomosy.FileToBase64(clPathCombine('ok.png',Clomosy.AppFilesPath));
SourceImg = Form1.AddNewImage(Form1,'SourceImg');
SourceImg.Align = alClient;
Form1.setImage(SourceImg,'https://clomosy.com/educa/hint.png');
btn1 = Form1.AddNewButton(Form1,'btn1','Upload');
btn1.StyledSettings = ssFamily;
btn1.TextSettings.Font.Size = 20;
btn1.TextSettings.FontColor = clAlphaColor.clHexToColor('#8a067c');
btn1.Align = alBottom;
Form1.AddNewEvent(btn1,tbeOnClick,'noteMemoOnClick');
Form1.Run;
}