Skip to content

Commit e72d9c9

Browse files
authored
Merge pull request #2 from Shuttle/async
Async
2 parents da90eab + 97c36d6 commit e72d9c9

7 files changed

Lines changed: 103 additions & 13 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ Provides `Stream` extensions.
88

99
``` c#
1010
byte[] ToBytes(this Stream stream)
11+
Task<byte[]> ToBytesAsync(this Stream stream)
1112
```
1213

1314
Returns the given `stream` as a `byte` array.
1415

1516
``` c#
1617
Stream Copy(this Stream stream)
18+
Task<Stream> CopyAsync(this Stream stream)
1719
```
1820

1921
Creates a copyof the given `stream`. THe copy will be at position 0 and the source `stream` will remain at its original position.

Shuttle.Core.Streams.Tests/Shuttle.Core.Streams.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net6.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
9-
<PackageReference Include="NUnit" Version="3.13.3" />
10-
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
9+
<PackageReference Include="NUnit" Version="3.14.0" />
10+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

Shuttle.Core.Streams.Tests/StreamExtensionsFixture.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Threading.Tasks;
23
using NUnit.Framework;
34

45
namespace Shuttle.Core.Streams.Tests
@@ -17,6 +18,17 @@ public void Should_be_able_to_convert_a_stream_to_an_array_of_bytes()
1718
Assert.AreEqual(4, bytes[4]);
1819
}
1920

21+
[Test]
22+
public async Task Should_be_able_to_convert_a_stream_to_an_array_of_bytes_async()
23+
{
24+
var stream = new MemoryStream(new byte[] {0, 1, 2, 3, 4});
25+
var bytes = await stream.ToBytesAsync();
26+
27+
Assert.AreEqual(5, bytes.Length);
28+
Assert.AreEqual(0, bytes[0]);
29+
Assert.AreEqual(4, bytes[4]);
30+
}
31+
2032
[Test]
2133
public void Should_be_able_to_make_a_copy_of_a_stream()
2234
{
@@ -56,5 +68,45 @@ public void Should_be_able_to_make_a_readonly_copy_of_a_stream()
5668
Assert.AreEqual(0, copy.Position);
5769
Assert.AreEqual(5, stream.Position);
5870
}
71+
72+
[Test]
73+
public async Task Should_be_able_to_make_a_copy_of_a_stream_async()
74+
{
75+
var bytes = new byte[] {0, 1, 2, 3, 4};
76+
var stream = new MemoryStream(bytes);
77+
var output = new MemoryStream();
78+
79+
await stream.CopyToAsync(output);
80+
81+
Assert.AreEqual(5, output.Length);
82+
Assert.AreEqual(5, output.Position);
83+
Assert.AreEqual(5, stream.Position);
84+
85+
var copy = await stream.CopyAsync();
86+
87+
Assert.AreEqual(5, copy.Length);
88+
Assert.AreEqual(0, copy.Position);
89+
Assert.AreEqual(5, stream.Position);
90+
}
91+
92+
[Test]
93+
public async Task Should_be_able_to_make_a_readonly_copy_of_a_stream_async()
94+
{
95+
var bytes = new byte[] {0, 1, 2, 3, 4};
96+
var stream = new MemoryStream(bytes, 0, bytes.Length, false, true);
97+
var output = new MemoryStream();
98+
99+
await stream.CopyToAsync(output);
100+
101+
Assert.AreEqual(5, output.Length);
102+
Assert.AreEqual(5, output.Position);
103+
Assert.AreEqual(5, stream.Position);
104+
105+
var copy = await stream.CopyAsync();
106+
107+
Assert.AreEqual(5, copy.Length);
108+
Assert.AreEqual(0, copy.Position);
109+
Assert.AreEqual(5, stream.Position);
110+
}
59111
}
60112
}

Shuttle.Core.Streams/.package/package.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package>
44
<metadata>
55
<id>Shuttle.Core.Streams</id>
6-
<version>10.1.1</version>
6+
<version>10.2.0</version>
77
<authors>Eben Roux</authors>
88
<owners>Eben Roux</owners>
99
<license type="expression">BSD-3-Clause</license>
@@ -13,10 +13,10 @@
1313
<repository type="git" url="https://github.com/shuttle/Shuttle.Core.Streams.git" />
1414
<projectUrl>https://github.com/shuttle/Shuttle.Core.Streams</projectUrl>
1515
<description>Stream infrastructure utilities.</description>
16-
<copyright>Copyright (c) 2022, Eben Roux</copyright>
16+
<copyright>Copyright (c) 2024, Eben Roux</copyright>
1717
<tags>shuttle stream</tags>
1818
<dependencies>
19-
<dependency id="Shuttle.Core.Contract" version="11.0.0" />
19+
<dependency id="Shuttle.Core.Contract" version="11.1.0" />
2020
</dependencies>
2121
</metadata>
2222
<files>

