I'm attempting to get an array of bytes from a TMemoryStream. I'm struggling to understand how a memory stream works. To my understand I should just be able to loop through the MemoryStream using the Position and Size properties.
My expected result is to populate an array of bytes looping through the memory stream however when adjusting the Position property of the Memory stream it jumps from example 0 to 2 and then from 2 to 6.
Data.Position := 0;
repeat
SetLength(arrBytes, Length(arrBytes) + 1);
Data.Read(arrBytes[High(arrBytes)], Data.Size);
Data.Position := Data.Position + 1;
until (Data.Position >= Data.Size -1);
The above code results in partial or in some cases just no data at all. How can I correctly convert the data from a memory stream to an Array of Byte
Data.Position := Data.Position + 1;and debug it I can see that the Position does not change but stays at 0 making a endless loop. Is there something else I am perhaps doing wrong?Data.Read(arrBytes[High(arrBytes)], Data.Size);attempts to fit 1561563 bytes (say) in a single byte. Probably you wantData.Read(arrBytes[High(arrBytes)], 1);and noData.Position := Data.Position + 1. But this is an extremely slow and inefficient approach.