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.
[SuppressMessage("Security", "CA5350:Do Not Use Weak Cryptographic Algorithms", Justification = "Legacy support with clear documentation")]
[SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms", Justification = "Legacy support with clear documentation")]
public static Task<byte[]> ComputeHashAsync(this byte[] data, string algorithmName, CancellationToken cancellationToken = default)
Parameters
databyte[]The data to hash.
algorithmNamestringThe hash algorithm name (e.g., "SHA256", "MD5").
cancellationTokenCancellationTokenThe 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
databyte[]The data to copy to the stream.
streamStreamThe destination stream.
chunkSizeintThe size of each chunk (default 4096).
cancellationTokenCancellationTokenThe 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
databyte[]The data to search in.
patternbyte[]The pattern to search for.
cancellationTokenCancellationTokenThe 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
databyte[]The data to process.
processorFunc<byte[], Task<TResult>>The function to process each chunk.
chunkSizeintThe size of each chunk (default 4096).
maxConcurrencyintThe maximum number of concurrent operations (default Environment.ProcessorCount).
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<TResult[]>
A task representing the asynchronous operation with the processed results.
Type Parameters
TResultThe 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
filePathstringThe path to the file to read.
cancellationTokenCancellationTokenThe 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
streamStreamThe stream to read from.
bufferSizeintThe buffer size for reading (default 4096).
cancellationTokenCancellationTokenThe 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
data1byte[]The first byte array.
data2byte[]The second byte array.
cancellationTokenCancellationTokenThe 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
databyte[]The data to transform.
transformerFunc<byte[], Task<byte[]>>The transformation function.
chunkSizeintThe size of each chunk for processing (default 4096).
cancellationTokenCancellationTokenThe 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
databyte[]The data to verify.
expectedHashbyte[]The expected hash value.
algorithmNamestringThe hash algorithm name.
cancellationTokenCancellationTokenThe 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
databyte[]The data to write to the file.
filePathstringThe path to the file to write.
cancellationTokenCancellationTokenThe 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
databyte[]The data to write to the stream.
streamStreamThe stream to write to.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
A task representing the asynchronous operation.