Skip to content

Commit

Permalink
added StreamTaskParallelism#ReadAsync method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jittuu committed Mar 23, 2012
1 parent f451e58 commit 3acaad2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NSupport/NSupport.csproj
Expand Up @@ -14,6 +14,7 @@
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -53,6 +54,7 @@
<Compile Include="IntegerEvenOdd.cs" />
<Compile Include="IntegerTime.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamTaskParallelism.cs" />
<Compile Include="StringAccess.cs" />
<Compile Include="StringCryptography.cs" />
<Compile Include="StringHumanization.cs" />
Expand Down
34 changes: 34 additions & 0 deletions NSupport/StreamTaskParallelism.cs
@@ -0,0 +1,34 @@
namespace NSupport {
using System.IO;
using System.Threading.Tasks;

/// <summary>
/// Provides Task Parallel Library usage to wrap Asynchronous Programming Model pattern.
/// </summary>
public static class StreamTaskParallelism {
/// <summary>
/// Begins an asynchronous read operation.
/// </summary>
/// <param name="stream">A <see cref="Stream"/> instance.</param>
/// <param name="buffer">The buffer to read the data into.</param>
/// <param name="offset">The byte offset in buffer at which to begin writing data read from the stream.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <returns>The created <see cref="Task{WebResponse}"/> that represents the asynchronous operation.</returns>
public static Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count) {
return stream.ReadAsync(buffer, offset, count, null);
}

/// <summary>
/// Begins an asynchronous read operation.
/// </summary>
/// <param name="stream">A <see cref="Stream"/> instance.</param>
/// <param name="buffer">The buffer to read the data into.</param>
/// <param name="offset">The byte offset in buffer at which to begin writing data read from the stream.</param>
/// <param name="count">The maximum number of bytes to read.</param>
/// <param name="state">An object containing state information for this asynchronous request.</param>
/// <returns>The created <see cref="Task{WebResponse}"/> that represents the asynchronous operation.</returns>
public static Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, object state) {
return Task.Factory.FromAsync<byte[], int, int, int>(stream.BeginRead, stream.EndRead, buffer, offset, count, state);
}
}
}

0 comments on commit 3acaad2

Please sign in to comment.