Resizing or add data
Unlike a stream, TManagedMemory does not have a Position pointer. It's not designed for sequential access. However, you can easily resize it or append data:
- Grow / Shrink / ScaleTo: These methods resize the buffer. Be aware that this is a re-allocation and copy, not an in-place resize, so it can be an expensive operation.
- Append(Data: TUInt8Array): This is a shortcut that Grows the buffer by the size of Data and then writes Data to the end.
- Push(Data: TUInt8Array): Prepends data, pushing existing content back (another re-allocation).
- Pop(BlockSize: int32): Reads and removes a block of bytes from the beginning of the buffer, shrinking it.
- Insert / Delete: Injects or deletes X a block of bytes from anywhere in the buffer, shrinking or growing it automatically for you
So TManagedMemory is what you use when you need to control the structure of a binary packet, write your own database engine or create a graphics application. TMemoryStream is what you use when you just need to stream data sequentially.