-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBObjectExtensions.cs
More file actions
75 lines (70 loc) · 2.87 KB
/
Copy pathBObjectExtensions.cs
File metadata and controls
75 lines (70 loc) · 2.87 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
using System;
using System.Buffers;
using System.IO;
using System.Text;
namespace BencodeNET.Objects
{
/// <summary>
/// Extensions to simplify encoding directly as a string or byte array.
/// </summary>
public static class BObjectExtensions
{
/// <summary>
/// Encodes the object and returns the result as a string using <see cref="Encoding.UTF8"/>.
/// </summary>
/// <returns>The object bencoded and converted to a string using <see cref="Encoding.UTF8"/>.</returns>
public static string EncodeAsString(this IBObject bobject) => EncodeAsString(bobject, Encoding.UTF8);
/// <summary>
/// Encodes the byte-string as bencode and returns the encoded string.
/// Uses the current value of the <see cref="Encoding"/> property.
/// </summary>
/// <returns>The byte-string as a bencoded string.</returns>
public static string EncodeAsString(this BString bstring) => EncodeAsString(bstring, bstring.Encoding);
/// <summary>
/// Encodes the object and returns the result as a string using the specified encoding.
/// </summary>
/// <param name="bobject"></param>
/// <param name="encoding">The encoding used to convert the encoded bytes to a string.</param>
/// <returns>The object bencoded and converted to a string using the specified encoding.</returns>
public static string EncodeAsString(this IBObject bobject, Encoding encoding)
{
var size = bobject.GetSizeInBytes();
var buffer = ArrayPool<byte>.Shared.Rent(size);
try
{
using (var stream = new MemoryStream(buffer))
{
bobject.EncodeTo(stream);
return encoding.GetString(buffer.AsSpan().Slice(0, size));
}
}
finally { ArrayPool<byte>.Shared.Return(buffer); }
}
/// <summary>
/// Encodes the object and returns the raw bytes.
/// </summary>
/// <returns>The raw bytes of the bencoded object.</returns>
public static byte[] EncodeAsBytes(this IBObject bobject)
{
var size = bobject.GetSizeInBytes();
var bytes = new byte[size];
using (var stream = new MemoryStream(bytes))
{
bobject.EncodeTo(stream);
return bytes;
}
}
/// <summary>
/// Writes the object as bencode to the specified file path.
/// </summary>
/// <param name="bobject"></param>
/// <param name="filePath">The file path to write the encoded object to.</param>
public static void EncodeTo(this IBObject bobject, string filePath)
{
using (var stream = File.OpenWrite(filePath))
{
bobject.EncodeTo(stream);
}
}
}
}