diff --git a/sdk/src/Core/Amazon.Runtime/Internal/Util/StringUtils.cs b/sdk/src/Core/Amazon.Runtime/Internal/Util/StringUtils.cs index 37ea49c9ebe5..d635e96ae8b9 100644 --- a/sdk/src/Core/Amazon.Runtime/Internal/Util/StringUtils.cs +++ b/sdk/src/Core/Amazon.Runtime/Internal/Util/StringUtils.cs @@ -21,6 +21,7 @@ using Amazon.Util; using System.Linq; using System.Diagnostics.CodeAnalysis; +using System.Buffers; namespace Amazon.Runtime.Internal.Util { @@ -32,7 +33,7 @@ public static class StringUtils private static readonly Encoding UTF_8 = Encoding.UTF8; private static readonly char[] rfc7230HeaderFieldValueDelimeters = "\"(),/:;<=>?@[\\]{}".ToCharArray(); - public static string FromString(String value) + public static string FromString(String value) { return value; } @@ -49,14 +50,30 @@ public static string FromString(ConstantClass value) public static string FromMemoryStream(MemoryStream value) { - return Convert.ToBase64String(value.ToArray()); + if (value.TryGetBuffer(out var buffer)) + { + return Convert.ToBase64String(buffer.Array, buffer.Offset, buffer.Count); + } + else + { + var array = ArrayPool.Shared.Rent((int)value.Length); + try + { + value.Read(array, 0, (int)value.Length); + return Convert.ToBase64String(array, 0, (int)value.Length); + } + finally + { + ArrayPool.Shared.Return(array); + } + } } public static string FromInt(int value) { return value.ToString(CultureInfo.InvariantCulture); } - + public static string FromInt(int? value) { if (!value.HasValue) @@ -348,7 +365,7 @@ public static string FromValueTypeList(IEnumerable values) where T : struc /// List of T /// Header value representing the list of T [SuppressMessage("Microsoft.Globalization", "CA1308", Justification = "Value is not surfaced to user. Booleans have been lowercased by SDK precedent.")] - public static string FromValueTypeList(List values) where T : struct + public static string FromValueTypeList(List values) where T : struct { // ToString() on boolean types automatically Pascal Cases. Xml-based protocols // are case sensitive and accept "true" and "false" as the valid set of booleans. @@ -404,7 +421,7 @@ private static string EscapeHeaderListEntry(string headerListEntry) if (headerListEntry.IndexOfAny(rfc7230HeaderFieldValueDelimeters) != -1) { return $"\"{headerListEntry.Replace("\"", "\\\"")}\""; - } + } return headerListEntry; }