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

Commit 61fdee4

Browse files
Anipikstephentoub
authored andcommitted
Rename new Stream.Read/Write{Async} Span/Memory arguments to buffer (#28429)
* updated implementation * http content fixed
1 parent 68f0e3b commit 61fdee4

File tree

9 files changed

+62
-62
lines changed

9 files changed

+62
-62
lines changed

src/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public override int Read(byte[] buffer, int offset, int count)
1818
return Read(new Span<byte>(buffer, offset, count));
1919
}
2020

21-
public override int Read(Span<byte> destination)
21+
public override int Read(Span<byte> buffer)
2222
{
2323
if (_mode != CompressionMode.Decompress)
2424
throw new InvalidOperationException(SR.BrotliStream_Compress_UnsupportedOperation);
@@ -27,7 +27,7 @@ public override int Read(Span<byte> destination)
2727
Span<byte> source = Span<byte>.Empty;
2828
OperationStatus lastResult = OperationStatus.DestinationTooSmall;
2929
// We want to continue calling Decompress until we're either out of space for output or until Decompress indicates it is finished.
30-
while (destination.Length > 0 && lastResult != OperationStatus.Done)
30+
while (buffer.Length > 0 && lastResult != OperationStatus.Done)
3131
{
3232
int bytesConsumed = 0;
3333
int bytesWritten = 0;
@@ -53,15 +53,15 @@ public override int Read(Span<byte> destination)
5353
source = new Span<byte>(_buffer, 0, readBytes);
5454
}
5555

56-
lastResult = _decoder.Decompress(source, destination, out bytesConsumed, out bytesWritten);
56+
lastResult = _decoder.Decompress(source, buffer, out bytesConsumed, out bytesWritten);
5757
if (lastResult == OperationStatus.InvalidData)
5858
throw new InvalidOperationException(SR.BrotliStream_Decompress_InvalidData);
5959
if (bytesConsumed > 0)
6060
source = source.Slice(bytesConsumed);
6161
if (bytesWritten > 0)
6262
{
6363
totalWritten += bytesWritten;
64-
destination = destination.Slice(bytesWritten);
64+
buffer = buffer.Slice(bytesWritten);
6565
}
6666
}
6767

@@ -80,7 +80,7 @@ public override Task<int> ReadAsync(byte[] array, int offset, int count, Cancell
8080
return ReadAsync(new Memory<byte>(array, offset, count), cancellationToken).AsTask();
8181
}
8282

