The bytes are written to MemoryStream object and there is a need to get underlying buffer to save it to file
MemoryStream ms = new MemoryStream();
// ms.Write(...)
// ms.Write(...)
// etc... some bytes are written to the stream
byte[] data = ms.GetBuffer();
int length = data.Length;
However the returned data is of ms capacity rather than the real ms length.
Is it better (faster, safer, …) to set ms capacity to its length or allocate a data and copy ms contents into it?
just use the member-method
ms.ToArray()