NamespaceSystemLibrary.Common.Net.Extensions SystemLibrary.Common.Net.dll
Extensions for streams like reading content as json directly into class through ToJsonAsync()
StreamExtensions
public static class StreamExtensions
Inheritance
Examples
Arguments
Methods
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
public static async Task<T> JsonAsync<T>(this Stream stream, JsonSerializerOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
using (var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
return await contentStream.ToJsonAsync<T>(jsonSerializerOptions).ConfigureAwait(false);
Generic types
Name | Description |
---|---|
T |
Methods arguments
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | |
System.Text.Json.JsonSerializerOptions | options | |
System.Threading.CancellationToken | cancellationToken |
Type | Description |
---|---|
System.Threading.Tasks.Task<T> |
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
public static string ToMD5Hash(this Stream stream)
var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
var hash = fileStream.ToMD5Hash();
Methods arguments
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Type | Description |
---|---|
System.String |
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
public static async Task<string> ToMD5HashAsync(this Stream stream)
var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
var hash = fileStream.ToMD5HashAsync().Result;
Methods arguments
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> |
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
public static string ToSha1Hash(this Stream stream)
var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
var hash = fileStream.ToSha1Hash();
Methods arguments
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Type | Description |
---|---|
System.String |
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
public static async Task<string> ToSha1HashAsync(this Stream stream)
var fileStream = new FileStream(@"C:\file.txt", FileMode.Open);
var hash = fileStream.ToSha1HashAsync().Result;
Methods arguments
Type | Name | Description |
---|---|---|
System.IO.Stream | stream |
Type | Description |
---|---|
System.Threading.Tasks.Task<System.String> |
}