Skip to content

Commit 14094d2

Browse files
authored
Apply CA1835 - Prefer memory overloads for Stream.ReadAsync/WriteAsync (#35941)
* 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. * Additional missing unit test overload for ReadOnlyMemory
1 parent f8410ed commit 14094d2

File tree

10 files changed

+40
-16
lines changed

10 files changed

+40
-16
lines changed

src/libraries/System.Net.Mail/src/System/Net/BufferedReadStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
9797

9898
private async Task<int> ReadMoreAsync(int bytesAlreadyRead, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
9999
{
100-
int returnValue = await base.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
100+
int returnValue = await base.ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false);
101101
return bytesAlreadyRead + returnValue;
102102
}
103103

src/libraries/System.Private.Xml.Linq/tests/misc/LoadSaveAsyncTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
415415
Assert.True(_isAsync, "Stream is not in asynchronous mode when asynchronous Write is called");
416416
return Task.CompletedTask;
417417
}
418+
419+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default)
420+
{
421+
Assert.True(_isAsync, "Stream is not in asynchronous mode when asynchronous Write is called");
422+
return default;
423+
}
418424
}
419425

420426
public class CheckSyncAsyncStream : Stream

src/libraries/System.Private.Xml/src/System/Xml/Core/XmlEncodedRawTextWriterAsync.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ protected virtual async Task FlushBufferAsync()
645645
if (bufPos - 1 > 0)
646646
{
647647
// Write text to TextWriter
648-
await writer.WriteAsync(bufChars, 1, bufPos - 1).ConfigureAwait(false);
648+
await writer.WriteAsync(bufChars.AsMemory(1, bufPos - 1)).ConfigureAwait(false);
649649
}
650650
}
651651
}
@@ -688,13 +688,13 @@ private async Task EncodeCharsAsync(int startOffset, int endOffset, bool writeAl
688688
bufBytesUsed += bEnc;
689689
if (bufBytesUsed >= (bufBytes.Length - 16))
690690
{
691-
await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false);
691+
await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false);
692692
bufBytesUsed = 0;
693693
}
694694
}
695695
if (writeAllToStream && bufBytesUsed > 0)
696696
{
697-
await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false);
697+
await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false);
698698
bufBytesUsed = 0;
699699
}
700700
}

src/libraries/System.Private.Xml/src/System/Xml/Core/XmlRawTextWriterGeneratorAsync.ttinclude

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ namespace System.Xml
624624
if (bufPos - 1 > 0)
625625
{
626626
Debug.Assert(stream != null);
627-
await stream.WriteAsync(bufBytes, 1, bufPos - 1).ConfigureAwait(false);
627+
await stream.WriteAsync(bufBytes.AsMemory(1, bufPos - 1)).ConfigureAwait(false);
628628
}
629629
<# } else { #>
630630
Debug.Assert(stream != null || writer != null);
@@ -656,7 +656,7 @@ namespace System.Xml
656656
if (bufPos - 1 > 0)
657657
{
658658
// Write text to TextWriter
659-
await writer.WriteAsync(<#= BufferName #>, 1, bufPos - 1).ConfigureAwait(false);
659+
await writer.WriteAsync(<#= BufferName #>.AsMemory(1, bufPos - 1)).ConfigureAwait(false);
660660
}
661661
}
662662
<# } #>
@@ -711,13 +711,13 @@ namespace System.Xml
711711
bufBytesUsed += bEnc;
712712
if (bufBytesUsed >= (bufBytes.Length - 16))
713713
{
714-
await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false);
714+
await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false);
715715
bufBytesUsed = 0;
716716
}
717717
}
718718
if (writeAllToStream && bufBytesUsed > 0)
719719
{
720-
await stream.WriteAsync(bufBytes, 0, bufBytesUsed).ConfigureAwait(false);
720+
await stream.WriteAsync(bufBytes.AsMemory(0, bufBytesUsed)).ConfigureAwait(false);
721721
bufBytesUsed = 0;
722722
}
723723
}

