From c751ed4d103a1495b05eb2b25ec3139825e57f31 Mon Sep 17 00:00:00 2001 From: carlossanlop Date: Mon, 11 May 2020 14:50:26 -0700 Subject: [PATCH] Apply CA1835 Roslyn fixes on .NET Libraries - Prefer the Stream.ReadAsync and Stream.WriteAsync memory overloads. - Unit test fix suggested by buyaa-n to adjust the usage of these overloads. --- .../System.Net.Mail/src/System/Net/BufferedReadStream.cs | 2 +- .../tests/misc/LoadSaveAsyncTests.cs | 6 ++++++ .../src/System/Xml/Core/XmlEncodedRawTextWriterAsync.cs | 6 +++--- .../Xml/Core/XmlRawTextWriterGeneratorAsync.ttinclude | 8 ++++---- .../src/System/Xml/Core/XmlTextReaderImplAsync.cs | 6 +++--- .../src/System/Xml/Core/XmlUtf8RawTextWriterAsync.cs | 2 +- .../System.Private.Xml/tests/XmlWriter/DisposeTests.cs | 4 ++++ .../src/System/IO/StreamOperationsImplementation.cs | 4 ++-- .../src/System/Text/Json/Document/JsonDocument.Parse.cs | 8 ++++++++ .../src/System/Media/SoundPlayer.cs | 4 ++-- 10 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Net.Mail/src/System/Net/BufferedReadStream.cs b/src/libraries/System.Net.Mail/src/System/Net/BufferedReadStream.cs index 95f851e240dc6..c9f5c51e5c346 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/BufferedReadStream.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/BufferedReadStream.cs @@ -97,7 +97,7 @@ public override Task ReadAsync(byte[] buffer, int offset, int count, Cancel private async Task ReadMoreAsync(int bytesAlreadyRead, byte[] buffer, int offset, int count, CancellationToken cancellationToken) { - int returnValue = await base.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); + int returnValue = await base.ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false); return bytesAlreadyRead + returnValue; } diff --git a/src/libraries/System.Private.Xml.Linq/tests/misc/LoadSaveAsyncTests.cs b/src/libraries/System.Private.Xml.Linq/tests/misc/LoadSaveAsyncTests.cs index 2e9db3bc282ca..774c7bc4540ec 100644 --- a/src/libraries/System.Private.Xml.Linq/tests/misc/LoadSaveAsyncTests.cs +++ b/src/libraries/System.Private.Xml.Linq/tests/misc/LoadSaveAsyncTests.cs @@ -415,6 +415,12 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati Assert.True(_isAsync, "Stream is not in asynchronous mode when asynchronous Write is called"); return Task.CompletedTask; } + + public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default) + { + Assert.True(_isAsync, "Stream is not in asynchronous mode when asynchronous Write is called"); + return default; + } } public class CheckSyncAsyncStream : Stream diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriterAsync.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriterAsync.cs index 13a694c1f98aa..dadeda60d0756 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriterAsync.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriterAsync.cs @@ -645,7 +645,7 @@ protected virtual async Task FlushBufferAsync() if (bufPos - 1 > 0) { // Write text to TextWriter - await writer.WriteAsync(bufChars, 1, bufPos - 1).ConfigureAwait(false); + await writer.WriteAsync(bufChars.AsMemory(1, bufPos - 1)).ConfigureAwait(false); } } } @@ -688,13 +688,13 @@ private async Task EncodeCharsAsync(int startOffset, int endOffset, bool writeAl bufBytesUsed += bEnc; if (bufBytesUsed >= (bufBytes.Length - 16)) { - await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false); + await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false); bufBytesUsed = 0; } } if (writeAllToStream && bufBytesUsed > 0) { - await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false); + await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false); bufBytesUsed = 0; } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGeneratorAsync.ttinclude b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGeneratorAsync.ttinclude index 52032722d5d9a..a87f83b553c79 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGeneratorAsync.ttinclude +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGeneratorAsync.ttinclude @@ -624,7 +624,7 @@ namespace System.Xml if (bufPos - 1 > 0) { Debug.Assert(stream != null); - await stream.WriteAsync(bufBytes, 1, bufPos - 1).ConfigureAwait(false); + await stream.WriteAsync(bufBytes.AsMemory(1, bufPos - 1)).ConfigureAwait(false); } <# } else { #> Debug.Assert(stream != null || writer != null); @@ -656,7 +656,7 @@ namespace System.Xml if (bufPos - 1 > 0) { // Write text to TextWriter - await writer.WriteAsync(<#= BufferName #>, 1, bufPos - 1).ConfigureAwait(false); + await writer.WriteAsync(<#= BufferName #>.AsMemory(1, bufPos - 1)).ConfigureAwait(false); } } <# } #> @@ -711,13 +711,13 @@ namespace System.Xml bufBytesUsed += bEnc; if (bufBytesUsed >= (bufBytes.Length - 16)) { - await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false); + await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false); bufBytesUsed = 0; } } if (writeAllToStream && bufBytesUsed > 0) { - await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false); + await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false); bufBytesUsed = 0; } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs index a6e8135b92176..50a457599f8f0 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs @@ -975,7 +975,7 @@ private async Task InitStreamInputAsync(Uri baseUri, string baseUriStr, Stream s _ps.bytePos = 0; while (_ps.bytesUsed < 4 && _ps.bytes.Length - _ps.bytesUsed > 0) { - int read = await stream.ReadAsync(_ps.bytes, _ps.bytesUsed, _ps.bytes.Length - _ps.bytesUsed).ConfigureAwait(false); + int read = await stream.ReadAsync(_ps.bytes.AsMemory(_ps.bytesUsed)).ConfigureAwait(false); if (read == 0) { _ps.isStreamEof = true; @@ -1192,7 +1192,7 @@ private async Task ReadDataAsync() // read new bytes if (_ps.bytePos == _ps.bytesUsed && _ps.bytes.Length - _ps.bytesUsed > 0) { - int read = await _ps.stream.ReadAsync(_ps.bytes, _ps.bytesUsed, _ps.bytes.Length - _ps.bytesUsed).ConfigureAwait(false); + int read = await _ps.stream.ReadAsync(_ps.bytes.AsMemory(_ps.bytesUsed)).ConfigureAwait(false); if (read == 0) { _ps.isStreamEof = true; @@ -1214,7 +1214,7 @@ private async Task ReadDataAsync() else if (_ps.textReader != null) { // read chars - charsRead = await _ps.textReader.ReadAsync(_ps.chars, _ps.charsUsed, _ps.chars.Length - _ps.charsUsed - 1).ConfigureAwait(false); + charsRead = await _ps.textReader.ReadAsync(_ps.chars.AsMemory(_ps.charsUsed, _ps.chars.Length - _ps.charsUsed - 1)).ConfigureAwait(false); _ps.charsUsed += charsRead; } else diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriterAsync.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriterAsync.cs index d4916eb83445a..950ebe60acd85 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriterAsync.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriterAsync.cs @@ -552,7 +552,7 @@ protected virtual async Task FlushBufferAsync() if (bufPos - 1 > 0) { Debug.Assert(stream != null); - await stream.WriteAsync(bufBytes, 1, bufPos - 1).ConfigureAwait(false); + await stream.WriteAsync(bufBytes.AsMemory(1, bufPos - 1)).ConfigureAwait(false); } } } diff --git a/src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs b/src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs index 7701003b2c186..47fe5d8215cbd 100644 --- a/src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs +++ b/src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs @@ -278,6 +278,10 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati { return Task.CompletedTask; } + public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default) + { + return default; + } } } } diff --git a/src/libraries/System.Runtime.WindowsRuntime/src/System/IO/StreamOperationsImplementation.cs b/src/libraries/System.Runtime.WindowsRuntime/src/System/IO/StreamOperationsImplementation.cs index 9414d9f042cc2..5fd829c8ed949 100644 --- a/src/libraries/System.Runtime.WindowsRuntime/src/System/IO/StreamOperationsImplementation.cs +++ b/src/libraries/System.Runtime.WindowsRuntime/src/System/IO/StreamOperationsImplementation.cs @@ -111,7 +111,7 @@ internal static IAsyncOperationWithProgress ReadAsync_AbstractStr try { // Read asynchronously: - bytesRead = await stream.ReadAsync(data!, offset + bytesCompleted, bytesRequested - bytesCompleted, cancelToken) + bytesRead = await stream.ReadAsync(data!.AsMemory(offset + bytesCompleted, bytesRequested - bytesCompleted), cancelToken) .ConfigureAwait(continueOnCapturedContext: false); // We will continue here on a different thread when read async completed: @@ -181,7 +181,7 @@ internal static IAsyncOperationWithProgress WriteAsync_AbstractStrea int bytesToWrite = (int)buffer.Length; - await stream.WriteAsync(data, offset, bytesToWrite, cancelToken).ConfigureAwait(continueOnCapturedContext: false); + await stream.WriteAsync(data.AsMemory(offset, bytesToWrite), cancelToken).ConfigureAwait(continueOnCapturedContext: false); if (progressListener != null) progressListener.Report((uint)bytesToWrite); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs index 0ffe89e51e823..49770ef3f5f67 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs @@ -675,9 +675,13 @@ private static async Debug.Assert(rented.Length >= JsonConstants.Utf8Bom.Length); lastRead = await stream.ReadAsync( +#if BUILDING_INBOX_LIBRARY + rented.AsMemory(written, utf8BomLength - written), +#else rented, written, utf8BomLength - written, +#endif cancellationToken).ConfigureAwait(false); written += lastRead; @@ -702,9 +706,13 @@ private static async } lastRead = await stream.ReadAsync( +#if BUILDING_INBOX_LIBRARY + rented.AsMemory(written), +#else rented, written, rented.Length - written, +#endif cancellationToken).ConfigureAwait(false); written += lastRead; diff --git a/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs b/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs index 69d474b8803a1..ae9cd72ae2e24 100644 --- a/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs +++ b/src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs @@ -489,7 +489,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) _streamData = new byte[BlockSize]; - int readBytes = await _stream.ReadAsync(_streamData, _currentPos, BlockSize, cancellationToken).ConfigureAwait(false); + int readBytes = await _stream.ReadAsync(_streamData.AsMemory(_currentPos, BlockSize), cancellationToken).ConfigureAwait(false); int totalBytes = readBytes; while (readBytes > 0) @@ -501,7 +501,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) Array.Copy(_streamData, newData, _streamData.Length); _streamData = newData; } - readBytes = await _stream.ReadAsync(_streamData, _currentPos, BlockSize, cancellationToken).ConfigureAwait(false); + readBytes = await _stream.ReadAsync(_streamData.AsMemory(_currentPos, BlockSize), cancellationToken).ConfigureAwait(false); totalBytes += readBytes; }