Class ByteArrayAsyncExtensions
- Namespace
- Plugin.ByteArrays
- Assembly
- Plugin.ByteArrays.dll
Asynchronous operations for byte arrays.
public static class ByteArrayAsyncExtensions
- Inheritance
-
ByteArrayAsyncExtensions
- Inherited Members
Methods
ComputeHashAsync(byte[], string, CancellationToken)
Asynchronously computes a hash of the byte array using the specified algorithm.
public static Task<byte[]> ComputeHashAsync(this byte[] data, string algorithmName, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to hash.
algorithmName
stringThe hash algorithm name (e.g., "SHA256", "MD5").
cancellationToken
CancellationTokenThe cancellation token.
Returns
CopyToStreamAsync(byte[], Stream, int, CancellationToken)
Asynchronously copies a byte array to a stream in chunks.
public static Task CopyToStreamAsync(this byte[] data, Stream stream, int chunkSize = 4096, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to copy to the stream.
stream
StreamThe destination stream.
chunkSize
intThe size of each chunk (default 4096).
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.
IndexOfAsync(byte[], byte[], CancellationToken)
Asynchronously searches for a pattern in the byte array.
public static Task<int> IndexOfAsync(this byte[] data, byte[] pattern, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to search in.
pattern
byte[]The pattern to search for.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task<int>
A task representing the asynchronous operation with the index of the first occurrence, or -1 if not found.
ProcessInParallelAsync<TResult>(byte[], Func<byte[], Task<TResult>>, int, int, CancellationToken)
Asynchronously processes byte array data in parallel chunks.
public static Task<TResult[]> ProcessInParallelAsync<TResult>(this byte[] data, Func<byte[], Task<TResult>> processor, int chunkSize = 4096, int maxConcurrency = 0, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to process.
processor
Func<byte[], Task<TResult>>The function to process each chunk.
chunkSize
intThe size of each chunk (default 4096).
maxConcurrency
intThe maximum number of concurrent operations (default Environment.ProcessorCount).
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task<TResult[]>
A task representing the asynchronous operation with the processed results.
Type Parameters
TResult
The type of the result.
ReadFromFileAsync(string, CancellationToken)
Asynchronously reads a byte array from a file.
public static Task<byte[]> ReadFromFileAsync(string filePath, CancellationToken cancellationToken = default)
Parameters
filePath
stringThe path to the file to read.
cancellationToken
CancellationTokenThe cancellation token.
Returns
ReadFromStreamAsync(Stream, int, CancellationToken)
Asynchronously reads a byte array from a stream.
public static Task<byte[]> ReadFromStreamAsync(Stream stream, int bufferSize = 4096, CancellationToken cancellationToken = default)
Parameters
stream
StreamThe stream to read from.
bufferSize
intThe buffer size for reading (default 4096).
cancellationToken
CancellationTokenThe cancellation token.
Returns
SequenceEqualAsync(byte[], byte[], CancellationToken)
Asynchronously compares two byte arrays for equality.
public static Task<bool> SequenceEqualAsync(this byte[] data1, byte[] data2, CancellationToken cancellationToken = default)
Parameters
data1
byte[]The first byte array.
data2
byte[]The second byte array.
cancellationToken
CancellationTokenThe cancellation token.
Returns
TransformAsync(byte[], Func<byte[], Task<byte[]>>, int, CancellationToken)
Asynchronously transforms byte array data using a transformation function.
public static Task<byte[]> TransformAsync(this byte[] data, Func<byte[], Task<byte[]>> transformer, int chunkSize = 4096, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to transform.
transformer
Func<byte[], Task<byte[]>>The transformation function.
chunkSize
intThe size of each chunk for processing (default 4096).
cancellationToken
CancellationTokenThe cancellation token.
Returns
VerifyIntegrityAsync(byte[], byte[], string, CancellationToken)
Asynchronously verifies the integrity of data against a provided hash.
public static Task<bool> VerifyIntegrityAsync(this byte[] data, byte[] expectedHash, string algorithmName, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to verify.
expectedHash
byte[]The expected hash value.
algorithmName
stringThe hash algorithm name.
cancellationToken
CancellationTokenThe cancellation token.
Returns
WriteToFileAsync(byte[], string, CancellationToken)
Asynchronously writes the byte array to a file.
public static Task WriteToFileAsync(this byte[] data, string filePath, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to write to the file.
filePath
stringThe path to the file to write.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.
WriteToStreamAsync(byte[], Stream, CancellationToken)
Asynchronously writes the byte array to a stream.
public static Task WriteToStreamAsync(this byte[] data, Stream stream, CancellationToken cancellationToken = default)
Parameters
data
byte[]The data to write to the stream.
stream
StreamThe stream to write to.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.