-
Notifications
You must be signed in to change notification settings - Fork 5k
Use Span<T>-based methods in System.Net.Mail #115111
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e742ffd
Span-based IByteEncoder
rzikm c9cabdd
IEncodableStream.DecodeBytes
rzikm a020b34
IEncodableStream.EncodeBytes
rzikm 284b521
DelegatedStream and derived implementations.
rzikm f96c60e
Minor changes
rzikm 455ae2e
Update src/libraries/System.Net.Mail/src/System/Net/Base64Stream.cs
rzikm 86b8f0b
Apply suggestions from code review
rzikm 02672d5
Code review feedback
rzikm 457136f
Removed TODO
rzikm 4ce61e5
Minor changes
rzikm 2832126
Merge branch 'main' into smtp-spanify
rzikm 21f436a
Consistent exceptions
rzikm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Buffers; | ||
|
||
namespace System.Net | ||
{ | ||
|
@@ -23,96 +24,76 @@ internal BufferedReadStream(Stream stream, bool readMore) : base(stream) | |
_readMore = readMore; | ||
} | ||
|
||
public override bool CanWrite | ||
{ | ||
get | ||
{ | ||
return false; | ||
} | ||
} | ||
public override bool CanWrite => false; | ||
public override bool CanRead => BaseStream.CanRead; | ||
|
||
public override bool CanSeek | ||
{ | ||
get | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) => | ||
TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), callback, state); | ||
public override bool CanSeek => false; | ||
|
||
public override int EndRead(IAsyncResult asyncResult) => | ||
TaskToAsyncResult.End<int>(asyncResult); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
protected override int ReadInternal(Span<byte> buffer) | ||
{ | ||
int read = 0; | ||
if (_storedOffset < _storedLength) | ||
{ | ||
read = Math.Min(count, _storedLength - _storedOffset); | ||
Buffer.BlockCopy(_storedBuffer!, _storedOffset, buffer, offset, read); | ||
int read = Math.Min(buffer.Length, _storedLength - _storedOffset); | ||
_storedBuffer.AsSpan(_storedOffset, read).CopyTo(buffer); | ||
_storedOffset += read; | ||
if (read == count || !_readMore) | ||
if (read == buffer.Length || !_readMore) | ||
{ | ||
return read; | ||
} | ||
|
||
offset += read; | ||
count -= read; | ||
// Need to read more from the underlying stream | ||
return read + BaseStream.Read(buffer.Slice(read)); | ||
} | ||
return read + base.Read(buffer, offset, count); | ||
|
||
return BaseStream.Read(buffer); | ||
} | ||
|
||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
protected override ValueTask<int> ReadAsyncInternal(Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
int read; | ||
if (_storedOffset >= _storedLength) | ||
{ | ||
return base.ReadAsync(buffer, offset, count, cancellationToken); | ||
return BaseStream.ReadAsync(buffer, cancellationToken); | ||
} | ||
|
||
read = Math.Min(count, _storedLength - _storedOffset); | ||
Buffer.BlockCopy(_storedBuffer!, _storedOffset, buffer, offset, read); | ||
int read = Math.Min(buffer.Length, _storedLength - _storedOffset); | ||
_storedBuffer.AsMemory(_storedOffset, read).CopyTo(buffer); | ||
_storedOffset += read; | ||
if (read == count || !_readMore) | ||
if (read == buffer.Length || !_readMore) | ||
{ | ||
return Task.FromResult<int>(read); | ||
return new ValueTask<int>(read); | ||
} | ||
|
||
offset += read; | ||
count -= read; | ||
|
||
return ReadMoreAsync(read, buffer, offset, count, cancellationToken); | ||
// Need to read more from the underlying stream | ||
return ReadMoreAsync(read, buffer.Slice(read), cancellationToken); | ||
} | ||
|
||
private async Task<int> ReadMoreAsync(int bytesAlreadyRead, byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
private async ValueTask<int> ReadMoreAsync(int bytesAlreadyRead, Memory<byte> buffer, CancellationToken cancellationToken) | ||
{ | ||
int returnValue = await base.ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false); | ||
int returnValue = await BaseStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); | ||
return bytesAlreadyRead + returnValue; | ||
} | ||
|
||
public override int ReadByte() | ||
protected override void WriteInternal(ReadOnlySpan<byte> buffer) | ||
{ | ||
if (_storedOffset < _storedLength) | ||
{ | ||
return _storedBuffer![_storedOffset++]; | ||
} | ||
else | ||
{ | ||
return base.ReadByte(); | ||
} | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override ValueTask WriteAsyncInternal(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
// adds additional content to the beginning of the buffer | ||
// so the layout of the storedBuffer will be | ||
// <buffer><existingBuffer> | ||
// after calling push | ||
internal void Push(byte[] buffer, int offset, int count) | ||
internal void Push(ReadOnlySpan<byte> buffer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion for future: This looks replaceable with our ArrayBuffer helper |
||
{ | ||
if (count == 0) | ||
if (buffer.Length == 0) | ||
return; | ||
|
||
int count = buffer.Length; | ||
|
||
if (_storedOffset == _storedLength) | ||
{ | ||
if (_storedBuffer == null || _storedBuffer.Length < count) | ||
|
@@ -146,7 +127,7 @@ internal void Push(byte[] buffer, int offset, int count) | |
} | ||
} | ||
|
||
Buffer.BlockCopy(buffer, offset, _storedBuffer!, _storedOffset, count); | ||
buffer.CopyTo(_storedBuffer.AsSpan(_storedOffset)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to consider for the future if we're cleaning this up is to try and replace all of this with built-in base64 helpers.