Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f819fdc

Browse files
Anipikstephentoub
authored andcommitted
Rename new Stream.Read/Write{Async} Span/Memory source/Destination arguments to buffer (#17141)
* changed to buffer * More Common changes * fixed * another fix
1 parent 3225742 commit f819fdc

File tree

6 files changed

+86
-86
lines changed

6 files changed

+86
-86
lines changed

src/mscorlib/shared/System/IO/FileStream.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,15 @@ public override int Read(byte[] array, int offset, int count)
304304
ReadSpan(new Span<byte>(array, offset, count));
305305
}
306306

307-
public override int Read(Span<byte> destination)
307+
public override int Read(Span<byte> buffer)
308308
{
309309
if (GetType() == typeof(FileStream) && !_useAsyncIO)
310310
{
311311
if (_fileHandle.IsClosed)
312312
{
313313
throw Error.GetFileNotOpen();
314314
}
315-
return ReadSpan(destination);
315+
return ReadSpan(buffer);
316316
}
317317
else
318318
{
@@ -322,7 +322,7 @@ public override int Read(Span<byte> destination)
322322
// of Read(byte[],int,int) overload. Or if the stream is in async mode, we can't call the
323323
// synchronous ReadSpan, so we similarly call the base Read, which will turn delegate to
324324
// Read(byte[],int,int), which will do the right thing if we're in async mode.
325-
return base.Read(destination);
325+
return base.Read(buffer);
326326
}
327327
}
328328

@@ -355,14 +355,14 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
355355
return ReadAsyncTask(buffer, offset, count, cancellationToken);
356356
}
357357

358-
public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
358+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
359359
{
360360
if (!_useAsyncIO || GetType() != typeof(FileStream))
361361
{
362362
// If we're not using async I/O, delegate to the base, which will queue a call to Read.
363363
// Or if this isn't a concrete FileStream, a derived type may have overridden ReadAsync(byte[],...),
364364
// which was introduced first, so delegate to the base which will delegate to that.
365-
return base.ReadAsync(destination, cancellationToken);
365+
return base.ReadAsync(buffer, cancellationToken);
366366
}
367367

368368
if (cancellationToken.IsCancellationRequested)
@@ -375,7 +375,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
375375
throw Error.GetFileNotOpen();
376376
}
377377

378-
Task<int> t = ReadAsyncInternal(destination, cancellationToken, out int synchronousResult);
378+
Task<int> t = ReadAsyncInternal(buffer, cancellationToken, out int synchronousResult);
379379
return t != null ?
380380
new ValueTask<int>(t) :
381381
new ValueTask<int>(synchronousResult);
@@ -412,15 +412,15 @@ public override void Write(byte[] array, int offset, int count)
412412
}
413413
}
414414

415-
public override void Write(ReadOnlySpan<byte> destination)
415+
public override void Write(ReadOnlySpan<byte> buffer)
416416
{
417417
if (GetType() == typeof(FileStream) && !_useAsyncIO)
418418
{
419419
if (_fileHandle.IsClosed)
420420
{
421421
throw Error.GetFileNotOpen();
422422
}
423-
WriteSpan(destination);
423+
WriteSpan(buffer);
424424
}
425425
else
426426
{
@@ -430,7 +430,7 @@ public override void Write(ReadOnlySpan<byte> destination)
430430
// of Write(byte[],int,int) overload. Or if the stream is in async mode, we can't call the
431431
// synchronous WriteSpan, so we similarly call the base Write, which will turn delegate to
432432
// Write(byte[],int,int), which will do the right thing if we're in async mode.
433-
base.Write(destination);
433+
base.Write(buffer);
434434
}
435435
}
436436

@@ -461,14 +461,14 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
461461
return WriteAsyncInternal(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken).AsTask();
462462
}
463463

