Call Slice in GetMemory (#7113)
This commit is contained in:
parent
5ba89945ea
commit
7d21ee1a5a
|
|
@ -95,7 +95,7 @@ namespace System.IO.Pipelines
|
|||
|
||||
EnsureCapacity(sizeHint);
|
||||
|
||||
return _currentSegment;
|
||||
return _currentSegment.Slice(_position);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
|
|
@ -350,6 +350,44 @@ namespace System.IO.Pipelines.Tests
|
|||
Assert.Throws<ArgumentOutOfRangeException>(() => Writer.GetSpan(-1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMemorySlicesCorrectly()
|
||||
{
|
||||
var expectedString = "abcdef";
|
||||
var memory = Writer.GetMemory();
|
||||
|
||||
Encoding.ASCII.GetBytes("abc").CopyTo(memory);
|
||||
Writer.Advance(3);
|
||||
memory = Writer.GetMemory();
|
||||
Encoding.ASCII.GetBytes("def").CopyTo(memory);
|
||||
Writer.Advance(3);
|
||||
|
||||
await Writer.FlushAsync();
|
||||
Assert.Equal(expectedString, ReadAsString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSpanSlicesCorrectly()
|
||||
{
|
||||
var expectedString = "abcdef";
|
||||
|
||||
void NonAsyncMethod()
|
||||
{
|
||||
var span = Writer.GetSpan();
|
||||
|
||||
Encoding.ASCII.GetBytes("abc").CopyTo(span);
|
||||
Writer.Advance(3);
|
||||
span = Writer.GetSpan();
|
||||
Encoding.ASCII.GetBytes("def").CopyTo(span);
|
||||
Writer.Advance(3);
|
||||
}
|
||||
|
||||
NonAsyncMethod();
|
||||
|
||||
await Writer.FlushAsync();
|
||||
Assert.Equal(expectedString, ReadAsString());
|
||||
}
|
||||
|
||||
private void WriteStringToStream(string input)
|
||||
{
|
||||
var buffer = Encoding.ASCII.GetBytes(input);
|
||||
|
|
|
|||
Loading…
Reference in New Issue