using System;
using System.IO;
namespace Agent.Polyfills
{
///
/// .NET 4.0부터 지원하는 Stream의 [CopyTo] 메서드 구현
///
static class StreamPollyfill
{
const int DefaultBufferSize = 4096;
public static long CopyTo(this Stream source, Stream destination)
{
return CopyTo(source, destination, DefaultBufferSize, _ => { });
}
public static long CopyTo(this Stream source, Stream destination, int bufferSize)
{
return CopyTo(source, destination, bufferSize, _ => { });
}
public static long CopyTo(this Stream source, Stream destination, Action reportProgress)
{
return CopyTo(source, destination, DefaultBufferSize, reportProgress);
}
public static long CopyTo(this Stream source, Stream destination, int bufferSize, Action reportProgress)
{
if (source == null) throw new ArgumentNullException("source");
if (destination == null) throw new ArgumentNullException("destination");
if (reportProgress == null) throw new ArgumentNullException("reportProgress");
var buffer = new byte[bufferSize];
var transferredBytes = 0L;
for (var bytesRead = source.Read(buffer, 0, buffer.Length); bytesRead > 0; bytesRead = source.Read(buffer, 0, buffer.Length))
{
transferredBytes += bytesRead;
reportProgress(transferredBytes);
destination.Write(buffer, 0, bytesRead);
}
destination.Flush();
return transferredBytes;
}
}
}