464-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
464+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
465465
{
466466
if (!_useAsyncIO || GetType() != typeof(FileStream))
467467
{
468468
// If we're not using async I/O, delegate to the base, which will queue a call to Write.
469469
// Or if this isn't a concrete FileStream, a derived type may have overridden WriteAsync(byte[],...),
470470
// which was introduced first, so delegate to the base which will delegate to that.
471-
return base.WriteAsync(source, cancellationToken);
471+
return base.WriteAsync(buffer, cancellationToken);
472472
}
473473

474474
if (cancellationToken.IsCancellationRequested)
@@ -481,7 +481,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
481481
throw Error.GetFileNotOpen();
482482
}
483483

484-
return WriteAsyncInternal(source, cancellationToken);
484+
return WriteAsyncInternal(buffer, cancellationToken);
485485
}
486486

487487
/// <summary>

src/mscorlib/shared/System/IO/MemoryStream.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,27 +367,27 @@ public override int Read(byte[] buffer, int offset, int count)
367367
return n;
368368
}
369369

370-
public override int Read(Span<byte> destination)
370+
public override int Read(Span<byte> buffer)
371371
{
372372
if (GetType() != typeof(MemoryStream))
373373
{
374374
// MemoryStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior
375375
// to this Read(Span<byte>) overload being introduced. In that case, this Read(Span<byte>) overload
376376
// should use the behavior of Read(byte[],int,int) overload.
377-
return base.Read(destination);
377+
return base.Read(buffer);
378378
}
379379

380380
EnsureNotClosed();
381381

382-
int n = Math.Min(_length - _position, destination.Length);
382+
int n = Math.Min(_length - _position, buffer.Length);
383383
if (n <= 0)
384384
return 0;
385385

386386
// TODO https://github.com/dotnet/coreclr/issues/15076:
387387
// Read(byte[], int, int) has an n <= 8 optimization, presumably based
388388
// on benchmarking. Determine if/where such a cut-off is here and add
389389
// an equivalent optimization if necessary.
390-
new Span<byte>(_buffer, _position, n).CopyTo(destination);
390+
new Span<byte>(_buffer, _position, n).CopyTo(buffer);
391391

392392
_position += n;
393393
return n;
@@ -426,7 +426,7 @@ public override Task<int> ReadAsync(Byte[] buffer, int offset, int count, Cancel
426426
}
427427
}
428428

429-
public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
429+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
430430
{
431431
if (cancellationToken.IsCancellationRequested)
432432
{
@@ -448,9 +448,9 @@ public override Task<int> ReadAsync(Byte[] buffer, int offset, int count, Cancel
448448
// something other than an array and this is a MemoryStream-derived type that doesn't override Read(Span<byte>) will
449449
// it then fall back to doing the ArrayPool/copy behavior.
450450
return new ValueTask<int>(
451-
MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> destinationArray) ?
451+
MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> destinationArray) ?
452452
Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) :
453-
Read(destination.Span));
453+
Read(buffer.Span));
454454
}
455455
catch (OperationCanceledException oce)
456456
{
@@ -681,22 +681,22 @@ public override void Write(byte[] buffer, int offset, int count)
681681
_position = i;
682682
}
683683

684-
public override void Write(ReadOnlySpan<byte> source)
684+
public override void Write(ReadOnlySpan<byte> buffer)
685685
{
686686
if (GetType() != typeof(MemoryStream))
687687
{
688688
// MemoryStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior
689689
// to this Write(Span<byte>) overload being introduced. In that case, this Write(Span<byte>) overload
690690
// should use the behavior of Write(byte[],int,int) overload.
691-
base.Write(source);
691+
base.Write(buffer);
692692
return;
693693
}
694694

695695
EnsureNotClosed();
696696
EnsureWriteable();
697697

698698
// Check for overflow
699-
int i = _position + source.Length;
699+
int i = _position + buffer.Length;
700700
if (i < 0)
701701
throw new IOException(SR.IO_StreamTooLong);
702702

@@ -718,7 +718,7 @@ public override void Write(ReadOnlySpan<byte> source)
718718
_length = i;
719719
}
720720

721-
source.CopyTo(new Span<byte>(_buffer, _position, source.Length));
721+
buffer.CopyTo(new Span<byte>(_buffer, _position, buffer.Length));
722722
_position = i;
723723
}
724724