83-
public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
83+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
8484
{
8585
if (_mode != CompressionMode.Decompress)
8686
throw new InvalidOperationException(SR.BrotliStream_Compress_UnsupportedOperation);
@@ -91,10 +91,10 @@ public override Task<int> ReadAsync(byte[] array, int offset, int count, Cancell
9191
{
9292
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
9393
}
94-
return FinishReadAsyncMemory(destination, cancellationToken);
94+
return FinishReadAsyncMemory(buffer, cancellationToken);
9595
}
9696

97-
private async ValueTask<int> FinishReadAsyncMemory(Memory<byte> destination, CancellationToken cancellationToken)
97+
private async ValueTask<int> FinishReadAsyncMemory(Memory<byte> buffer, CancellationToken cancellationToken)
9898
{
9999
AsyncOperationStarting();
100100
try
@@ -103,7 +103,7 @@ private async ValueTask<int> FinishReadAsyncMemory(Memory<byte> destination, Can
103103
Memory<byte> source = Memory<byte>.Empty;
104104
OperationStatus lastResult = OperationStatus.DestinationTooSmall;
105105
// We want to continue calling Decompress until we're either out of space for output or until Decompress indicates it is finished.
106-
while (destination.Length > 0 && lastResult != OperationStatus.Done)
106+
while (buffer.Length > 0 && lastResult != OperationStatus.Done)
107107
{
108108

109109
int bytesConsumed = 0;
@@ -131,15 +131,15 @@ private async ValueTask<int> FinishReadAsyncMemory(Memory<byte> destination, Can
131131
}
132132

133133
cancellationToken.ThrowIfCancellationRequested();
134-
lastResult = _decoder.Decompress(source, destination, out bytesConsumed, out bytesWritten);
134+
lastResult = _decoder.Decompress(source, buffer, out bytesConsumed, out bytesWritten);
135135
if (lastResult == OperationStatus.InvalidData)
136136
throw new InvalidOperationException(SR.BrotliStream_Decompress_InvalidData);
137137
if (bytesConsumed > 0)
138138
source = source.Slice(bytesConsumed);
139139
if (bytesWritten > 0)
140140
{
141141
totalWritten += bytesWritten;
142-
destination = destination.Slice(bytesWritten);
142+
buffer = buffer.Slice(bytesWritten);
143143
}
144144
}
145145

src/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public override void Write(byte[] buffer, int offset, int count)
2424
WriteCore(new ReadOnlySpan<byte>(buffer, offset, count));
2525
}
2626

27-
public override void Write(ReadOnlySpan<byte> source)
27+
public override void Write(ReadOnlySpan<byte> buffer)
2828
{
29-
WriteCore(source);
29+
WriteCore(buffer);
3030
}
3131

32-
internal void WriteCore(ReadOnlySpan<byte> source, bool isFinalBlock = false)
32+
internal void WriteCore(ReadOnlySpan<byte> buffer, bool isFinalBlock = false)
3333
{
3434
if (_mode != CompressionMode.Compress)
3535
throw new InvalidOperationException(SR.BrotliStream_Decompress_UnsupportedOperation);
@@ -41,13 +41,13 @@ internal void WriteCore(ReadOnlySpan<byte> source, bool isFinalBlock = false)
4141
{
4242
int bytesConsumed = 0;
4343
int bytesWritten = 0;
44-
lastResult = _encoder.Compress(source, output, out bytesConsumed, out bytesWritten, isFinalBlock);
44+
lastResult = _encoder.Compress(buffer, output, out bytesConsumed, out bytesWritten, isFinalBlock);
4545
if (lastResult == OperationStatus.InvalidData)
4646
throw new InvalidOperationException(SR.BrotliStream_Compress_InvalidData);
4747
if (bytesWritten > 0)
4848
_stream.Write(output.Slice(0, bytesWritten));
4949
if (bytesConsumed > 0)
50-
source = source.Slice(bytesConsumed);
50+
buffer = buffer.Slice(bytesConsumed);
5151
}
5252
}
5353

@@ -63,7 +63,7 @@ public override Task WriteAsync(byte[] array, int offset, int count, Cancellatio
6363
return WriteAsync(new ReadOnlyMemory<byte>(array, offset, count), cancellationToken).AsTask();
6464
}
6565

66-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
66+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
6767
{
6868
if (_mode != CompressionMode.Compress)
6969
throw new InvalidOperationException(SR.BrotliStream_Decompress_UnsupportedOperation);
@@ -72,10 +72,10 @@ public override Task WriteAsync(byte[] array, int offset, int count, Cancellatio
7272

7373
return new ValueTask(cancellationToken.IsCancellationRequested ?
7474
Task.FromCanceled<int>(cancellationToken) :
75-
WriteAsyncMemoryCore(source, cancellationToken));
75+
WriteAsyncMemoryCore(buffer, cancellationToken));
7676
}
7777

78-
private async Task WriteAsyncMemoryCore(ReadOnlyMemory<byte> source, CancellationToken cancellationToken)
78+
private async Task WriteAsyncMemoryCore(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
7979
{
8080
AsyncOperationStarting();
8181
try
@@ -86,11 +86,11 @@ private async Task WriteAsyncMemoryCore(ReadOnlyMemory<byte> source, Cancellatio
8686
Memory<byte> output = new Memory<byte>(_buffer);
8787
int bytesConsumed = 0;
8888
int bytesWritten = 0;
89-
lastResult = _encoder.Compress(source, output, out bytesConsumed, out bytesWritten, isFinalBlock: false);
89+
lastResult = _encoder.Compress(buffer, output, out bytesConsumed, out bytesWritten, isFinalBlock: false);
9090
if (lastResult == OperationStatus.InvalidData)
9191
throw new InvalidOperationException(SR.BrotliStream_Compress_InvalidData);
9292
if (bytesConsumed > 0)
93-
source = source.Slice(bytesConsumed);
93+
buffer = buffer.Slice(bytesConsumed);
9494
if (bytesWritten > 0)
9595
await _stream.WriteAsync(new ReadOnlyMemory<byte>(_buffer, 0, bytesWritten), cancellationToken).ConfigureAwait(false);
9696
}

src/System.IO.Compression/src/System/IO/Compression/PositionPreservingWriteOnlyStreamWrapper.netcoreapp.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ namespace System.IO.Compression
99
{
1010
internal sealed partial class PositionPreservingWriteOnlyStreamWrapper : Stream
1111
{
12-
public override void Write(ReadOnlySpan<byte> source)
12+
public override void Write(ReadOnlySpan<byte> buffer)
1313
{
14-
_position += source.Length;
15-
_stream.Write(source);
14+
_position += buffer.Length;
15+
_stream.Write(buffer);
1616
}
1717

18-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken))
18+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken))
1919
{
20-
_position += source.Length;
21-
return _stream.WriteAsync(source, cancellationToken);
20+
_position += buffer.Length;
21+
return _stream.WriteAsync(buffer, cancellationToken);
2222
}
2323
}
2424
}

