Skip to content

Commit 90011e1

Browse files
committed
Add support for encoding to stream async
Using a PipeWriter internally.
1 parent 22c0eea commit 90011e1

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

BencodeNET.Tests/Objects/BDictionaryTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.IO.Pipelines;
45
using System.Linq;
56
using System.Text;
@@ -424,5 +425,17 @@ public async Task WriteToPipeWriterAsync()
424425
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
425426
result.Should().Be("d3:key5:valuee");
426427
}
428+
429+
[Fact]
430+
public async Task WriteToStreamAsync()
431+
{
432+
var dict = new BDictionary { { "key", "value" } };
433+
434+
var stream = new MemoryStream();
435+
await dict.EncodeToAsync(stream);
436+
437+
var result = Encoding.UTF8.GetString(stream.ToArray());
438+
result.Should().Be("d3:key5:valuee");
439+
}
427440
}
428441
}

BencodeNET.Tests/Objects/BListTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.IO.Pipelines;
34
using System.Linq;
45
using System.Text;
@@ -237,5 +238,17 @@ public async Task WriteToPipeWriterAsync()
237238
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
238239
result.Should().Be("li1ei2e3:abce");
239240
}
241+
242+
[Fact]
243+
public async Task WriteToStreamAsync()
244+
{
245+
var blist = new BList { 1, 2, "abc" };
246+
247+
var stream = new MemoryStream();
248+
await blist.EncodeToAsync(stream);
249+
250+
var result = Encoding.UTF8.GetString(stream.ToArray());
251+
result.Should().Be("li1ei2e3:abce");
252+
}
240253
}
241254
}

BencodeNET.Tests/Objects/BNumberTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,17 @@ public async Task WriteToPipeWriterAsync()
369369
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
370370
result.Should().Be("i1234e");
371371
}
372+
373+
[Fact]
374+
public async Task WriteToStreamAsync()
375+
{
376+
var bnumber = new BNumber(1234);
377+
378+
var ms = new MemoryStream();
379+
await bnumber.EncodeToAsync(ms);
380+
381+
var result = Encoding.UTF8.GetString(ms.ToArray());
382+
result.Should().Be("i1234e");
383+
}
372384
}
373385
}

BencodeNET.Tests/Objects/BStringTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,17 @@ public async Task WriteToPipeWriterAsync()
309309
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
310310
result.Should().Be("21:æøå äö èéê ñ");
311311
}
312+
313+
[Fact]
314+
public async Task WriteToStreamAsync()
315+
{
316+
var bstring = new BString("æøå äö èéê ñ");
317+
318+
var stream = new MemoryStream();
319+
await bstring.EncodeToAsync(stream);
320+
321+
var result = Encoding.UTF8.GetString(stream.ToArray());
322+
result.Should().Be("21:æøå äö èéê ñ");
323+
}
312324
}
313325
}

BencodeNET/Objects/BObject.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ public ValueTask<FlushResult> EncodeToAsync(PipeWriter writer, CancellationToken
5252
return EncodeObjectAsync(writer, cancellationToken);
5353
}
5454

55+
/// <summary>
56+
/// Writes the object asynchronously as bencode to the specified <see cref="Stream"/> using a <see cref="PipeWriter"/>.
57+
/// </summary>
58+
/// <param name="stream">The stream to write to.</param>
59+
/// <param name="writerOptions">The options for the <see cref="PipeWriter"/>.</param>
60+
/// <param name="cancellationToken"></param>
61+
public ValueTask<FlushResult> EncodeToAsync(Stream stream, StreamPipeWriterOptions writerOptions = null, CancellationToken cancellationToken = default)
62+
{
63+
return EncodeObjectAsync(PipeWriter.Create(stream, writerOptions), cancellationToken);
64+
}
65+
5566
/// <summary>
5667
/// Implementations of this method should encode their
5768
/// underlying value to bencode and write it to the stream.

BencodeNET/Objects/IBObject.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,13 @@ public interface IBObject
3636
/// <param name="writer">The writer to write to.</param>
3737
/// <param name="cancellationToken"></param>
3838
ValueTask<FlushResult> EncodeToAsync(PipeWriter writer, CancellationToken cancellationToken = default);
39+
40+
/// <summary>
41+
/// Writes the object asynchronously as bencode to the specified <see cref="Stream"/> using a <see cref="PipeWriter"/>.
42+
/// </summary>
43+
/// <param name="stream">The stream to write to.</param>
44+
/// <param name="writerOptions">The options for the <see cref="PipeWriter"/>.</param>
45+
/// <param name="cancellationToken"></param>
46+
ValueTask<FlushResult> EncodeToAsync(Stream stream, StreamPipeWriterOptions writerOptions = null, CancellationToken cancellationToken = default);
3947
}
4048
}

0 commit comments

Comments
 (0)