@@ -752,7 +752,7 @@ public override Task WriteAsync(Byte[] buffer, int offset, int count, Cancellati
752752
}
753753
}
754754

755-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
755+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
756756
{
757757
if (cancellationToken.IsCancellationRequested)
758758
{
@@ -763,13 +763,13 @@ public override Task WriteAsync(Byte[] buffer, int offset, int count, Cancellati
763763
{
764764
// See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan<byte>).
765765
// Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency.
766-
if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> sourceArray))
766+
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> sourceArray))
767767
{
768768
Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count);
769769
}
770770
else
771771
{
772-
Write(source.Span);
772+
Write(buffer.Span);
773773
}
774774
return default;
775775
}

src/mscorlib/shared/System/IO/PinnedBufferMemoryStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ internal PinnedBufferMemoryStream(byte[] array)
3838
Initialize(ptr, len, len, FileAccess.Read);
3939
}
4040

41-
public override int Read(Span<byte> destination) => ReadCore(destination);
41+
public override int Read(Span<byte> buffer) => ReadCore(buffer);
4242

43-
public override void Write(ReadOnlySpan<byte> source) => WriteCore(source);
43+
public override void Write(ReadOnlySpan<byte> buffer) => WriteCore(buffer);
4444

4545
~PinnedBufferMemoryStream()
4646
{

src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -379,22 +379,22 @@ public override int Read(byte[] buffer, int offset, int count)
379379
return ReadCore(new Span<byte>(buffer, offset, count));
380380
}
381381

382-
public override int Read(Span<byte> destination)
382+
public override int Read(Span<byte> buffer)
383383
{
384384
if (GetType() == typeof(UnmanagedMemoryStream))
385385
{
386-
return ReadCore(destination);
386+
return ReadCore(buffer);
387387
}
388388
else
389389
{
390390
// UnmanagedMemoryStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior
391391
// to this Read(Span<byte>) overload being introduced. In that case, this Read(Span<byte>) overload
392392
// should use the behavior of Read(byte[],int,int) overload.
393-
return base.Read(destination);
393+
return base.Read(buffer);
394394
}
395395
}
396396

