Skip to content

Commit

Permalink
Fix SnowBreakHttpClient.cs
Browse files Browse the repository at this point in the history
- Undo my forget-to-delete debug code.
- The thing now properly re-use buffer if possible to avoid extra memory usage.
  • Loading branch information
Leayal committed Aug 2, 2024
1 parent 69f4364 commit e6e4da4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Snowbreak/SnowBreakHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,25 @@ public async Task<Uri[]> FetchResourceURL(bool allowOfficialFetchingFromLauncher
if (scanFoundStart != -1 && scanFoundEnd != -1)
{
var jsonLengthInBytes = (int)((scanFoundEnd + 1) - scanFoundStart);
if (false && tmpbuffer.Length >= jsonLengthInBytes)
if (tmpbuffer.Length >= jsonLengthInBytes)
{
localStream.Position = scanFoundStart;
await localStream.ReadExactlyAsync(tmpbuffer, 0, jsonLengthInBytes, cancellationToken);
officialJsonDataRaw = encodingANSICompat.GetString(tmpbuffer, 0, jsonLengthInBytes);
}
else
{
var resultArray = new byte[jsonLengthInBytes];
localStream.Position = scanFoundStart;
await localStream.ReadExactlyAsync(resultArray, cancellationToken);
officialJsonDataRaw = encodingANSICompat.GetString(resultArray);
var encodeBuffer = ArrayPool<byte>.Shared.Rent(jsonLengthInBytes);
try
{
localStream.Position = scanFoundStart;
await localStream.ReadExactlyAsync(encodeBuffer, 0, jsonLengthInBytes, cancellationToken);
officialJsonDataRaw = encodingANSICompat.GetString(encodeBuffer, 0, jsonLengthInBytes);
}
finally
{
ArrayPool<byte>.Shared.Return(encodeBuffer);
}
}
}
}
Expand Down

0 comments on commit e6e4da4

Please sign in to comment.