From db10ffc9e9fa0c67d92a3f7f2b73894ee97fa8e3 Mon Sep 17 00:00:00 2001 From: Daniel Marbach Date: Thu, 25 Mar 2021 23:46:55 +0100 Subject: [PATCH] ReadStreamToArraySegment use BufferListStream directly when available (#19823) --- .../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)