src/System.Net.Http/src/System/Net/Http/CurlHandler/CurlHandler.CurlResponseMessage.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
253253
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
254254
}
255255

256-
public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default)
256+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
257257
{
258258
CheckDisposed();
259259

260-
EventSourceTrace("Buffer: {0}", destination.Length);
260+
EventSourceTrace("Buffer: {0}", buffer.Length);
261261

262262
// Check for cancellation
263263
if (cancellationToken.IsCancellationRequested)
@@ -290,16 +290,16 @@ public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationT
290290

291291
// Quick check for if no data was actually requested. We do this after the check
292292
// for errors so that we can still fail the read and transfer the exception if we should.
293-
if (destination.Length == 0)
293+
if (buffer.Length == 0)
294294
{
295295
return new ValueTask<int>(0);
296296
}
297297

298298
// If there's any data left over from a previous call, grab as much as we can.
299299
if (_remainingDataCount > 0)
300300
{
301-
int bytesToCopy = Math.Min(destination.Length, _remainingDataCount);
302-
new Span<byte>(_remainingData, _remainingDataOffset, bytesToCopy).CopyTo(destination.Span);
301+
int bytesToCopy = Math.Min(buffer.Length, _remainingDataCount);
302+
new Span<byte>(_remainingData, _remainingDataOffset, bytesToCopy).CopyTo(buffer.Span);
303303

304304
_remainingDataOffset += bytesToCopy;
305305
_remainingDataCount -= bytesToCopy;
@@ -332,7 +332,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationT
332332
// the cancellation token. Dispose on the registration won't return until the action
333333
// associated with the registration has completed, but if that action is currently
334334
// executing and is blocked on the lock that's held while calling Dispose... deadlock.
335-
var crs = new CancelableReadState(destination, this, cancellationToken);
335+
var crs = new CancelableReadState(buffer, this, cancellationToken);
336336
crs._registration = cancellationToken.Register(s1 =>
337337
{
338338
((CancelableReadState)s1)._stream.EventSourceTrace("Cancellation invoked. Queueing work item to cancel read state");
@@ -356,7 +356,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationT
356356
else
357357
{
358358
// The token isn't cancelable. Just create a normal read state.
359-
_pendingReadRequest = new ReadState(destination);
359+
_pendingReadRequest = new ReadState(buffer);
360360
}
361361

362362
_easy._associatedMultiAgent.RequestUnpause(_easy);

src/System.Net.Http/src/System/Net/Http/HttpContent.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,10 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
731731
return base.WriteAsync(buffer, offset, count, cancellationToken);
732732
}
733733

734-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken)
734+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
735735
{
736-
CheckSize(source.Length);
737-
return base.WriteAsync(source, cancellationToken);
736+
CheckSize(buffer.Length);
737+
return base.WriteAsync(buffer, cancellationToken);
738738
}
739739

740740
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
@@ -869,11 +869,11 @@ public override void Write(byte[] buffer, int offset, int count)
869869
_length += count;
870870
}
871871

872-
public override void Write(ReadOnlySpan<byte> source)
872+
public override void Write(ReadOnlySpan<byte> buffer)
873873
{
874-
EnsureCapacity(_length + source.Length);
875-
source.CopyTo(new Span<byte>(_buffer, _length, source.Length));
876-
_length += source.Length;
874+
EnsureCapacity(_length + buffer.Length);
875+
buffer.CopyTo(new Span<byte>(_buffer, _length, buffer.Length));
876+
_length += buffer.Length;
877877
}
878878

879879
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
@@ -882,9 +882,9 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
882882
return Task.CompletedTask;
883883
}
884884

