diff --git a/StabilityMatrix/Helper/FileHash.cs b/StabilityMatrix/Helper/FileHash.cs index 41d96682..bfc10c09 100644 --- a/StabilityMatrix/Helper/FileHash.cs +++ b/StabilityMatrix/Helper/FileHash.cs @@ -9,20 +9,6 @@ namespace StabilityMatrix.Helper; public static class FileHash { - /// - /// Determines suitable buffer size based on stream length. - /// - /// - /// - private static ulong GetBufferSize(ulong totalBytes) => totalBytes switch - { - < Size.MiB => 8 * Size.KiB, - < 100 * Size.MiB => 16 * Size.KiB, - < 500 * Size.MiB => Size.MiB, - < Size.GiB => 16 * Size.MiB, - _ => 32 * Size.MiB - }; - public static async Task GetHashAsync(HashAlgorithm hashAlgorithm, Stream stream, byte[] buffer, Action? progress = default) { ulong totalBytesRead = 0; @@ -55,7 +41,7 @@ public static class FileHash var totalBytes = Convert.ToUInt64(new FileInfo(filePath).Length); var shared = ArrayPool.Shared; - var buffer = shared.Rent((int) GetBufferSize(totalBytes)); + var buffer = shared.Rent((int) FileTransfers.GetBufferSize(totalBytes)); try { await using var stream = File.OpenRead(filePath); diff --git a/StabilityMatrix/Helper/FileTransfers.cs b/StabilityMatrix/Helper/FileTransfers.cs index fe7c111e..7e870471 100644 --- a/StabilityMatrix/Helper/FileTransfers.cs +++ b/StabilityMatrix/Helper/FileTransfers.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Linq; @@ -9,7 +10,20 @@ namespace StabilityMatrix.Helper; public static class FileTransfers { - public const int BufferSize = 10240; + /// + /// Determines suitable buffer size based on stream length. + /// + /// + /// + public static ulong GetBufferSize(ulong totalBytes) => totalBytes switch + { + < Size.MiB => 8 * Size.KiB, + < 100 * Size.MiB => 16 * Size.KiB, + < 500 * Size.MiB => Size.MiB, + < Size.GiB => 16 * Size.MiB, + _ => 32 * Size.MiB + }; + public static async Task CopyFiles(Dictionary files, IProgress? fileProgress = default, IProgress? totalProgress = default) { var totalFiles = files.Count; @@ -39,15 +53,23 @@ public static class FileTransfers private static async Task CopyStream(Stream from, Stream to, Action progress) { - var buffer = new byte[BufferSize]; + var shared = ArrayPool.Shared; + var buffer = shared.Rent((int) GetBufferSize((ulong) from.Length)); var totalRead = 0L; - - while (totalRead < from.Length) + + try + { + while (totalRead < from.Length) + { + var read = await from.ReadAsync(buffer.AsMemory(0, BufferSize)); + await to.WriteAsync(buffer.AsMemory(0, read)); + totalRead += read; + progress(totalRead); + } + } + finally { - var read = await from.ReadAsync(buffer.AsMemory(0, BufferSize)); - await to.WriteAsync(buffer.AsMemory(0, read)); - totalRead += read; - progress(totalRead); + shared.Return(buffer); } } }