From: Anirudh Agnihotry Date: Fri, 23 Mar 2018 12:43:47 +0000 (-0700) Subject: Rename new Stream.Read/Write{Async} Span/Memory source/Destination arguments to buffe... X-Git-Tag: accepted/tizen/unified/20190422.045933~2536 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f819fdcabea2e79962235012af296178f3a3b3bf;p=platform%2Fupstream%2Fcoreclr.git Rename new Stream.Read/Write{Async} Span/Memory source/Destination arguments to buffer (#17141) * changed to buffer * More Common changes * fixed * another fix --- diff --git a/src/mscorlib/shared/System/IO/FileStream.cs b/src/mscorlib/shared/System/IO/FileStream.cs index 717b73f..a6ad63c 100644 --- a/src/mscorlib/shared/System/IO/FileStream.cs +++ b/src/mscorlib/shared/System/IO/FileStream.cs @@ -304,7 +304,7 @@ namespace System.IO ReadSpan(new Span(array, offset, count)); } - public override int Read(Span destination) + public override int Read(Span buffer) { if (GetType() == typeof(FileStream) && !_useAsyncIO) { @@ -312,7 +312,7 @@ namespace System.IO { throw Error.GetFileNotOpen(); } - return ReadSpan(destination); + return ReadSpan(buffer); } else { @@ -322,7 +322,7 @@ namespace System.IO // of Read(byte[],int,int) overload. Or if the stream is in async mode, we can't call the // synchronous ReadSpan, so we similarly call the base Read, which will turn delegate to // Read(byte[],int,int), which will do the right thing if we're in async mode. - return base.Read(destination); + return base.Read(buffer); } } @@ -355,14 +355,14 @@ namespace System.IO return ReadAsyncTask(buffer, offset, count, cancellationToken); } - public override ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (!_useAsyncIO || GetType() != typeof(FileStream)) { // If we're not using async I/O, delegate to the base, which will queue a call to Read. // Or if this isn't a concrete FileStream, a derived type may have overridden ReadAsync(byte[],...), // which was introduced first, so delegate to the base which will delegate to that. - return base.ReadAsync(destination, cancellationToken); + return base.ReadAsync(buffer, cancellationToken); } if (cancellationToken.IsCancellationRequested) @@ -375,7 +375,7 @@ namespace System.IO throw Error.GetFileNotOpen(); } - Task t = ReadAsyncInternal(destination, cancellationToken, out int synchronousResult); + Task t = ReadAsyncInternal(buffer, cancellationToken, out int synchronousResult); return t != null ? new ValueTask(t) : new ValueTask(synchronousResult); @@ -412,7 +412,7 @@ namespace System.IO } } - public override void Write(ReadOnlySpan destination) + public override void Write(ReadOnlySpan buffer) { if (GetType() == typeof(FileStream) && !_useAsyncIO) { @@ -420,7 +420,7 @@ namespace System.IO { throw Error.GetFileNotOpen(); } - WriteSpan(destination); + WriteSpan(buffer); } else { @@ -430,7 +430,7 @@ namespace System.IO // of Write(byte[],int,int) overload. Or if the stream is in async mode, we can't call the // synchronous WriteSpan, so we similarly call the base Write, which will turn delegate to // Write(byte[],int,int), which will do the right thing if we're in async mode. - base.Write(destination); + base.Write(buffer); } } @@ -461,14 +461,14 @@ namespace System.IO return WriteAsyncInternal(new ReadOnlyMemory(buffer, offset, count), cancellationToken).AsTask(); } - public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (!_useAsyncIO || GetType() != typeof(FileStream)) { // If we're not using async I/O, delegate to the base, which will queue a call to Write. // Or if this isn't a concrete FileStream, a derived type may have overridden WriteAsync(byte[],...), // which was introduced first, so delegate to the base which will delegate to that. - return base.WriteAsync(source, cancellationToken); + return base.WriteAsync(buffer, cancellationToken); } if (cancellationToken.IsCancellationRequested) @@ -481,7 +481,7 @@ namespace System.IO throw Error.GetFileNotOpen(); } - return WriteAsyncInternal(source, cancellationToken); + return WriteAsyncInternal(buffer, cancellationToken); } /// diff --git a/src/mscorlib/shared/System/IO/MemoryStream.cs b/src/mscorlib/shared/System/IO/MemoryStream.cs index ffe7f60..fb319de 100644 --- a/src/mscorlib/shared/System/IO/MemoryStream.cs +++ b/src/mscorlib/shared/System/IO/MemoryStream.cs @@ -367,19 +367,19 @@ namespace System.IO return n; } - public override int Read(Span destination) + public override int Read(Span buffer) { if (GetType() != typeof(MemoryStream)) { // MemoryStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior // to this Read(Span) overload being introduced. In that case, this Read(Span) overload // should use the behavior of Read(byte[],int,int) overload. - return base.Read(destination); + return base.Read(buffer); } EnsureNotClosed(); - int n = Math.Min(_length - _position, destination.Length); + int n = Math.Min(_length - _position, buffer.Length); if (n <= 0) return 0; @@ -387,7 +387,7 @@ namespace System.IO // Read(byte[], int, int) has an n <= 8 optimization, presumably based // on benchmarking. Determine if/where such a cut-off is here and add // an equivalent optimization if necessary. - new Span(_buffer, _position, n).CopyTo(destination); + new Span(_buffer, _position, n).CopyTo(buffer); _position += n; return n; @@ -426,7 +426,7 @@ namespace System.IO } } - public override ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { @@ -448,9 +448,9 @@ namespace System.IO // something other than an array and this is a MemoryStream-derived type that doesn't override Read(Span) will // it then fall back to doing the ArrayPool/copy behavior. return new ValueTask( - MemoryMarshal.TryGetArray(destination, out ArraySegment destinationArray) ? + MemoryMarshal.TryGetArray(buffer, out ArraySegment destinationArray) ? Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) : - Read(destination.Span)); + Read(buffer.Span)); } catch (OperationCanceledException oce) { @@ -681,14 +681,14 @@ namespace System.IO _position = i; } - public override void Write(ReadOnlySpan source) + public override void Write(ReadOnlySpan buffer) { if (GetType() != typeof(MemoryStream)) { // MemoryStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior // to this Write(Span) overload being introduced. In that case, this Write(Span) overload // should use the behavior of Write(byte[],int,int) overload. - base.Write(source); + base.Write(buffer); return; } @@ -696,7 +696,7 @@ namespace System.IO EnsureWriteable(); // Check for overflow - int i = _position + source.Length; + int i = _position + buffer.Length; if (i < 0) throw new IOException(SR.IO_StreamTooLong); @@ -718,7 +718,7 @@ namespace System.IO _length = i; } - source.CopyTo(new Span(_buffer, _position, source.Length)); + buffer.CopyTo(new Span(_buffer, _position, buffer.Length)); _position = i; } @@ -752,7 +752,7 @@ namespace System.IO } } - public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { @@ -763,13 +763,13 @@ namespace System.IO { // See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan). // Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency. - if (MemoryMarshal.TryGetArray(source, out ArraySegment sourceArray)) + if (MemoryMarshal.TryGetArray(buffer, out ArraySegment sourceArray)) { Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count); } else { - Write(source.Span); + Write(buffer.Span); } return default; } diff --git a/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs b/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs index dfcc05d..94331a2 100644 --- a/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs +++ b/src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs @@ -38,9 +38,9 @@ namespace System.IO Initialize(ptr, len, len, FileAccess.Read); } - public override int Read(Span destination) => ReadCore(destination); + public override int Read(Span buffer) => ReadCore(buffer); - public override void Write(ReadOnlySpan source) => WriteCore(source); + public override void Write(ReadOnlySpan buffer) => WriteCore(buffer); ~PinnedBufferMemoryStream() { diff --git a/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs b/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs index d1a1315..2bcb16f 100644 --- a/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs +++ b/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs @@ -379,22 +379,22 @@ namespace System.IO return ReadCore(new Span(buffer, offset, count)); } - public override int Read(Span destination) + public override int Read(Span buffer) { if (GetType() == typeof(UnmanagedMemoryStream)) { - return ReadCore(destination); + return ReadCore(buffer); } else { // UnmanagedMemoryStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior // to this Read(Span) overload being introduced. In that case, this Read(Span) overload // should use the behavior of Read(byte[],int,int) overload. - return base.Read(destination); + return base.Read(buffer); } } - internal int ReadCore(Span destination) + internal int ReadCore(Span buffer) { EnsureNotClosed(); EnsureReadable(); @@ -403,7 +403,7 @@ namespace System.IO // changes our position after we decide we can read some bytes. long pos = Interlocked.Read(ref _position); long len = Interlocked.Read(ref _length); - long n = Math.Min(len - pos, destination.Length); + long n = Math.Min(len - pos, buffer.Length); if (n <= 0) { return 0; @@ -418,7 +418,7 @@ namespace System.IO unsafe { - fixed (byte* pBuffer = &MemoryMarshal.GetReference(destination)) + fixed (byte* pBuffer = &MemoryMarshal.GetReference(buffer)) { if (_buffer != null) { @@ -486,9 +486,9 @@ namespace System.IO /// /// Reads bytes from stream and puts them into the buffer /// - /// Buffer to read the bytes to. + /// Buffer to read the bytes to. /// Token that can be used to cancel this operation. - public override ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { @@ -510,9 +510,9 @@ namespace System.IO // something other than an array and this is an UnmanagedMemoryStream-derived type that doesn't override Read(Span) will // it then fall back to doing the ArrayPool/copy behavior. return new ValueTask( - MemoryMarshal.TryGetArray(destination, out ArraySegment destinationArray) ? + MemoryMarshal.TryGetArray(buffer, out ArraySegment destinationArray) ? Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) : - Read(destination.Span)); + Read(buffer.Span)); } catch (Exception ex) { @@ -659,29 +659,29 @@ namespace System.IO WriteCore(new Span(buffer, offset, count)); } - public override void Write(ReadOnlySpan source) + public override void Write(ReadOnlySpan buffer) { if (GetType() == typeof(UnmanagedMemoryStream)) { - WriteCore(source); + WriteCore(buffer); } else { // UnmanagedMemoryStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior // to this Write(Span) overload being introduced. In that case, this Write(Span) overload // should use the behavior of Write(byte[],int,int) overload. - base.Write(source); + base.Write(buffer); } } - internal unsafe void WriteCore(ReadOnlySpan source) + internal unsafe void WriteCore(ReadOnlySpan buffer) { EnsureNotClosed(); EnsureWriteable(); long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition long len = Interlocked.Read(ref _length); - long n = pos + source.Length; + long n = pos + buffer.Length; // Check for overflow if (n < 0) { @@ -709,12 +709,12 @@ namespace System.IO } } - fixed (byte* pBuffer = &MemoryMarshal.GetReference(source)) + fixed (byte* pBuffer = &MemoryMarshal.GetReference(buffer)) { if (_buffer != null) { long bytesLeft = _capacity - pos; - if (bytesLeft < source.Length) + if (bytesLeft < buffer.Length) { throw new ArgumentException(SR.Arg_BufferTooSmall); } @@ -724,7 +724,7 @@ namespace System.IO try { _buffer.AcquirePointer(ref pointer); - Buffer.Memcpy(pointer + pos + _offset, pBuffer, source.Length); + Buffer.Memcpy(pointer + pos + _offset, pBuffer, buffer.Length); } finally { @@ -736,7 +736,7 @@ namespace System.IO } else { - Buffer.Memcpy(_mem + pos, pBuffer, source.Length); + Buffer.Memcpy(_mem + pos, pBuffer, buffer.Length); } } @@ -783,7 +783,7 @@ namespace System.IO /// /// Buffer that will be written. /// Token that can be used to cancel the operation. - public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) { if (cancellationToken.IsCancellationRequested) { @@ -794,13 +794,13 @@ namespace System.IO { // See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan). // Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency. - if (MemoryMarshal.TryGetArray(source, out ArraySegment sourceArray)) + if (MemoryMarshal.TryGetArray(buffer, out ArraySegment sourceArray)) { Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count); } else { - Write(source.Span); + Write(buffer.Span); } return default; } diff --git a/src/mscorlib/shared/System/IO/UnmanagedMemoryStreamWrapper.cs b/src/mscorlib/shared/System/IO/UnmanagedMemoryStreamWrapper.cs index f34c3c4..65a3396 100644 --- a/src/mscorlib/shared/System/IO/UnmanagedMemoryStreamWrapper.cs +++ b/src/mscorlib/shared/System/IO/UnmanagedMemoryStreamWrapper.cs @@ -112,9 +112,9 @@ namespace System.IO return _unmanagedStream.Read(buffer, offset, count); } - public override int Read(Span destination) + public override int Read(Span buffer) { - return _unmanagedStream.Read(destination); + return _unmanagedStream.Read(buffer); } public override int ReadByte() @@ -139,9 +139,9 @@ namespace System.IO _unmanagedStream.Write(buffer, offset, count); } - public override void Write(ReadOnlySpan source) + public override void Write(ReadOnlySpan buffer) { - _unmanagedStream.Write(source); + _unmanagedStream.Write(buffer); } public override void WriteByte(byte value) @@ -206,9 +206,9 @@ namespace System.IO return _unmanagedStream.ReadAsync(buffer, offset, count, cancellationToken); } - public override ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) { - return _unmanagedStream.ReadAsync(destination, cancellationToken); + return _unmanagedStream.ReadAsync(buffer, cancellationToken); } @@ -217,9 +217,9 @@ namespace System.IO return _unmanagedStream.WriteAsync(buffer, offset, count, cancellationToken); } - public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) { - return _unmanagedStream.WriteAsync(source, cancellationToken); + return _unmanagedStream.WriteAsync(buffer, cancellationToken); } } // class UnmanagedMemoryStreamWrapper } // namespace diff --git a/src/mscorlib/src/System/IO/Stream.cs b/src/mscorlib/src/System/IO/Stream.cs index ec29dc2..4cf8435 100644 --- a/src/mscorlib/src/System/IO/Stream.cs +++ b/src/mscorlib/src/System/IO/Stream.cs @@ -374,16 +374,16 @@ namespace System.IO : BeginEndReadAsync(buffer, offset, count); } - public virtual ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default(CancellationToken)) + public virtual ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) { - if (MemoryMarshal.TryGetArray(destination, out ArraySegment array)) + if (MemoryMarshal.TryGetArray(buffer, out ArraySegment array)) { return new ValueTask(ReadAsync(array.Array, array.Offset, array.Count, cancellationToken)); } else { - byte[] buffer = ArrayPool.Shared.Rent(destination.Length); - return FinishReadAsync(ReadAsync(buffer, 0, destination.Length, cancellationToken), buffer, destination); + byte[] sharedBuffer = ArrayPool.Shared.Rent(buffer.Length); + return FinishReadAsync(ReadAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer, buffer); async ValueTask FinishReadAsync(Task readTask, byte[] localBuffer, Memory localDestination) { @@ -683,17 +683,17 @@ namespace System.IO : BeginEndWriteAsync(buffer, offset, count); } - public virtual ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default(CancellationToken)) + public virtual ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) { - if (MemoryMarshal.TryGetArray(source, out ArraySegment array)) + if (MemoryMarshal.TryGetArray(buffer, out ArraySegment array)) { return new ValueTask(WriteAsync(array.Array, array.Offset, array.Count, cancellationToken)); } else { - byte[] buffer = ArrayPool.Shared.Rent(source.Length); - source.Span.CopyTo(buffer); - return new ValueTask(FinishWriteAsync(WriteAsync(buffer, 0, source.Length, cancellationToken), buffer)); + byte[] sharedBuffer = ArrayPool.Shared.Rent(buffer.Length); + buffer.Span.CopyTo(sharedBuffer); + return new ValueTask(FinishWriteAsync(WriteAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer)); } } @@ -738,20 +738,20 @@ namespace System.IO public abstract int Read(byte[] buffer, int offset, int count); - public virtual int Read(Span destination) + public virtual int Read(Span buffer) { - byte[] buffer = ArrayPool.Shared.Rent(destination.Length); + byte[] sharedBuffer = ArrayPool.Shared.Rent(buffer.Length); try { - int numRead = Read(buffer, 0, destination.Length); - if ((uint)numRead > destination.Length) + int numRead = Read(sharedBuffer, 0, buffer.Length); + if ((uint)numRead > buffer.Length) { throw new IOException(SR.IO_StreamTooLong); } - new Span(buffer, 0, numRead).CopyTo(destination); + new Span(sharedBuffer, 0, numRead).CopyTo(buffer); return numRead; } - finally { ArrayPool.Shared.Return(buffer); } + finally { ArrayPool.Shared.Return(sharedBuffer); } } // Reads one byte from the stream by calling Read(byte[], int, int). @@ -771,15 +771,15 @@ namespace System.IO public abstract void Write(byte[] buffer, int offset, int count); - public virtual void Write(ReadOnlySpan source) + public virtual void Write(ReadOnlySpan buffer) { - byte[] buffer = ArrayPool.Shared.Rent(source.Length); + byte[] sharedBuffer = ArrayPool.Shared.Rent(buffer.Length); try { - source.CopyTo(buffer); - Write(buffer, 0, source.Length); + buffer.CopyTo(sharedBuffer); + Write(sharedBuffer, 0, buffer.Length); } - finally { ArrayPool.Shared.Return(buffer); } + finally { ArrayPool.Shared.Return(sharedBuffer); } } // Writes one byte from the stream by calling Write(byte[], int, int). @@ -972,7 +972,7 @@ namespace System.IO return 0; } - public override int Read(Span destination) + public override int Read(Span buffer) { return 0; } @@ -982,7 +982,7 @@ namespace System.IO return AsyncTaskMethodBuilder.s_defaultResultTask; } - public override ValueTask ReadAsync(Memory destination, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) { return new ValueTask(0); } @@ -996,7 +996,7 @@ namespace System.IO { } - public override void Write(ReadOnlySpan source) + public override void Write(ReadOnlySpan buffer) { } @@ -1007,7 +1007,7 @@ namespace System.IO Task.CompletedTask; } - public override ValueTask WriteAsync(ReadOnlyMemory source, CancellationToken cancellationToken = default(CancellationToken)) + public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) { return cancellationToken.IsCancellationRequested ? new ValueTask(Task.FromCanceled(cancellationToken)) : @@ -1256,10 +1256,10 @@ namespace System.IO return _stream.Read(bytes, offset, count); } - public override int Read(Span destination) + public override int Read(Span buffer) { lock (_stream) - return _stream.Read(destination); + return _stream.Read(buffer); } public override int ReadByte() @@ -1313,10 +1313,10 @@ namespace System.IO _stream.Write(bytes, offset, count); } - public override void Write(ReadOnlySpan source) + public override void Write(ReadOnlySpan buffer) { lock (_stream) - _stream.Write(source); + _stream.Write(buffer); } public override void WriteByte(byte b)