Skip to content

Commit

Permalink
Use different constructor of MemoryStream for stream properties that …
Browse files Browse the repository at this point in the history
…allows access to the underlying buffer. (#3410)
  • Loading branch information
normj authored Aug 2, 2024
1 parent 391da90 commit dc3a970
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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}'");
}
Expand All @@ -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; }
}
}
}

0 comments on commit dc3a970

Please sign in to comment.