Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: stream extensions #89

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<VersionMajor>2</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionMinor>2</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="$(Configuration.Equals('Debug'))">Development</VersionSuffix>
Expand Down
23 changes: 23 additions & 0 deletions src/BB84.Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace BB84.Extensions;

/// <summary>
/// The stream extensions class.
/// </summary>
public static partial class StreamExtensions
{
/// <summary>
/// The method will keep reading (and copying into a <see cref="MemoryStream"/>)
/// until it runs out of data.
/// </summary>
/// <param name="inputStream">The stream to work with.</param>
/// <returns>The <see cref="Stream"/> as <see cref="byte"/> array.</returns>
public static byte[] ToByteArray(this Stream inputStream)
{
byte[] buffer = new byte[16 * 1024];
using MemoryStream memoryStream = new();
int read;
while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
memoryStream.Write(buffer, 0, read);
return memoryStream.ToArray();
}
}
30 changes: 30 additions & 0 deletions tests/BB84.ExtensionsTests/StreamExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using BB84.Extensions;

namespace BB84.ExtensionsTests;

[TestClass]
public sealed partial class StreamExtensionsTests
{
private readonly static Random Random = new();

[DataTestMethod]
[DynamicData(nameof(GetData), DynamicDataSourceType.Method)]
public void ToByteArrayTest(byte[] buffer)
{
MemoryStream stream = new(buffer);

byte[] result = stream.ToByteArray();

Assert.IsTrue(result.SequenceEqual(buffer));
}

private static IEnumerable<object[]> GetData()
{
for (int i = 0; i < 10; i++)
{
byte[] buffer = new byte[10];
Random.NextBytes(buffer);
yield return new object[] { buffer };
}
}
}