From dc3a970e1808a08440ab49a9a824b1e9e749877c Mon Sep 17 00:00:00 2001 From: Norm Johanson Date: Fri, 2 Aug 2024 10:20:46 -0700 Subject: [PATCH] Use different constructor of MemoryStream for stream properties that allows access to the underlying buffer. (#3410) --- .../Transform/SimpleTypeUnmarshaller.cs | 4 ++-- .../SimpleTypeUnmarshallerTests.cs | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/sdk/src/Core/Amazon.Runtime/Internal/Transform/SimpleTypeUnmarshaller.cs b/sdk/src/Core/Amazon.Runtime/Internal/Transform/SimpleTypeUnmarshaller.cs index 99ab8770cb1d..eaff5e86a762 100644 --- a/sdk/src/Core/Amazon.Runtime/Internal/Transform/SimpleTypeUnmarshaller.cs +++ b/sdk/src/Core/Amazon.Runtime/Internal/Transform/SimpleTypeUnmarshaller.cs @@ -761,7 +761,7 @@ public static MemoryStreamUnmarshaller GetInstance() public MemoryStream Unmarshall(XmlUnmarshallerContext context) { byte[] bytes = Convert.FromBase64String(context.ReadText()); - MemoryStream stream = new MemoryStream(bytes); + MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length, true, true); return stream; } @@ -772,7 +772,7 @@ public MemoryStream Unmarshall(JsonUnmarshallerContext context) return null; byte[] bytes = Convert.FromBase64String(context.ReadText()); - MemoryStream stream = new MemoryStream(bytes); + MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length, true, true); return stream; } } diff --git a/sdk/test/UnitTests/Custom/Marshalling/SimpleTypeUnmarshallerTests.cs b/sdk/test/UnitTests/Custom/Marshalling/SimpleTypeUnmarshallerTests.cs index a7d8e872357e..7c5b4641ba00 100644 --- a/sdk/test/UnitTests/Custom/Marshalling/SimpleTypeUnmarshallerTests.cs +++ b/sdk/test/UnitTests/Custom/Marshalling/SimpleTypeUnmarshallerTests.cs @@ -16,14 +16,15 @@ using AWSSDK_DotNet.UnitTests; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.IO; namespace AWSSDK.UnitTests { [TestClass] public class SimpleTypeUnmarshallerTests { - private static readonly string JsonWithValues = "{'Priority': 1, 'ReservoirQuotaTTL': 1533081600, 'StartTimeISO8601': '2018-08-01T00:00:00.0000000Z', 'StartTimeEpoch': 1533081600, 'StartTimeRFC822': 'Wed, 01 Aug 2018 00:00:00 GMT'}".Replace("'", "\""); - private static readonly string JsonWithNullValues = "{'Priority': null, 'ReservoirQuotaTTL': null, 'StartTimeISO8601': null, 'StartTimeEpoch': null, 'StartTimeRFC822': null}".Replace("'", "\""); + private static readonly string JsonWithValues = "{'Priority': 1, 'ReservoirQuotaTTL': 1533081600, 'StartTimeISO8601': '2018-08-01T00:00:00.0000000Z', 'StartTimeEpoch': 1533081600, 'StartTimeRFC822': 'Wed, 01 Aug 2018 00:00:00 GMT', 'Stream':'SEVMTE8='}".Replace("'", "\""); + private static readonly string JsonWithNullValues = "{'Priority': null, 'ReservoirQuotaTTL': null, 'StartTimeISO8601': null, 'StartTimeEpoch': null, 'StartTimeRFC822': null, 'Stream': null}".Replace("'", "\""); [TestMethod] [TestCategory("UnitTest")] @@ -64,7 +65,7 @@ private Model UnmarshallModel(string json) int targetDepth = context.CurrentDepth; var model = new Model(); bool isSetPriority = false, isSetReservoirQuotaTTL = false, isSetStartTimeISO8601 = false, - isSetStartTimeEpoch = false, isSetStartTimeRFC822 = false; + isSetStartTimeEpoch = false, isSetStartTimeRFC822 = false, isSetStream = false; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("Priority", targetDepth)) @@ -97,8 +98,21 @@ private Model UnmarshallModel(string json) model.StartTimeRFC822 = unmarshaller.Unmarshall(context); isSetStartTimeRFC822 = true; } + if (context.TestExpression("Stream", targetDepth)) + { + var unmarshaller = MemoryStreamUnmarshaller.Instance; + model.Stream = unmarshaller.Unmarshall(context); + + if (model.Stream != null) + { + // V4 added support for getting access to the buffer to allow customers to avoid copying data. + Assert.IsTrue(model.Stream.GetBuffer().Length > 0); + } + + isSetStream = true; + } } - if (!(isSetPriority && isSetReservoirQuotaTTL && isSetStartTimeISO8601 && isSetStartTimeEpoch && isSetStartTimeRFC822)) + if (!(isSetPriority && isSetReservoirQuotaTTL && isSetStartTimeISO8601 && isSetStartTimeEpoch && isSetStartTimeRFC822 && isSetStream)) { throw new Exception($"Could not parse all properties in JSON '{json}'"); } @@ -112,6 +126,7 @@ class Model public DateTime StartTimeISO8601 { get; set; } public DateTime StartTimeEpoch { get; set; } public DateTime StartTimeRFC822 { get; set; } + public MemoryStream Stream { get; set; } } } }