Created
November 7, 2014 05:21
-
-
Save Tewr/f0599ca5e765c47d21b7 to your computer and use it in GitHub Desktop.
Stream CopyTo with bytecount
This file contains 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
namespace Extensions { | |
public static class StreamExtensions | |
{ | |
/// <summary> | |
/// Implementation of <see cref="Stream.CopyTo(System.IO.Stream)"/> with progress reporting | |
/// </summary> | |
/// <param name="fromStream"></param> | |
/// <param name="destination"></param> | |
/// <param name="bufferSize"></param> | |
/// <param name="progressInfo"></param> | |
internal static void CopyTo(this Stream fromStream, Stream destination, int bufferSize, CopyProgressInfo progressInfo) | |
{ | |
var buffer = new byte[bufferSize]; | |
int count; | |
while ((count = fromStream.Read(buffer, 0, buffer.Length)) != 0) | |
{ | |
progressInfo.BytesTransfered += count; | |
destination.Write(buffer, 0, count); | |
} | |
} | |
/// <summary> | |
/// Implementation of <see cref="Stream.CopyToAsync(System.IO.Stream)"/> with progress reporting | |
/// </summary> | |
/// <param name="fromStream"></param> | |
/// <param name="destination"></param> | |
/// <param name="bufferSize"></param> | |
/// <param name="progressInfo"></param> | |
internal static async Task CopyToAsync(this Stream fromStream, Stream destination, int bufferSize, CopyProgressInfo progressInfo) | |
{ | |
var buffer = new byte[bufferSize]; | |
int count; | |
while ((count = await fromStream.ReadAsync(buffer, 0, buffer.Length)) != 0) | |
{ | |
progressInfo.BytesTransfered += count; | |
await destination.WriteAsync(buffer, 0, count); | |
} | |
} | |
/// <summary> | |
/// Implementation of <see cref="Stream.CopyToAsync(System.IO.Stream)"/> with progress reporting | |
/// </summary> | |
/// <param name="fromStream"></param> | |
/// <param name="destination"></param> | |
/// <param name="bufferSize"></param> | |
/// <param name="progressInfo"></param> | |
internal static async Task CopyToAsync(this Stream fromStream, Stream destination, int bufferSize, CopyProgressInfo progressInfo, CancellationToken cancellationToken) | |
{ | |
var buffer = new byte[bufferSize]; | |
int count; | |
while ((count = await fromStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) != 0) | |
{ | |
progressInfo.BytesTransfered += count; | |
await destination.WriteAsync(buffer, 0, count, cancellationToken); | |
} | |
} | |
} | |
public class CopyProgressInfo | |
{ | |
public long BytesTransfered { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment