Skip to content

Commit f85bf9b

Browse files
committed
Added PushStreamContent<T> to avoid delegate allocation.
1 parent ea3383d commit f85bf9b

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

ClassLibraries/Macross.Json.Extensions/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 2.1.0
44

5+
* Added `PushStreamContent<T>` to allow passing of state to the callback invoked
6+
to write data to the request stream which can be used to avoid allocation of a
7+
delegate.
8+
59
* If you are using the 5.0.0+ version of System.Text.Json you can now decorate
610
enum values with `JsonPropertyName` instead of `EnumMember` for
711
`JsonStringEnumMemberConverter`.

ClassLibraries/Macross.Json.Extensions/Code/Macross.Json.Extensions.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
System.Text.Json.Serialization.JsonDelegatedStringConverter
2020
System.Text.Json.Serialization.JsonTypeConverterAdapter
2121
System.Net.Http.PushStreamContent
22+
System.Net.Http.PushStreamContent&lt;T&gt;
2223
</Description>
2324
<Product>Macross.Json.Extensions</Product>
2425
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
2526
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
2627
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2728
<VersionPrefix>2.1.0</VersionPrefix>
28-
<VersionSuffix>beta2</VersionSuffix>
29+
<VersionSuffix>beta3</VersionSuffix>
2930
<FileVersion>$(VersionPrefix)</FileVersion>
3031
<PackageProjectUrl>https://github.com/Macross-Software/core/tree/develop/ClassLibraries/Macross.Json.Extensions</PackageProjectUrl>
3132
</PropertyGroup>

ClassLibraries/Macross.Json.Extensions/Code/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
[assembly: InternalsVisibleTo("Macross.Json.Extensions.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051b7a480b13cecfa44862449486c6884bd6168c325445a0848f48deca9643657c5ae85df3cf6ffdb24d5bd3e9b71dc074ca602544b83511fbce1f83f1d06bb8b7b564414c9d8c719e4e39b95643dfc8e9ce997b5e2a1542a8ff6379186f87b8b695fee82c506170c4fb8ffcbf2e68f4b5d270083f8909c67916500608ce747e9")]
1212

13-
[assembly: AssemblyVersion("2.1.0.21080")]
13+
[assembly: AssemblyVersion("2.1.0.21178")]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
4+
namespace System.Net.Http
5+
{
6+
/// <summary>
7+
/// Provides an <see cref="HttpContent"/> implementation that exposes an output <see cref="Stream"/>
8+
/// which can be written to directly. The ability to push data to the output stream differs from the
9+
/// <see cref="StreamContent"/> where data is pulled and not pushed.
10+
/// </summary>
11+
/// <typeparam name="T">State type.</typeparam>
12+
public class PushStreamContent<T> : HttpContent
13+
{
14+
private readonly Func<Stream, T, Task> _OnStreamAvailable;
15+
private readonly T _State;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="PushStreamContent{T}"/> class.
19+
/// </summary>
20+
/// <param name="onStreamAvailable">Callback function to write to the stream once it is available.</param>
21+
/// <param name="state">State to be passed to the callback function.</param>
22+
public PushStreamContent(Func<Stream, T, Task> onStreamAvailable, T state)
23+
{
24+
_OnStreamAvailable = onStreamAvailable ?? throw new ArgumentNullException(nameof(onStreamAvailable));
25+
_State = state;
26+
}
27+
28+
/// <inheritdoc/>
29+
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context)
30+
=> await _OnStreamAvailable(stream, _State).ConfigureAwait(false);
31+
32+
/// <inheritdoc/>
33+
protected override bool TryComputeLength(out long length)
34+
{
35+
// We can't know the length of the content being pushed to the output stream.
36+
length = -1;
37+
return false;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)