Skip to content

Commit 015935a

Browse files
committed
Clean up IBencodeParser interface and convert methods to extensions
1 parent 90011e1 commit 015935a

File tree

3 files changed

+58
-92
lines changed

3 files changed

+58
-92
lines changed

BencodeNET/Parsing/BencodeParser.cs

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.IO;
3-
using System.IO.Pipelines;
42
using System.Text;
53
using System.Threading;
64
using System.Threading.Tasks;
@@ -15,7 +13,10 @@ namespace BencodeNET.Parsing
1513
/// </summary>
1614
public class BencodeParser : IBencodeParser
1715
{
18-
protected BObjectParserList Parsers { get; }
16+
/// <summary>
17+
/// List of parsers used by the <see cref="BencodeParser"/>.
18+
/// </summary>
19+
public BObjectParserList Parsers { get; }
1920

2021
/// <summary>
2122
/// The encoding use for parsing.
@@ -49,33 +50,6 @@ public BencodeParser(Encoding encoding = null)
4950
};
5051
}
5152

52-
/// <summary>
53-
/// Parses a stream into an <see cref="IBObject"/>.
54-
/// </summary>
55-
/// <param name="stream">The stream to parse.</param>
56-
/// <returns>The parsed object.</returns>
57-
public virtual IBObject Parse(Stream stream)
58-
{
59-
using (var reader = new BencodeReader(stream, leaveOpen: true))
60-
{
61-
return Parse(reader);
62-
}
63-
}
64-
65-
/// <summary>
66-
/// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
67-
/// </summary>
68-
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
69-
/// <param name="stream">The stream to parse.</param>
70-
/// <returns>The parsed object.</returns>
71-
public virtual T Parse<T>(Stream stream) where T : class, IBObject
72-
{
73-
using (var reader = new BencodeReader(stream, leaveOpen: true))
74-
{
75-
return Parse<T>(reader);
76-
}
77-
}
78-
7953
/// <summary>
8054
/// Parses an <see cref="IBObject"/> from the reader.
8155
/// </summary>
@@ -118,25 +92,6 @@ public virtual T Parse<T>(BencodeReader reader) where T : class, IBObject
11892
return parser.Parse(reader);
11993
}
12094

121-
/// <summary>
122-
/// Parse an <see cref="IBObject"/> from the <see cref="PipeReader"/>.
123-
/// </summary>
124-
public virtual ValueTask<IBObject> ParseAsync(PipeReader pipeReader, CancellationToken cancellationToken = default)
125-
{
126-
var reader = new PipeBencodeReader(pipeReader);
127-
return ParseAsync(reader, cancellationToken);
128-
}
129-
130-
/// <summary>
131-
/// Parse an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="PipeReader"/>.
132-
/// </summary>
133-
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
134-
public virtual ValueTask<T> ParseAsync<T>(PipeReader pipeReader, CancellationToken cancellationToken = default) where T : class, IBObject
135-
{
136-
var reader = new PipeBencodeReader(pipeReader);
137-
return ParseAsync<T>(reader, cancellationToken);
138-
}
139-
14095
/// <summary>
14196
/// Parse an <see cref="IBObject"/> from the <see cref="PipeBencodeReader"/>.
14297
/// </summary>

BencodeNET/Parsing/BencodeParserExtensions.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using System.IO;
2+
using System.IO.Pipelines;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using BencodeNET.IO;
26
using BencodeNET.Objects;
37

48
namespace BencodeNET.Parsing
@@ -94,44 +98,71 @@ public static T Parse<T>(this IBencodeParser parser, string filePath) where T :
9498
}
9599
}
96100

97-
#if !NETSTANDARD1_3
98101
/// <summary>
99-
/// Parses a stream through a <see cref="BufferedStream"/> into an <see cref="IBObject"/>.
100-
/// The input <paramref name="stream"/> is disposed when parsing is completed.
102+
/// Parses a stream into an <see cref="IBObject"/>.
101103
/// </summary>
102104
/// <param name="parser"></param>
103105
/// <param name="stream">The stream to parse.</param>
104-
/// <param name="bufferSize">The buffer size to use. Uses default size of <see cref="BufferedStream"/> if null.</param>
105106
/// <returns>The parsed object.</returns>
106-
public static IBObject ParseBuffered(this IBencodeParser parser, Stream stream, int? bufferSize = null)
107+
public static IBObject Parse(this IBencodeParser parser, Stream stream)
107108
{
108-
;
109-
using (var bufferedStream = bufferSize == null
110-
? new BufferedStream(stream)
111-
: new BufferedStream(stream, bufferSize.Value))
109+
using (var reader = new BencodeReader(stream, leaveOpen: true))
112110
{
113-
return parser.Parse(bufferedStream);
111+
return parser.Parse(reader);
114112
}
115113
}
116114

