Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buffer size handling and progress reset issues. #80

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/DummyFileCreator/DummyFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,19 @@
DummyFileWriter? fileWriter = null;
try
{
fileWriter = new DummyFileWriter(filepathToCreate, bufferSize);

Check warning on line 95 in src/DummyFileCreator/DummyFile.cs

View workflow job for this annotation

GitHub Actions / CI on windows

Use recommended dispose pattern to ensure that object created by 'new DummyFileWriter(filepathToCreate, bufferSize)' is disposed on all paths. If possible, wrap the creation within a 'using' statement or a 'using' declaration. Otherwise, use a try-finally pattern, with a dedicated local variable declared before the try region and an unconditional Dispose invocation on non-null value in the 'finally' region, say 'x?.Dispose()'. If the object is explicitly disposed within the try region or the dispose ownership is transfered to another object or method, assign 'null' to the local variable just after such an operation to prevent double dispose in 'finally'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Check warning on line 95 in src/DummyFileCreator/DummyFile.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Use recommended dispose pattern to ensure that object created by 'new DummyFileWriter(filepathToCreate, bufferSize)' is disposed on all paths. If possible, wrap the creation within a 'using' statement or a 'using' declaration. Otherwise, use a try-finally pattern, with a dedicated local variable declared before the try region and an unconditional Dispose invocation on non-null value in the 'finally' region, say 'x?.Dispose()'. If the object is explicitly disposed within the try region or the dispose ownership is transfered to another object or method, assign 'null' to the local variable just after such an operation to prevent double dispose in 'finally'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)
var writtenBytes = 0;
long writtenBytes = 0;
while (writtenBytes < byteSizeToCreate)
{
var remainingBytes = byteSizeToCreate - writtenBytes;
var bytesToWrite = Math.Min(bufferSize, remainingBytes);
if (fillWithZeros)
{
writtenBytes += await fileWriter.WriteZeroValue().ConfigureAwait(false);
writtenBytes += await fileWriter.WriteZeroValue(bytesToWrite).ConfigureAwait(false);
}
else
{
writtenBytes += await fileWriter.WriteRandomText().ConfigureAwait(false);
writtenBytes += await fileWriter.WriteRandomText(bytesToWrite).ConfigureAwait(false);
}

progress?.Invoke(writtenBytes, byteSizeToCreate);
Expand Down
23 changes: 17 additions & 6 deletions src/DummyFileCreator/DummyFileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,44 @@ public async ValueTask DisposeAsync()
/// <summary>
/// 非同期操作として、0を書き込みます。
/// </summary>
/// <param name="bytesToWrite">書き込むバイト数。</param>
/// <returns>書き込んだバイトサイズ。</returns>
public async Task<int> WriteZeroValue()
public async Task<long> WriteZeroValue(long bytesToWrite)
{
if (this._writer is null)
{
throw new InvalidOperationException();
}

ReadOnlyMemory<char> dataToFill = new char[128];
ReadOnlyMemory<char> dataToFill = new char[bytesToWrite];
await this._writer.WriteAsync(dataToFill).ConfigureAwait(false);
return dataToFill.Length;
}

/// <summary>
/// 非同期操作として、ランダムな文字列を書き込みます。
/// </summary>
/// <param name="bytesToWrite">書き込むバイト数。</param>
/// <returns>書き込んだバイトサイズ。</returns>
public async Task<int> WriteRandomText()
public async Task<long> WriteRandomText(long bytesToWrite)
{
if (this._writer is null)
{
throw new InvalidOperationException();
}

var dataToWrite = Password.Generate(128, 32).AsMemory();
await this._writer.WriteAsync(dataToWrite).ConfigureAwait(false);
return dataToWrite.Length;
long totalLength = 0;
const int maxChunkSize = 128; // Password.Generate の制約に合わせた最大チャンクサイズです。
while (totalLength < bytesToWrite)
{
var remainingLength = bytesToWrite - totalLength;
var currentChunkSize = (int)Math.Min(maxChunkSize, remainingLength);
var dataToWrite = Password.Generate(currentChunkSize, currentChunkSize / 4).AsMemory();
await this._writer.WriteAsync(dataToWrite).ConfigureAwait(false);
totalLength += dataToWrite.Length;
}

return totalLength;
}

/// <summary>
Expand Down
Loading