From 671f65de61723289d1159fb1a3a8481e8192aa58 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Thu, 25 Mar 2021 14:02:15 +0100 Subject: [PATCH] ReadStreamToArraySegment use BufferListStream directly when available --- .../src/Amqp/AmqpMessageConverter.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs index 3f0a6b7e9f4d0..fb78f6847b87e 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs @@ -139,10 +139,17 @@ private static ArraySegment ReadStreamToArraySegment(Stream stream) return new ArraySegment(); } - using var memStream = new MemoryStream(StreamBufferSizeInBytes); - stream.CopyTo(memStream, StreamBufferSizeInBytes); - - return new ArraySegment(memStream.ToArray()); + switch (stream) + { + case BufferListStream bufferListStream: + return bufferListStream.ReadBytes((int)stream.Length); + default: + { + using var memStream = new MemoryStream(StreamBufferSizeInBytes); + stream.CopyTo(memStream, StreamBufferSizeInBytes); + return new ArraySegment(memStream.ToArray()); + } + } } public static AmqpMessage SBMessageToAmqpMessage(SBMessage sbMessage)