-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBObjectParser.cs
More file actions
73 lines (64 loc) · 2.94 KB
/
Copy pathBObjectParser.cs
File metadata and controls
73 lines (64 loc) · 2.94 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
using System.IO;
using System.IO.Pipelines;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BencodeNET.IO;
using BencodeNET.Objects;
namespace BencodeNET.Parsing
{
/// <summary>
/// Abstract base parser for parsing bencode of specific types.
/// </summary>
/// <typeparam name="T">The type of bencode object the parser returns.</typeparam>
public abstract class BObjectParser<T> : IBObjectParser<T> where T : IBObject
{
/// <summary>
/// The encoding used for parsing.
/// </summary>
public abstract Encoding Encoding { get; }
IBObject IBObjectParser.Parse(Stream stream)
{
return Parse(stream);
}
IBObject IBObjectParser.Parse(BencodeReader reader)
{
return Parse(reader);
}
async ValueTask<IBObject> IBObjectParser.ParseAsync(PipeReader pipeReader, CancellationToken cancellationToken)
{
return await ParseAsync(new PipeBencodeReader(pipeReader), cancellationToken).ConfigureAwait(false);
}
async ValueTask<IBObject> IBObjectParser.ParseAsync(PipeBencodeReader pipeReader, CancellationToken cancellationToken)
{
return await ParseAsync(pipeReader, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
/// </summary>
/// <param name="stream">The stream to parse.</param>
/// <returns>The parsed object.</returns>
public virtual T Parse(Stream stream) => Parse(new BencodeReader(stream, leaveOpen: true));
/// <summary>
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from a <see cref="BencodeReader"/>.
/// </summary>
/// <param name="reader">The reader to read from.</param>
/// <returns>The parsed object.</returns>
public abstract T Parse(BencodeReader reader);
/// <summary>
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from a <see cref="PipeReader"/>.
/// </summary>
/// <param name="pipeReader">The pipe reader to read from.</param>
/// <param name="cancellationToken"></param>
/// <returns>The parsed object.</returns>
public ValueTask<T> ParseAsync(PipeReader pipeReader, CancellationToken cancellationToken = default)
=> ParseAsync(new PipeBencodeReader(pipeReader), cancellationToken);
/// <summary>
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from a <see cref="PipeBencodeReader"/>.
/// </summary>
/// <param name="pipeReader">The pipe reader to read from.</param>
/// <param name="cancellationToken"></param>
/// <returns>The parsed object.</returns>
public abstract ValueTask<T> ParseAsync(PipeBencodeReader pipeReader, CancellationToken cancellationToken = default);
}
}