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

Apply CA1835 - Prefer memory overloads for Stream.ReadAsync/WriteAsync #35941

Merged
merged 2 commits into from
May 12, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel

private async Task<int> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> source, CancellationToken cancellationToken = default)
{
Assert.True(_isAsync, "Stream is not in asynchronous mode when asynchronous Write is called");
return default;
}
}

public class CheckSyncAsyncStream : Stream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
carlossanlop marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
<# } #>
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1192,7 +1192,7 @@ private async Task<int> 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;
Expand All @@ -1214,7 +1214,7 @@ private async Task<int> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
carlossanlop marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Private.Xml/tests/XmlWriter/DisposeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ public override Task WriteAsync(char[] buffer, int offset, int count)
{
return Task.CompletedTask;
}

public override Task WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
}

internal class AsyncOnlyStream : MemoryStream
Expand All @@ -278,6 +283,11 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
{
return Task.CompletedTask;
}

public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default)
{
return default;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal static IAsyncOperationWithProgress<IBuffer, uint> 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:
Expand Down Expand Up @@ -181,7 +181,7 @@ internal static IAsyncOperationWithProgress<uint, uint> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down