Optimal performance of an endoreversible cycle operating between a heat source and sink of finite capacities