Files
PhotoRenamer/PhotoRenamer/StreamExtensions.cs
2023-11-04 12:08:53 +01:00

40 lines
1.3 KiB
C#

namespace PhotoRenamer;
public static class StreamExtensions
{
public static async Task CopyToAsync(
this Stream source,
Stream destination,
int bufferSize,
IProgress<double>? progress = null,
CancellationToken cancellationToken = default
)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (!source.CanRead)
throw new ArgumentException("Has to be readable", nameof(source));
if (destination == null)
throw new ArgumentNullException(nameof(destination));
if (!destination.CanWrite)
throw new ArgumentException("Has to be writable", nameof(destination));
if (bufferSize < 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));
var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while (
(bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false))
!= 0
)
{
await destination
.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken)
.ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead / (double)source.Length);
}
}
}