Shuttle.Core.Streams/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
[assembly: AssemblyTitle(".NET Standard")]
1414
#endif
1515

16-
[assembly: AssemblyVersion("10.1.1.0")]
17-
[assembly: AssemblyCopyright("Copyright (c) 2022, Eben Roux")]
16+
[assembly: AssemblyVersion("10.2.0.0")]
17+
[assembly: AssemblyCopyright("Copyright (c) 2024, Eben Roux")]
1818
[assembly: AssemblyProduct("Shuttle.Core.Streams")]
1919
[assembly: AssemblyCompany("Eben Roux")]
2020
[assembly: AssemblyConfiguration("Release")]
21-
[assembly: AssemblyInformationalVersion("10.1.1")]
21+
[assembly: AssemblyInformationalVersion("10.2.0")]
2222
[assembly: ComVisible(false)]

Shuttle.Core.Streams/Shuttle.Core.Streams.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Shuttle.Core.Contract" Version="11.0.0" />
17+
<PackageReference Include="Shuttle.Core.Contract" Version="11.1.0" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

Shuttle.Core.Streams/StreamExtensions.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Threading.Tasks;
34
using Shuttle.Core.Contract;
45

56
namespace Shuttle.Core.Streams
@@ -13,18 +14,46 @@ public static class StreamExtensions
1314
/// <returns>An array of bytes</returns>
1415
public static byte[] ToBytes(this Stream stream)
1516
{
16-
using (var result = (MemoryStream)stream.Copy())
17+
using (var result = (MemoryStream)Guard.AgainstNull(stream, nameof(stream)).Copy())
1718
{
1819
return result.ToArray();
1920
}
2021
}
2122

23+
/// <summary>
24+
/// Creates an array of bytes from the given stream. The stream position is reset once the operation has completed.
25+
/// </summary>
26+
/// <param name="stream">Input stream</param>
27+
/// <returns>An array of bytes</returns>
28+
public static async Task<byte[]> ToBytesAsync(this Stream stream)
29+
{
30+
using (var result = (MemoryStream)(await Guard.AgainstNull(stream, nameof(stream)).CopyAsync()))
31+
{
32+
return await Task.FromResult(result.ToArray());
33+
}
34+
}
35+
2236
/// <summary>
2337
/// Returns a copy of the given stream. The underlying type used is a `MemoryStream` and if the given `stream` is a `MemoryStream` the operation will attempt to use internal buffer if exposed and return a read-only stream; else a standard `MemoryStream` is used and the `stream` data copied to the that.
2438
/// </summary>
2539
/// <param name="stream">The `Stream` instance that contains the source data.</param>
2640
/// <returns>A new `MemoryStream` object.</returns>
2741
public static Stream Copy(this Stream stream)
42+
{
43+
return CopyAsync(stream, true).GetAwaiter().GetResult();
44+
}
45+
46+
/// <summary>
47+
/// Returns a copy of the given stream. The underlying type used is a `MemoryStream` and if the given `stream` is a `MemoryStream` the operation will attempt to use internal buffer if exposed and return a read-only stream; else a standard `MemoryStream` is used and the `stream` data copied to the that.
48+
/// </summary>
49+
/// <param name="stream">The `Stream` instance that contains the source data.</param>
50+
/// <returns>A new `MemoryStream` object.</returns>
51+
public static async Task<Stream> CopyAsync(this Stream stream)
52+
{
53+
return await CopyAsync(stream, false).ConfigureAwait(false);
54+
}
55+
56+
private static async Task<Stream> CopyAsync(this Stream stream, bool sync)
2857
{
2958
Guard.AgainstNull(stream, nameof(stream));
3059

@@ -44,7 +73,14 @@ public static Stream Copy(this Stream stream)
4473
{
4574
stream.Seek(0, SeekOrigin.Begin);
4675

47-
stream.CopyTo(result);
76+
if (sync)
77+
{
78+
stream.CopyTo(result);
79+
}
80+
else
81+
{
82+
await stream.CopyToAsync(result).ConfigureAwait(false);
83+
}
4884

4985
result.Seek(0, SeekOrigin.Begin);
5086
}

0 commit comments

Comments
 (0)