CA1835: Prefer the memory-based overloads of ReadAsync/WriteAsync methods in stream-based classes
MODIFY
WalletWasabi.Gui/Rpc/JsonRpcServer.cs
WalletWasabi.Gui/Rpc/JsonRpcServer.cs
@@ -69,7 +69,7 @@ namespace WalletWasabi.Gui.Rpc
response.ContentType = "application/json-rpc";
var output = response.OutputStream;
var buffer = Encoding.UTF8.GetBytes(result);
- await output.WriteAsync(buffer, 0, buffer.Length, stoppingToken).ConfigureAwait(false);
+ await output.WriteAsync(buffer.AsMemory(0, buffer.Length), stoppingToken).ConfigureAwait(false);
await output.FlushAsync(stoppingToken).ConfigureAwait(false);
}
}

MODIFY
WalletWasabi/Extensions/StreamExtensions.cs
WalletWasabi/Extensions/StreamExtensions.cs
using System.Threading;
using System.Threading.Tasks;
+using System;
namespace System.IO
{
@@ -8,7 +9,7 @@ namespace System.IO
public static async Task<int> ReadByteAsync(this Stream stream, CancellationToken ctsToken = default)
{
var buf = new byte[1];
- var len = await stream.ReadAsync(buf, 0, 1, ctsToken).ConfigureAwait(false);
+ var len = await stream.ReadAsync(buf.AsMemory(0, 1), ctsToken).ConfigureAwait(false);
if (len == 0)
{
return -1;
@@ -22,7 +23,7 @@ namespace System.IO
var left = count;
while (left != 0)
{
- var read = await stream.ReadAsync(buffer, count - left, left, ctsToken).ConfigureAwait(false);
+ var read = await stream.ReadAsync(buffer.AsMemory(count - left, left), ctsToken).ConfigureAwait(false);
left -= read;
}
return count - left;

MODIFY
WalletWasabi/Helpers/NBitcoinHelpers.cs
WalletWasabi/Helpers/NBitcoinHelpers.cs
@@ -132,9 +132,9 @@ namespace WalletWasabi.Helpers
using (var fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
data = new byte[fs.Length - 32];
- await fs.ReadAsync(data, 0, data.Length).ConfigureAwait(false);
+ await fs.ReadAsync(data.AsMemory(0, data.Length)).ConfigureAwait(false);
hash = new byte[32];
- await fs.ReadAsync(hash, 0, 32).ConfigureAwait(false);
+ await fs.ReadAsync(hash.AsMemory(0, 32)).ConfigureAwait(false);
}
var actual = Hashes.DoubleSHA256(data);
var expected = new uint256(hash);

MODIFY
WalletWasabi/Tor/Http/TorHttpClient.cs
WalletWasabi/Tor/Http/TorHttpClient.cs
@@ -263,7 +263,7 @@ namespace WalletWasabi.Tor.Http
Stream transportStream = TorSocks5Client.GetTransportStream();
- await transportStream.WriteAsync(bytes, 0, bytes.Length, cancel).ConfigureAwait(false);
+ await transportStream.WriteAsync(bytes.AsMemory(0, bytes.Length), cancel).ConfigureAwait(false);
await transportStream.FlushAsync(cancel).ConfigureAwait(false);
return await HttpResponseMessageExtensions.CreateNewAsync(transportStream, request.Method).ConfigureAwait(false);

MODIFY
WalletWasabi/Tor/Socks5/TorSocks5Client.cs
WalletWasabi/Tor/Socks5/TorSocks5Client.cs
@@ -350,7 +350,7 @@ namespace WalletWasabi.Tor.Socks5
var stream = TcpClient.GetStream();
// Write data to the stream
- await stream.WriteAsync(sendBuffer, 0, sendBuffer.Length, cancellationToken).ConfigureAwait(false);
+ await stream.WriteAsync(sendBuffer.AsMemory(0, sendBuffer.Length), cancellationToken).ConfigureAwait(false);
await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
// If receiveBufferSize is null, zero or negative or bigger than TcpClient.ReceiveBufferSize
@@ -363,7 +363,7 @@ namespace WalletWasabi.Tor.Socks5
// Receive the response
var receiveBuffer = new byte[actualReceiveBufferSize];
- int receiveCount = await stream.ReadAsync(receiveBuffer, 0, actualReceiveBufferSize, cancellationToken).ConfigureAwait(false);
+ int receiveCount = await stream.ReadAsync(receiveBuffer.AsMemory(0, actualReceiveBufferSize), cancellationToken).ConfigureAwait(false);
if (receiveCount <= 0)
{
@@ -381,7 +381,7 @@ namespace WalletWasabi.Tor.Socks5
while (stream.DataAvailable)
{
Array.Clear(receiveBuffer, 0, receiveBuffer.Length);
- receiveCount = await stream.ReadAsync(receiveBuffer, 0, actualReceiveBufferSize, cancellationToken).ConfigureAwait(false);
+ receiveCount = await stream.ReadAsync(receiveBuffer.AsMemory(0, actualReceiveBufferSize), cancellationToken).ConfigureAwait(false);
if (receiveCount <= 0)
{
throw new TorConnectionException($"Not connected to Tor SOCKS5 proxy: {TorSocks5EndPoint}.");

CA1835: Prefer the memory-based overloads of ReadAsync/WriteAsync methods in stream-based classes