Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

[WIP] inputStream/outptStream on ComputeHash{,Async} #40

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 59 additions & 15 deletions src/System.Data.HashFunction.Core/HashFunctionAsyncBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,94 @@ public abstract class HashFunctionAsyncBase
/// <summary>
/// Computes hash value for given stream asynchronously.
/// </summary>
/// <param name="data">Stream of data to hash.</param>
/// <param name="inputStream">Stream of data to hash.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <remarks>
/// All stream IO is done via ReadAsync.
/// </remarks>
/// <exception cref="ArgumentNullException">;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="data"/></exception>
public Task<IHashValue> ComputeHashAsync(Stream data) => ComputeHashAsync(data, CancellationToken.None);
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
public Task<IHashValue> ComputeHashAsync(Stream inputStream) => ComputeHashAsync(inputStream, null);

/// <summary>
/// Computes hash value for given stream asynchronously.
/// </summary>
/// <param name="data">Stream of data to hash.</param>
/// <param name="inputStream">Stream of data to hash.</param>
/// <param name="cancellationToken">A cancellation token to observe while calculating the hash value.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <remarks>
/// All stream IO is done via ReadAsync.
/// </remarks>
/// <exception cref="ArgumentNullException">;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="data"/></exception>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
/// <exception cref="TaskCanceledException">The <paramref name="cancellationToken"/> was canceled.</exception>
public async Task<IHashValue> ComputeHashAsync(Stream data, CancellationToken cancellationToken)
public Task<IHashValue> ComputeHashAsync(Stream inputStream, CancellationToken cancellationToken) => ComputeHashAsync(inputStream, null, cancellationToken);

/// <summary>
/// Computes hash value for given stream asynchronously.
/// </summary>
/// <param name="inputStream">Stream of data to hash.</param>
/// <param name="outputStream">Stream to write the read data to.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <remarks>
/// All stream IO is done via ReadAsync.
/// All outputStream IO is done via WriteAsync.
/// </remarks>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be writable.;<paramref name="outputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
public Task<IHashValue> ComputeHashAsync(Stream inputStream, Stream outputStream) => ComputeHashAsync(inputStream, outputStream, CancellationToken.None);

/// <summary>
/// Computes hash value for given stream asynchronously while outputting the read bytes to the second stream asynchronously.
/// </summary>
/// <param name="inputStream">Stream of data to hash.</param>
/// <param name="outputStream">Stream to write the read data to.</param>
/// <param name="cancellationToken">A cancellation token to observe while calculating the hash value.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <remarks>
/// All inputStream IO is done via ReadAsync.
/// All outputStream IO is done via WriteAsync.
/// </remarks>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be writable.;<paramref name="outputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
/// <exception cref="TaskCanceledException">The <paramref name="cancellationToken"/> was canceled.</exception>
public async Task<IHashValue> ComputeHashAsync(Stream inputStream, Stream outputStream, CancellationToken cancellationToken)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (inputStream == null)
throw new ArgumentNullException(nameof(inputStream));

if (!inputStream.CanRead)
throw new ArgumentException("Stream must be readable.", nameof(inputStream));

if (outputStream != null && !outputStream.CanWrite)
throw new ArgumentException("Stream must be writable.", nameof(outputStream));

if (!data.CanRead)
throw new ArgumentException("Stream \"data\" must be readable.", "data");

cancellationToken.ThrowIfCancellationRequested();


return new HashValue(
await ComputeHashAsyncInternal(new StreamData(data), cancellationToken)
await ComputeHashAsyncInternal(new StreamData(inputStream, outputStream), cancellationToken)
.ConfigureAwait(false),
HashSizeInBits);
}



/// <summary>
/// Computes hash value for given stream asynchronously.
/// </summary>
Expand Down
68 changes: 52 additions & 16 deletions src/System.Data.HashFunction.Core/HashFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,80 @@ public IHashValue ComputeHash(byte[] data, CancellationToken cancellationToken)
/// <summary>
/// Computes hash value for given stream.
/// </summary>
/// <param name="data">Stream of data to hash.</param>
/// <param name="inputStream">Stream of data to hash.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <exception cref="ArgumentNullException">;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="data"/></exception>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
/// <inheritdoc />
public IHashValue ComputeHash(Stream data) => ComputeHash(data, CancellationToken.None);
public IHashValue ComputeHash(Stream inputStream) => ComputeHash(inputStream, null, CancellationToken.None);


/// <summary>
/// Computes hash value for given stream.
/// </summary>
/// <param name="data">Stream of data to hash.</param>
/// <param name="inputStream">Stream of data to hash.</param>
/// <param name="cancellationToken">A cancellation token to observe while calculating the hash value.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <exception cref="ArgumentNullException">;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="data"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="data"/></exception>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
/// <exception cref="TaskCanceledException">The <paramref name="cancellationToken"/> was canceled.</exception>
public IHashValue ComputeHash(Stream inputStream, CancellationToken cancellationToken) => ComputeHash(inputStream, null, cancellationToken);


/// <summary>
/// Computes hash value for given stream while outputting the read bytes to the second stream.
/// </summary>
/// <param name="inputStream">Stream of data to hash.</param>
/// <param name="outputStream">Stream to write the read data to.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentNullException">;<paramref name="outputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be writable.;<paramref name="outputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
public IHashValue ComputeHash(Stream inputStream, Stream outputStream) => ComputeHash(inputStream, outputStream, CancellationToken.None);

/// <summary>
/// Computes hash value for given stream while outputting the read bytes to the second stream.
/// </summary>
/// <param name="inputStream">Stream of data to hash.</param>
/// <param name="outputStream">Stream to write the read data to.</param>
/// <param name="cancellationToken">A cancellation token to observe while calculating the hash value.</param>
/// <returns>
/// Hash value of the data.
/// </returns>
/// <exception cref="ArgumentNullException">;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be readable.;<paramref name="inputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be writable.;<paramref name="outputStream"/></exception>
/// <exception cref="ArgumentException">Stream must be seekable for this type of hash function.;<paramref name="inputStream"/></exception>
/// <exception cref="TaskCanceledException">The <paramref name="cancellationToken"/> was canceled.</exception>
public IHashValue ComputeHash(Stream data, CancellationToken cancellationToken)
public IHashValue ComputeHash(Stream inputStream, Stream outputStream, CancellationToken cancellationToken)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

if (!data.CanRead)
throw new ArgumentException("Stream must be readable.", nameof(data));
if (inputStream == null)
throw new ArgumentNullException(nameof(inputStream));

if (!inputStream.CanRead)
throw new ArgumentException("Stream must be readable.", nameof(inputStream));

if (outputStream != null && !outputStream.CanWrite)
throw new ArgumentException("Stream must be writable.", nameof(outputStream));



cancellationToken.ThrowIfCancellationRequested();


return new HashValue(
ComputeHashInternal(
new StreamData(data),
new StreamData(inputStream, outputStream),
cancellationToken),
HashSizeInBits);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<RepositoryUrl>https://github.com/brandondahler/Data.HashFunction/</RepositoryUrl>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>2.1.0</Version>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have updated VersionPrefix instead of adding Version.

</PropertyGroup>

<PropertyGroup Condition="'$(VcsRevision)'!=''">
Expand Down
Loading