Fix storage metrics not being accurate
This commit is contained in:
@@ -24,4 +24,41 @@ internal static class ExportFileWriter
|
||||
/// <summary>Writes <paramref name="html"/> to <paramref name="filePath"/> as UTF-8 without BOM.</summary>
|
||||
public static Task WriteHtmlAsync(string filePath, string html, CancellationToken ct)
|
||||
=> File.WriteAllTextAsync(filePath, html, Utf8NoBom, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Streams a <see cref="StringBuilder"/> directly to disk as UTF-8 with
|
||||
/// BOM, chunk by chunk. Avoids the full-document <c>ToString()</c> copy
|
||||
/// and the separate UTF-8 byte buffer that <see cref="File.WriteAllTextAsync(string, string, Encoding, CancellationToken)"/>
|
||||
/// would otherwise allocate — meaningful for large CSV exports.
|
||||
/// </summary>
|
||||
public static Task WriteCsvChunksAsync(string filePath, StringBuilder builder, CancellationToken ct)
|
||||
=> WriteChunksAsync(filePath, builder, Utf8WithBom, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Streams a <see cref="StringBuilder"/> directly to disk as UTF-8 without
|
||||
/// BOM. Same rationale as <see cref="WriteCsvChunksAsync"/> — for large
|
||||
/// HTML reports it halves peak memory by skipping the intermediate string.
|
||||
/// </summary>
|
||||
public static Task WriteHtmlChunksAsync(string filePath, StringBuilder builder, CancellationToken ct)
|
||||
=> WriteChunksAsync(filePath, builder, Utf8NoBom, ct);
|
||||
|
||||
private static async Task WriteChunksAsync(string filePath, StringBuilder builder, Encoding encoding, CancellationToken ct)
|
||||
{
|
||||
// FileOptions.Asynchronous lets StreamWriter use true async I/O.
|
||||
await using var fs = new FileStream(
|
||||
filePath,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None,
|
||||
bufferSize: 64 * 1024,
|
||||
options: FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
await using var sw = new StreamWriter(fs, encoding, bufferSize: 64 * 1024);
|
||||
|
||||
foreach (var chunk in builder.GetChunks())
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
await sw.WriteAsync(chunk, ct);
|
||||
}
|
||||
await sw.FlushAsync(ct);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user