Skip to content

Commit

Permalink
Apply CA1835 Roslyn fixes on .NET Libraries
Browse files Browse the repository at this point in the history
- Prefer the Stream.ReadAsync and Stream.WriteAsync memory overloads.
- Unit test fix suggested by buyaa-n to adjust the usage of these overloads.
  • Loading branch information
carlossanlop committed May 11, 2020
1 parent 3764f37 commit c751ed4
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 16 deletions.
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);
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ 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

0 comments on commit c751ed4

Please sign in to comment.