1- using System ;
2- using System . IO ;
1+ using System . IO ;
32using System . IO . Pipelines ;
43using System . Threading ;
54using System . Threading . Tasks ;
@@ -34,7 +33,17 @@ public TStream EncodeTo<TStream>(TStream stream) where TStream : Stream
3433 }
3534
3635 /// <summary>
37- /// Writes the object as bencode to the specified <see cref="PipeWriter"/>.
36+ /// Writes the object as bencode to the specified <see cref="PipeWriter"/> without flushing the writer,
37+ /// you should do that manually.
38+ /// </summary>
39+ /// <param name="writer">The writer to write to.</param>
40+ public void EncodeTo ( PipeWriter writer )
41+ {
42+ EncodeObject ( writer ) ;
43+ }
44+
45+ /// <summary>
46+ /// Writes the object as bencode to the specified <see cref="PipeWriter"/> and flushes the writer afterwards.
3847 /// </summary>
3948 /// <param name="writer">The writer to write to.</param>
4049 /// <param name="cancellationToken"></param>
@@ -54,8 +63,18 @@ public ValueTask<FlushResult> EncodeToAsync(PipeWriter writer, CancellationToken
5463 /// Implementations of this method should encode their underlying value to bencode and write it to the <see cref="PipeWriter"/>.
5564 /// </summary>
5665 /// <param name="writer">The writer to encode to.</param>
66+ protected abstract void EncodeObject ( PipeWriter writer ) ;
67+
68+ /// <summary>
69+ /// Encodes and writes the underlying value to the <see cref="PipeWriter"/> and flushes the writer afterwards.
70+ /// </summary>
71+ /// <param name="writer">The writer to encode to.</param>
5772 /// <param name="cancellationToken"></param>
58- protected abstract ValueTask < FlushResult > EncodeObjectAsync ( PipeWriter writer , CancellationToken cancellationToken = default ) ;
73+ protected virtual ValueTask < FlushResult > EncodeObjectAsync ( PipeWriter writer , CancellationToken cancellationToken )
74+ {
75+ EncodeObject ( writer ) ;
76+ return writer . FlushAsync ( cancellationToken ) ;
77+ }
5978 }
6079
6180 /// <summary>
0 commit comments