-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamExtensions.cs
106 lines (98 loc) · 3.81 KB
/
StreamExtensions.cs
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
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace SystemLibrary.Common.Net.Extensions;
/// <summary>
/// Extensions for streams like reading content as json directly into class through ToJsonAsync()
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Read a stream as of 'json data' into a class T
///
/// Usually used whenever receiving content from an API or similar over the network, and with the stream automatically convert its content in an async manner to your class
///
/// Note: if you do not send in 'JsonSerializerOptions' the common ones from SystemLibrary will be used, which contain a 'String To Enum', 'Int to String' and some other additional converters than the standard that comes with System.Text.json
/// </summary>
/// <example>
/// <code>
/// using (var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
/// return await contentStream.ToJsonAsync<T>(jsonSerializerOptions).ConfigureAwait(false);
/// </code>
/// </example>
public static async Task<T> JsonAsync<T>(this Stream stream, JsonSerializerOptions options = null, CancellationToken cancellationToken = default)
{
if (stream == null) return default;
options = GetJsonSerializerOptions.Default(options);
return await JsonSerializer.DeserializeAsync<T>(stream, options, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Reads the stream to its end, hashing the content and returning the hash as a string
///
/// Returns null if stream is null or cannot be read
///
/// Tip: If data is larger than ~200 bytes then .ToSha1Hash() is faster
/// </summary>
/// <example>
/// <code>
/// var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
/// var hash = fileStream.ToMD5HashAsync().Result;
/// </code>
/// </example>
public static async Task<string> ToMD5HashAsync(this Stream stream)
{
return await Md5.ComputeAsync(stream).ConfigureAwait(false);
}
/// <summary>
/// Reads the stream to its end, hashing the content and returning the hash as a string
///
/// Returns null if stream is null or cannot be read
///
/// Tip: If data is larger than ~200 bytes then .ToSha1Hash() is faster
/// </summary>
/// <example>
/// <code>
/// var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
/// var hash = fileStream.ToMD5Hash();
/// </code>
/// </example>
public static string ToMD5Hash(this Stream stream)
{
return Md5.Compute(stream);
}
/// <summary>
/// Reads the stream to its end, hashing the content and returning the hash as a string
///
/// Returns null if stream is null or cannot be read
///
/// Tip: If data is smaller than ~200 bytes then .ToMD5Hash() is faster
/// </summary>
/// <example>
/// <code>
/// var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
/// var hash = fileStream.ToSha1Hash();
/// </code>
/// </example>
public static string ToSha1Hash(this Stream stream)
{
return Sha1.Compute(stream);
}
/// <summary>
/// Reads the stream to its end, hashing the content and returning the hash as a string
///
/// Returns null if stream is null or cannot be read
///
/// Tip: If data is smaller than ~200 bytes then .ToMD5Hash() is faster
/// </summary>
/// <example>
/// <code>
/// var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
/// var hash = fileStream.ToSha1HashAsync().Result;
/// </code>
/// </example>
public static async Task<string> ToSha1HashAsync(this Stream stream)
{
return await Sha1.ComputeAsync(stream).ConfigureAwait(false);
}
}