885-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default)
885+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
886886
{
887-
Write(source.Span);
887+
Write(buffer.Span);
888888
return default;
889889
}
890890

src/System.Net.Http/src/System/Net/Http/MultipartContent.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ public override int Read(byte[] buffer, int offset, int count)
417417
}
418418
}
419419

420-
public override int Read(Span<byte> destination)
420+
public override int Read(Span<byte> buffer)
421421
{
422-
if (destination.Length == 0)
422+
if (buffer.Length == 0)
423423
{
424424
return 0;
425425
}
@@ -428,7 +428,7 @@ public override int Read(Span<byte> destination)
428428
{
429429
if (_current != null)
430430
{
431-
int bytesRead = _current.Read(destination);
431+
int bytesRead = _current.Read(buffer);
432432
if (bytesRead != 0)
433433
{
434434
_position += bytesRead;
@@ -453,18 +453,18 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
453453
return ReadAsyncPrivate(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
454454
}
455455

456-
public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default) =>
457-
ReadAsyncPrivate(destination, cancellationToken);
456+
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) =>
457+
ReadAsyncPrivate(buffer, cancellationToken);
458458

459459
public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) =>
460460
TaskToApm.Begin(ReadAsync(array, offset, count, CancellationToken.None), asyncCallback, asyncState);
461461

462462
public override int EndRead(IAsyncResult asyncResult) =>
463463
TaskToApm.End<int>(asyncResult);
464464

465-
public async ValueTask<int> ReadAsyncPrivate(Memory<byte> destination, CancellationToken cancellationToken)
465+
public async ValueTask<int> ReadAsyncPrivate(Memory<byte> buffer, CancellationToken cancellationToken)
466466
{
467-
if (destination.Length == 0)
467+
if (buffer.Length == 0)
468468
{
469469
return 0;
470470
}
@@ -473,7 +473,7 @@ public async ValueTask<int> ReadAsyncPrivate(Memory<byte> destination, Cancellat
473473
{
474474
if (_current != null)
475475
{
476-
int bytesRead = await _current.ReadAsync(destination, cancellationToken).ConfigureAwait(false);
476+
int bytesRead = await _current.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
477477
if (bytesRead != 0)
478478
{
479479
_position += bytesRead;
@@ -581,9 +581,9 @@ private static void ValidateReadArgs(byte[] buffer, int offset, int count)
581581
public override void Flush() { }
582582
public override void SetLength(long value) { throw new NotSupportedException(); }
583583
public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
584-
public override void Write(ReadOnlySpan<byte> source) { throw new NotSupportedException(); }
584+
public override void Write(ReadOnlySpan<byte> buffer) { throw new NotSupportedException(); }
585585
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { throw new NotSupportedException(); }
586-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default) { throw new NotSupportedException(); }
586+
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) { throw new NotSupportedException(); }
587587
}
588588
#endregion Serialization
589589
}

0 commit comments

Comments
 (0)