Skip to content

Commit

Permalink
Add an async version of ReadRequestedBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Numpsy committed May 8, 2021
1 parent 34879be commit f913eb5
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace ICSharpCode.SharpZipLib.Core
{
Expand Down Expand Up @@ -64,16 +66,8 @@ static public void ReadFully(Stream stream, byte[] buffer, int offset, int count
}
}

/// <summary>
/// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
/// </summary>
/// <param name="stream">The stream to read data from.</param>
/// <param name="buffer">The buffer to store data in.</param>
/// <param name="offset">The offset at which to begin storing data.</param>
/// <param name="count">The number of bytes of data to store.</param>
/// <exception cref="ArgumentNullException">Required parameter is null</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, int count)
// A helper function to share between the async and sync versions of ReadRequestedBytes
private static void ValidateArgumentsForRead(Stream stream, byte[] buffer, int offset, int count)
{
if (stream == null)
{
Expand All @@ -95,7 +89,23 @@ static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
{
throw new ArgumentOutOfRangeException(nameof(count));
}
}

/// <summary>
/// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
/// </summary>
/// <param name="stream">The stream to read data from.</param>
/// <param name="buffer">The buffer to store data in.</param>
/// <param name="offset">The offset at which to begin storing data.</param>
/// <param name="count">The number of bytes of data to store.</param>
/// <exception cref="ArgumentNullException">Required parameter is null</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, int count)
{
// Common validation function
ValidateArgumentsForRead(stream, buffer, offset, count);

// read the data using Read
int totalReadCount = 0;
while (count > 0)
{
Expand All @@ -112,6 +122,38 @@ static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
return totalReadCount;
}

/// <summary>
/// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
/// </summary>
/// <param name="stream">The stream to read data from.</param>
/// <param name="buffer">The buffer to store data in.</param>
/// <param name="offset">The offset at which to begin storing data.</param>
/// <param name="count">The number of bytes of data to store.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <exception cref="ArgumentNullException">Required parameter is null</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
static public async Task<int> ReadRequestedBytesAsync(Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
// Common validation function
ValidateArgumentsForRead(stream, buffer, offset, count);

// read the data using ReadAsync
int totalReadCount = 0;
while (count > 0)
{
int readCount = await stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
if (readCount <= 0)
{
break;
}
offset += readCount;
count -= readCount;
totalReadCount += readCount;
}

return totalReadCount;
}

/// <summary>
/// Copy the contents of one <see cref="Stream"/> to another.
/// </summary>
Expand Down

0 comments on commit f913eb5

Please sign in to comment.