Last active
August 1, 2016 04:45
-
-
Save yutopio/63532fe20efcf7f5173c6c3c71070af3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading; | |
using System.Threading.Tasks; | |
public static class StreamUtil | |
{ | |
public static async Task<byte[]> ReadAsync(this Stream stream, | |
int length, CancellationToken cancellationToken) | |
{ | |
var ret = new byte[length]; | |
for (var count = 0; count < length;) | |
{ | |
count += await stream.ReadAsync( | |
ret, count, length - count, cancellationToken); | |
} | |
return ret; | |
} | |
public static Task WriteAsync(this Stream stream, | |
byte[] buffer, CancellationToken cancellationToken) | |
{ | |
return stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken); | |
} | |
public static async Task<byte[]> ReadChunkAsync(this Stream stream, | |
CancellationToken cancellationToken) | |
{ | |
var length = BitConverter.ToInt32( | |
await stream.ReadAsync(4, cancellationToken), 0); | |
return await stream.ReadAsync(length, cancellationToken); | |
} | |
public static async Task WriteChunkAsync(this Stream stream, | |
byte[] buffer, CancellationToken cancellationToken) | |
{ | |
var length = BitConverter.GetBytes(buffer.Length); | |
await stream.WriteAsync(length, cancellationToken); | |
await stream.WriteAsync(buffer, cancellationToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment