1- using System . IO ;
1+ using System ;
2+ using System . IO ;
3+ using System . IO . Pipelines ;
24using System . Text ;
5+ using System . Threading . Tasks ;
6+ using BencodeNET . IO ;
7+ using BencodeNET . Objects ;
8+ using BencodeNET . Parsing ;
9+ using NSubstitute . Core ;
310
411namespace BencodeNET . Tests
512{
@@ -18,5 +25,49 @@ internal static string AsString(this Stream stream, Encoding encoding)
1825 var sr = new StreamReader ( stream , encoding ) ;
1926 return sr . ReadToEnd ( ) ;
2027 }
28+
29+ internal static void SkipBytes ( this BencodeReader reader , int length )
30+ {
31+ reader . Read ( new byte [ length ] ) ;
32+ }
33+
34+ internal static Task SkipBytesAsync ( this PipeBencodeReader reader , int length )
35+ {
36+ return reader . ReadAsync ( new byte [ length ] ) . AsTask ( ) ;
37+ }
38+
39+ internal static ConfiguredCall AndSkipsAhead ( this ConfiguredCall call , int length )
40+ {
41+ return call . AndDoes ( x => x . Arg < BencodeReader > ( ) . SkipBytes ( length ) ) ;
42+ }
43+
44+ internal static ConfiguredCall AndSkipsAheadAsync ( this ConfiguredCall call , int length )
45+ {
46+ return call . AndDoes ( async x => await x . Arg < PipeBencodeReader > ( ) . SkipBytesAsync ( length ) ) ;
47+ }
48+
49+ internal static async ValueTask < IBObject > ParseStringAsync ( this IBObjectParser parser , string bencodedString )
50+ {
51+ var bytes = Encoding . UTF8 . GetBytes ( bencodedString ) . AsMemory ( ) ;
52+ var ( reader , writer ) = new Pipe ( ) ;
53+ await writer . WriteAsync ( bytes ) ;
54+ writer . Complete ( ) ;
55+ return await parser . ParseAsync ( reader ) ;
56+ }
57+
58+ internal static async ValueTask < T > ParseStringAsync < T > ( this IBObjectParser < T > parser , string bencodedString ) where T : IBObject
59+ {
60+ var bytes = Encoding . UTF8 . GetBytes ( bencodedString ) . AsMemory ( ) ;
61+ var ( reader , writer ) = new Pipe ( ) ;
62+ await writer . WriteAsync ( bytes ) ;
63+ writer . Complete ( ) ;
64+ return await parser . ParseAsync ( reader ) ;
65+ }
66+
67+ internal static void Deconstruct ( this Pipe pipe , out PipeReader reader , out PipeWriter writer )
68+ {
69+ reader = pipe . Reader ;
70+ writer = pipe . Writer ;
71+ }
2172 }
2273}
0 commit comments