src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ private async Task InitStreamInputAsync(Uri baseUri, string baseUriStr, Stream s
975975
_ps.bytePos = 0;
976976
while (_ps.bytesUsed < 4 && _ps.bytes.Length - _ps.bytesUsed > 0)
977977
{
978-
int read = await stream.ReadAsync(_ps.bytes, _ps.bytesUsed, _ps.bytes.Length - _ps.bytesUsed).ConfigureAwait(false);
978+
int read = await stream.ReadAsync(_ps.bytes.AsMemory(_ps.bytesUsed)).ConfigureAwait(false);
979979
if (read == 0)
980980
{
981981
_ps.isStreamEof = true;
@@ -1192,7 +1192,7 @@ private async Task<int> ReadDataAsync()
11921192
// read new bytes
11931193
if (_ps.bytePos == _ps.bytesUsed && _ps.bytes.Length - _ps.bytesUsed > 0)
11941194
{
1195-
int read = await _ps.stream.ReadAsync(_ps.bytes, _ps.bytesUsed, _ps.bytes.Length - _ps.bytesUsed).ConfigureAwait(false);
1195+
int read = await _ps.stream.ReadAsync(_ps.bytes.AsMemory(_ps.bytesUsed)).ConfigureAwait(false);
11961196
if (read == 0)
11971197
{
11981198
_ps.isStreamEof = true;
@@ -1214,7 +1214,7 @@ private async Task<int> ReadDataAsync()
12141214
else if (_ps.textReader != null)
12151215
{
12161216
// read chars
1217-
charsRead = await _ps.textReader.ReadAsync(_ps.chars, _ps.charsUsed, _ps.chars.Length - _ps.charsUsed - 1).ConfigureAwait(false);
1217+
charsRead = await _ps.textReader.ReadAsync(_ps.chars.AsMemory(_ps.charsUsed, _ps.chars.Length - _ps.charsUsed - 1)).ConfigureAwait(false);
12181218
_ps.charsUsed += charsRead;
12191219
}
12201220
else

src/libraries/System.Private.Xml/src/System/Xml/Core/XmlUtf8RawTextWriterAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ protected virtual async Task FlushBufferAsync()
552552
if (bufPos - 1 > 0)
553553
{
554554
Debug.Assert(stream != null);
555-
await stream.WriteAsync(bufBytes, 1, bufPos - 1).ConfigureAwait(false);
555+
await stream.WriteAsync(bufBytes.AsMemory(1, bufPos - 1)).ConfigureAwait(false);
556556
}
557557
}
558558
}

src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ public override Task WriteAsync(char[] buffer, int offset, int count)
255255
{
256256
return Task.CompletedTask;
257257
}
258+
259+
public override Task WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
260+
{
261+
return Task.CompletedTask;
262+
}
258263
}
259264

260265
internal class AsyncOnlyStream : MemoryStream
@@ -278,6 +283,11 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
278283
{
279284
return Task.CompletedTask;
280285
}
286+
287+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default)
288+
{
289+
return default;
290+
}
281291
}
282292
}
283293
}

src/libraries/System.Runtime.WindowsRuntime/src/System/IO/StreamOperationsImplementation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internal static IAsyncOperationWithProgress<IBuffer, uint> ReadAsync_AbstractStr
111111
try
112112
{
113113
// Read asynchronously:
114-
bytesRead = await stream.ReadAsync(data!, offset + bytesCompleted, bytesRequested - bytesCompleted, cancelToken)
114+
bytesRead = await stream.ReadAsync(data!.AsMemory(offset + bytesCompleted, bytesRequested - bytesCompleted), cancelToken)
115115
.ConfigureAwait(continueOnCapturedContext: false);
116116

117117
// We will continue here on a different thread when read async completed:
@@ -181,7 +181,7 @@ internal static IAsyncOperationWithProgress<uint, uint> WriteAsync_AbstractStrea
181181

182182
int bytesToWrite = (int)buffer.Length;
183183

184-
await stream.WriteAsync(data, offset, bytesToWrite, cancelToken).ConfigureAwait(continueOnCapturedContext: false);
184+
await stream.WriteAsync(data.AsMemory(offset, bytesToWrite), cancelToken).ConfigureAwait(continueOnCapturedContext: false);
185185

186186
if (progressListener != null)
187187
progressListener.Report((uint)bytesToWrite);

src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,13 @@ private static async
675675
Debug.Assert(rented.Length >= JsonConstants.Utf8Bom.Length);
676676

677677
lastRead = await stream.ReadAsync(
678+
#if BUILDING_INBOX_LIBRARY
679+
rented.AsMemory(written, utf8BomLength - written),
680+
#else
678681
rented,
679682
written,
680683
utf8BomLength - written,
684+
#endif
681685
cancellationToken).ConfigureAwait(false);
682686

683687
written += lastRead;
@@ -702,9 +706,13 @@ private static async
702706
}
703707

704708
lastRead = await stream.ReadAsync(
709+
#if BUILDING_INBOX_LIBRARY
710+
rented.AsMemory(written),
711+
#else
705712
rented,
706713
written,
707714
rented.Length - written,
715+
#endif
708716
cancellationToken).ConfigureAwait(false);
709717

710718
written += lastRead;

src/libraries/System.Windows.Extensions/src/System/Media/SoundPlayer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken)
489489

490490
_streamData = new byte[BlockSize];
491491

492-
int readBytes = await _stream.ReadAsync(_streamData, _currentPos, BlockSize, cancellationToken).ConfigureAwait(false);
492+
int readBytes = await _stream.ReadAsync(_streamData.AsMemory(_currentPos, BlockSize), cancellationToken).ConfigureAwait(false);
493493
int totalBytes = readBytes;
494494

495495
while (readBytes > 0)
@@ -501,7 +501,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken)
501501
Array.Copy(_streamData, newData, _streamData.Length);
502502
_streamData = newData;
503503
}
504-
readBytes = await _stream.ReadAsync(_streamData, _currentPos, BlockSize, cancellationToken).ConfigureAwait(false);
504+
readBytes = await _stream.ReadAsync(_streamData.AsMemory(_currentPos, BlockSize), cancellationToken).ConfigureAwait(false);
505505
totalBytes += readBytes;
506506
}
507507

0 commit comments

Comments
 (0)