// zero-copy BinaryData creation from MemoryStream var stream = new MemoryStream(); stream.Write([1, 2, 3]); // some data is written to the memory stream stream.Flush(); // this requires a memory copy var dataViaStreamCopy = BinaryData.FromStream(stream); // this does not require a memory copy var dataViaBufferAccess = new BinaryData(stream.GetBuffer().AsMemory(0, (int)stream.Position)); // what BinaryData does internally is the same: // https://github.com/dotnet/dotnet/blob/main/src/runtime/src/libraries/System.Memory.Data/src/System/BinaryData.cs#L305