-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBencodeParserExtensions.cs
More file actions
152 lines (140 loc) · 6.77 KB
/
BencodeParserExtensions.cs
File metadata and controls
152 lines (140 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.IO;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using BencodeNET.IO;
using BencodeNET.Objects;
namespace BencodeNET.Parsing
{
/// <summary>
/// Extensions to simplify parsing strings, byte arrays or files directly.
/// </summary>
public static class BencodeParserExtensions
{
/// <summary>
/// Parses a bencoded string into an <see cref="IBObject"/>.
/// </summary>
/// <param name="parser"></param>
/// <param name="bencodedString">The bencoded string to parse.</param>
/// <returns>The parsed object.</returns>
public static IBObject ParseString(this IBencodeParser parser, string bencodedString)
{
using var stream = bencodedString.AsStream(parser.Encoding);
return parser.Parse(stream);
}
/// <summary>
/// Parses a bencoded array of bytes into an <see cref="IBObject"/>.
/// </summary>
/// <param name="parser"></param>
/// <param name="bytes">The bencoded bytes to parse.</param>
/// <returns>The parsed object.</returns>
public static IBObject Parse(this IBencodeParser parser, byte[] bytes)
{
using var stream = new MemoryStream(bytes);
return parser.Parse(stream);
}
/// <summary>
/// Parses a bencoded file into an <see cref="IBObject"/>.
/// </summary>
/// <param name="parser"></param>
/// <param name="filePath">The path to the file to parse.</param>
/// <returns>The parsed object.</returns>
public static IBObject Parse(this IBencodeParser parser, string filePath)
{
using var stream = File.OpenRead(filePath);
return parser.Parse(stream);
}
/// <summary>
/// Parses a bencoded string into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
/// <param name="parser"></param>
/// <param name="bencodedString">The bencoded string to parse.</param>
/// <returns>The parsed object.</returns>
public static T ParseString<T>(this IBencodeParser parser, string bencodedString) where T : class, IBObject
{
using var stream = bencodedString.AsStream(parser.Encoding);
return parser.Parse<T>(stream);
}
/// <summary>
/// Parses a bencoded array of bytes into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
/// <param name="parser"></param>
/// <param name="bytes">The bencoded bytes to parse.</param>
/// <returns>The parsed object.</returns>
public static T Parse<T>(this IBencodeParser parser, byte[] bytes) where T : class, IBObject
{
using var stream = new MemoryStream(bytes);
return parser.Parse<T>(stream);
}
/// <summary>
/// Parses a bencoded file into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
/// </summary>
/// <param name="parser"></param>
/// <param name="filePath">The path to the file to parse.</param>
/// <returns>The parsed object.</returns>
public static T Parse<T>(this IBencodeParser parser, string filePath) where T : class, IBObject
{
using var stream = File.OpenRead(filePath);
return parser.Parse<T>(stream);
}
/// <summary>
/// Parses a stream into an <see cref="IBObject"/>.
/// </summary>
/// <param name="parser"></param>
/// <param name="stream">The stream to parse.</param>
/// <returns>The parsed object.</returns>
public static IBObject Parse(this IBencodeParser parser, Stream stream)
{
using var reader = new BencodeReader(stream, leaveOpen: true);
return parser.Parse(reader);
}
/// <summary>
/// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
/// <param name="parser"></param>
/// <param name="stream">The stream to parse.</param>
/// <returns>The parsed object.</returns>
public static T Parse<T>(this IBencodeParser parser, Stream stream) where T : class, IBObject
{
using var reader = new BencodeReader(stream, leaveOpen: true);
return parser.Parse<T>(reader);
}
/// <summary>
/// Parses an <see cref="IBObject"/> from the <see cref="PipeReader"/>.
/// </summary>
public static ValueTask<IBObject> ParseAsync(this IBencodeParser parser, PipeReader pipeReader, CancellationToken cancellationToken = default)
{
var reader = new PipeBencodeReader(pipeReader);
return parser.ParseAsync(reader, cancellationToken);
}
/// <summary>
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="PipeReader"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
public static ValueTask<T> ParseAsync<T>(this IBencodeParser parser, PipeReader pipeReader, CancellationToken cancellationToken = default) where T : class, IBObject
{
var reader = new PipeBencodeReader(pipeReader);
return parser.ParseAsync<T>(reader, cancellationToken);
}
/// <summary>
/// Parses an <see cref="IBObject"/> from the <see cref="Stream"/> asynchronously using a <see cref="PipeReader"/>.
/// </summary>
public static ValueTask<IBObject> ParseAsync(this IBencodeParser parser, Stream stream, StreamPipeReaderOptions readerOptions = null, CancellationToken cancellationToken = default)
{
var reader = PipeReader.Create(stream, readerOptions);
return parser.ParseAsync(reader, cancellationToken);
}
/// <summary>
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="Stream"/> asynchronously using a <see cref="PipeReader"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
public static ValueTask<T> ParseAsync<T>(this IBencodeParser parser, Stream stream, StreamPipeReaderOptions readerOptions = null, CancellationToken cancellationToken = default) where T : class, IBObject
{
var reader = PipeReader.Create(stream, readerOptions);
return parser.ParseAsync<T>(reader, cancellationToken);
}
}
}