117115
/// <summary>
118-
/// Parses a stream through a <see cref="BufferedStream"/> into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
119-
/// The input <paramref name="stream"/> is disposed when parsing is completed.
116+
/// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
120117
/// </summary>
121118
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
122119
/// <param name="parser"></param>
123120
/// <param name="stream">The stream to parse.</param>
124-
/// <param name="bufferSize">The buffer size to use. Uses default size of <see cref="BufferedStream"/> if null.</param>
125121
/// <returns>The parsed object.</returns>
126-
public static T ParseBuffered<T>(this IBencodeParser parser, Stream stream, int? bufferSize = null) where T : class, IBObject
122+
public static T Parse<T>(this IBencodeParser parser, Stream stream) where T : class, IBObject
127123
{
128-
using (var bufferedStream = bufferSize == null
129-
? new BufferedStream(stream)
130-
: new BufferedStream(stream, bufferSize.Value))
124+
using (var reader = new BencodeReader(stream, leaveOpen: true))
131125
{
132-
return parser.Parse<T>(bufferedStream);
126+
return parser.Parse<T>(reader);
133127
}
134128
}
135-
#endif
129+
130+
/// <summary>
131+
/// Parses an <see cref="IBObject"/> from the <see cref="PipeReader"/>.
132+
/// </summary>
133+
public static ValueTask<IBObject> ParseAsync(this IBencodeParser parser, PipeReader pipeReader, CancellationToken cancellationToken = default)
134+
{
135+
var reader = new PipeBencodeReader(pipeReader);
136+
return parser.ParseAsync(reader, cancellationToken);
137+
}
138+
139+
/// <summary>
140+
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="PipeReader"/>.
141+
/// </summary>
142+
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
143+
public static ValueTask<T> ParseAsync<T>(this IBencodeParser parser, PipeReader pipeReader, CancellationToken cancellationToken = default) where T : class, IBObject
144+
{
145+
var reader = new PipeBencodeReader(pipeReader);
146+
return parser.ParseAsync<T>(reader, cancellationToken);
147+
}
148+
149+
/// <summary>
150+
/// Parses an <see cref="IBObject"/> from the <see cref="Stream"/> asynchronously using a <see cref="PipeReader"/>.
151+
/// </summary>
152+
public static ValueTask<IBObject> ParseAsync(this IBencodeParser parser, Stream stream, StreamPipeReaderOptions readerOptions = null, CancellationToken cancellationToken = default)
153+
{
154+
var reader = PipeReader.Create(stream, readerOptions);
155+
return parser.ParseAsync(reader, cancellationToken);
156+
}
157+
158+
/// <summary>
159+
/// Parses an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="Stream"/> asynchronously using a <see cref="PipeReader"/>.
160+
/// </summary>
161+
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
162+
public static ValueTask<T> ParseAsync<T>(this IBencodeParser parser, Stream stream, StreamPipeReaderOptions readerOptions = null, CancellationToken cancellationToken = default) where T : class, IBObject
163+
{
164+
var reader = PipeReader.Create(stream, readerOptions);
165+
return parser.ParseAsync<T>(reader, cancellationToken);
166+
}
136167
}
137168
}

BencodeNET/Parsing/IBencodeParser.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,14 @@ namespace BencodeNET.Parsing
1414
public interface IBencodeParser
1515
{
1616
/// <summary>
17-
/// The encoding use for parsing.
18-
/// </summary>
19-
Encoding Encoding { get; }
20-
21-
/// <summary>
22-
/// Parses a stream into an <see cref="IBObject"/>.
17+
/// List of parsers used by the <see cref="IBencodeParser"/>.
2318
/// </summary>
24-
/// <param name="stream">The stream to parse.</param>
25-
/// <returns>The parsed object.</returns>
26-
IBObject Parse(Stream stream);
19+
BObjectParserList Parsers { get; }
2720

2821
/// <summary>
29-
/// Parses a stream into an <see cref="IBObject"/> of type <typeparamref name="T"/>.
22+
/// The encoding use for parsing.
3023
/// </summary>
31-
/// <typeparam name="T">The type of <see cref="IBObject"/> to parse as.</typeparam>
32-
/// <param name="stream">The bencoded string to parse.</param>
33-
/// <returns>The parsed object.</returns>
34-
T Parse<T>(Stream stream) where T : class, IBObject;
24+
Encoding Encoding { get; }
3525

3626
/// <summary>
3727
/// Parses an <see cref="IBObject"/> from the reader.
@@ -46,21 +36,11 @@ public interface IBencodeParser
4636
/// <param name="reader"></param>
4737
T Parse<T>(BencodeReader reader) where T : class, IBObject;
4838

49-
/// <summary>
50-
/// Parse an <see cref="IBObject"/> from the <see cref="PipeReader"/>.
51-
/// </summary>
52-
ValueTask<IBObject> ParseAsync(PipeReader pipeReader, CancellationToken cancellationToken = default);
53-
5439
/// <summary>
5540
/// Parse an <see cref="IBObject"/> from the <see cref="PipeReader"/>.
5641
/// </summary>
5742
ValueTask<IBObject> ParseAsync(PipeBencodeReader pipeReader, CancellationToken cancellationToken = default);
5843

59-
/// <summary>
60-
/// Parse an <see cref="IBObject"/> from the <see cref="PipeBencodeReader"/>.
61-
/// </summary>
62-
ValueTask<T> ParseAsync<T>(PipeReader pipeReader, CancellationToken cancellationToken = default) where T : class, IBObject;
63-
6444
/// <summary>
6545
/// Parse an <see cref="IBObject"/> of type <typeparamref name="T"/> from the <see cref="PipeBencodeReader"/>.
6646
/// </summary>

0 commit comments

Comments
 (0)