From 82b85bcb9b1582b859b6378da7f7ac8430261833 Mon Sep 17 00:00:00 2001 From: azure-sdk Date: Tue, 30 Sep 2025 00:04:46 +0000 Subject: [PATCH] Update Azure Core Shared Codes 2025-09-29_17:03:17 --- .../Azure.Core.Shared/StringRequestContent.cs | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/assets/Azure.Core.Shared/StringRequestContent.cs b/src/assets/Azure.Core.Shared/StringRequestContent.cs index 58a40c46661..aa95208d503 100644 --- a/src/assets/Azure.Core.Shared/StringRequestContent.cs +++ b/src/assets/Azure.Core.Shared/StringRequestContent.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Buffers; using System.IO; using System.Text; using System.Threading; @@ -11,39 +12,50 @@ namespace Azure.Core { internal class StringRequestContent : RequestContent { - private readonly byte[] _bytes; + private readonly byte[] _buffer; + private readonly int _actualByteCount; public StringRequestContent(string value) { - _bytes = Encoding.UTF8.GetBytes(value); +#if NET6_0_OR_GREATER + var byteCount = Encoding.UTF8.GetMaxByteCount(value.Length); + _buffer = ArrayPool.Shared.Rent(byteCount); + _actualByteCount = Encoding.UTF8.GetBytes(value, _buffer); +#else + _buffer = Encoding.UTF8.GetBytes(value); + _actualByteCount = _buffer.Length; +#endif } public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) { #if NET6_0_OR_GREATER - await stream.WriteAsync(_bytes.AsMemory(), cancellation).ConfigureAwait(false); + await stream.WriteAsync(_buffer.AsMemory(0, _actualByteCount), cancellation).ConfigureAwait(false); #else - await stream.WriteAsync(_bytes, 0, _bytes.Length, cancellation).ConfigureAwait(false); + await stream.WriteAsync(_buffer, 0, _actualByteCount, cancellation).ConfigureAwait(false); #endif } public override void WriteTo(Stream stream, CancellationToken cancellation) { #if NET6_0_OR_GREATER - stream.Write(_bytes.AsSpan()); + stream.Write(_buffer.AsSpan(0, _actualByteCount)); #else - stream.Write(_bytes, 0, _bytes.Length); + stream.Write(_buffer, 0, _actualByteCount); #endif } public override bool TryComputeLength(out long length) { - length = _bytes.Length; + length = _actualByteCount; return true; } public override void Dispose() { +#if NET6_0_OR_GREATER + ArrayPool.Shared.Return(_buffer, clearArray: true); +#endif } } }