123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System;
- using System.IO;
- namespace Agent.Polyfills
- {
- /// <summary>
- /// .NET 4.0부터 지원하는 Stream의 [CopyTo] 메서드 구현
- /// </summary>
- 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<long> reportProgress)
- {
- return CopyTo(source, destination, DefaultBufferSize, reportProgress);
- }
- public static long CopyTo(this Stream source, Stream destination, int bufferSize, Action<long> 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;
- }
- }
- }
|