397-
internal int ReadCore(Span<byte> destination)
397+
internal int ReadCore(Span<byte> buffer)
398398
{
399399
EnsureNotClosed();
400400
EnsureReadable();
@@ -403,7 +403,7 @@ internal int ReadCore(Span<byte> destination)
403403
// changes our position after we decide we can read some bytes.
404404
long pos = Interlocked.Read(ref _position);
405405
long len = Interlocked.Read(ref _length);
406-
long n = Math.Min(len - pos, destination.Length);
406+
long n = Math.Min(len - pos, buffer.Length);
407407
if (n <= 0)
408408
{
409409
return 0;
@@ -418,7 +418,7 @@ internal int ReadCore(Span<byte> destination)
418418

419419
unsafe
420420
{
421-
fixed (byte* pBuffer = &MemoryMarshal.GetReference(destination))
421+
fixed (byte* pBuffer = &MemoryMarshal.GetReference(buffer))
422422
{
423423
if (_buffer != null)
424424
{
@@ -486,9 +486,9 @@ public override Task<Int32> ReadAsync(Byte[] buffer, Int32 offset, Int32 count,
486486
/// <summary>
487487
/// Reads bytes from stream and puts them into the buffer
488488
/// </summary>
489-
/// <param name="destination">Buffer to read the bytes to.</param>
489+
/// <param name="buffer">Buffer to read the bytes to.</param>
490490
/// <param name="cancellationToken">Token that can be used to cancel this operation.</param>
491-
public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
491+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
492492
{
493493
if (cancellationToken.IsCancellationRequested)
494494
{
@@ -510,9 +510,9 @@ public override Task<Int32> ReadAsync(Byte[] buffer, Int32 offset, Int32 count,
510510
// something other than an array and this is an UnmanagedMemoryStream-derived type that doesn't override Read(Span<byte>) will
511511
// it then fall back to doing the ArrayPool/copy behavior.
512512
return new ValueTask<int>(
513-
MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> destinationArray) ?
513+
MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> destinationArray) ?
514514
Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) :
515-
Read(destination.Span));
515+
Read(buffer.Span));
516516
}
517517
catch (Exception ex)
518518
{
@@ -659,29 +659,29 @@ public override void Write(byte[] buffer, int offset, int count)
659659
WriteCore(new Span<byte>(buffer, offset, count));
660660
}
661661

662-
public override void Write(ReadOnlySpan<byte> source)
662+
public override void Write(ReadOnlySpan<byte> buffer)
663663
{
664664
if (GetType() == typeof(UnmanagedMemoryStream))
665665
{
666-
WriteCore(source);
666+
WriteCore(buffer);
667667
}
668668
else
669669
{
670670
// UnmanagedMemoryStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior
671671
// to this Write(Span<byte>) overload being introduced. In that case, this Write(Span<byte>) overload
672672
// should use the behavior of Write(byte[],int,int) overload.
673-
base.Write(source);
673+
base.Write(buffer);
674674
}
675675
}
676676

677-
internal unsafe void WriteCore(ReadOnlySpan<byte> source)
677+
internal unsafe void WriteCore(ReadOnlySpan<byte> buffer)
678678
{
679679
EnsureNotClosed();
680680
EnsureWriteable();
681681

682682
long pos = Interlocked.Read(ref _position); // Use a local to avoid a race condition
683683
long len = Interlocked.Read(ref _length);
684-
long n = pos + source.Length;
684+
long n = pos + buffer.Length;
685685
// Check for overflow
686686
if (n < 0)
687687
{
@@ -709,12 +709,12 @@ internal unsafe void WriteCore(ReadOnlySpan<byte> source)
709709
}
710710
}
711711

712-
fixed (byte* pBuffer = &MemoryMarshal.GetReference(source))
712+
fixed (byte* pBuffer = &MemoryMarshal.GetReference(buffer))
713713
{
714714
if (_buffer != null)
715715
{
716716
long bytesLeft = _capacity - pos;
717-
if (bytesLeft < source.Length)
717+
if (bytesLeft < buffer.Length)
718718
{
719719
throw new ArgumentException(SR.Arg_BufferTooSmall);
720720
}
@@ -724,7 +724,7 @@ internal unsafe void WriteCore(ReadOnlySpan<byte> source)
724724
try
725725
{
726726
_buffer.AcquirePointer(ref pointer);
727-
Buffer.Memcpy(pointer + pos + _offset, pBuffer, source.Length);
727+
Buffer.Memcpy(pointer + pos + _offset, pBuffer, buffer.Length);
728728
}
729729
finally
730730
{
@@ -736,7 +736,7 @@ internal unsafe void WriteCore(ReadOnlySpan<byte> source)
736736
}
737737
else
738738
{
739-
Buffer.Memcpy(_mem + pos, pBuffer, source.Length);
739+
Buffer.Memcpy(_mem + pos, pBuffer, buffer.Length);
740740
}
741741
}
742742

@@ -783,7 +783,7 @@ public override Task WriteAsync(Byte[] buffer, Int32 offset, Int32 count, Cancel
783783
/// </summary>
784784
/// <param name="buffer">Buffer that will be written.</param>
785785
/// <param name="cancellationToken">Token that can be used to cancel the operation.</param>
786-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
786+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
787787
{
788788
if (cancellationToken.IsCancellationRequested)
789789
{
@@ -794,13 +794,13 @@ public override Task WriteAsync(Byte[] buffer, Int32 offset, Int32 count, Cancel
794794
{
795795
// See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan<byte>).
796796
// Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency.
797-
if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> sourceArray))
797+
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> sourceArray))
798798
{
799799
Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count);
800800
}
801801
else
802802
{
803-
Write(source.Span);
803+
Write(buffer.Span);
804804
}
805805
return default;
806806
}

0 commit comments

Comments
 (0)