本文整理汇总了C#中Span.Read方法的典型用法代码示例。如果您正苦于以下问题:C# Span.Read方法的具体用法?C# Span.Read怎么用?C# Span.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Span
的用法示例。
在下文中一共展示了Span.Read方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadTWorksAgainstMultipleBuffers
public async Task ReadTWorksAgainstMultipleBuffers()
{
using (var factory = new PipelineFactory())
{
var readerWriter = factory.Create();
var output = readerWriter.Alloc();
// we're going to try to force 3 buffers for 8 bytes
output.Write(new byte[] { 0, 1, 2 });
output.Ensure(4031);
output.Write(new byte[] { 3, 4, 5 });
output.Ensure(4031);
output.Write(new byte[] { 6, 7, 9 });
var readable = output.AsReadableBuffer();
Assert.Equal(9, readable.Length);
int spanCount = 0;
foreach (var _ in readable)
{
spanCount++;
}
Assert.Equal(3, spanCount);
byte[] local = new byte[9];
readable.CopyTo(local);
var span = new Span<byte>(local);
Assert.Equal(span.Read<byte>(), readable.ReadLittleEndian<byte>());
Assert.Equal(span.Read<sbyte>(), readable.ReadLittleEndian<sbyte>());
Assert.Equal(span.Read<short>(), readable.ReadLittleEndian<short>());
Assert.Equal(span.Read<ushort>(), readable.ReadLittleEndian<ushort>());
Assert.Equal(span.Read<int>(), readable.ReadLittleEndian<int>());
Assert.Equal(span.Read<uint>(), readable.ReadLittleEndian<uint>());
Assert.Equal(span.Read<long>(), readable.ReadLittleEndian<long>());
Assert.Equal(span.Read<ulong>(), readable.ReadLittleEndian<ulong>());
Assert.Equal(span.Read<float>(), readable.ReadLittleEndian<float>());
Assert.Equal(span.Read<double>(), readable.ReadLittleEndian<double>());
await output.FlushAsync();
}
}
示例2: ReadTWorksAgainstSimpleBuffers
public async Task ReadTWorksAgainstSimpleBuffers()
{
byte[] chunk = { 0, 1, 2, 3, 4, 5, 6, 7 };
var span = new Span<byte>(chunk);
using (var factory = new PipelineFactory())
{
var readerWriter = factory.Create();
var output = readerWriter.Alloc();
output.Write(span);
var readable = output.AsReadableBuffer();
Assert.True(readable.IsSingleSpan);
Assert.Equal(span.Read<byte>(), readable.ReadLittleEndian<byte>());
Assert.Equal(span.Read<sbyte>(), readable.ReadLittleEndian<sbyte>());
Assert.Equal(span.Read<short>(), readable.ReadLittleEndian<short>());
Assert.Equal(span.Read<ushort>(), readable.ReadLittleEndian<ushort>());
Assert.Equal(span.Read<int>(), readable.ReadLittleEndian<int>());
Assert.Equal(span.Read<uint>(), readable.ReadLittleEndian<uint>());
Assert.Equal(span.Read<long>(), readable.ReadLittleEndian<long>());
Assert.Equal(span.Read<ulong>(), readable.ReadLittleEndian<ulong>());
Assert.Equal(span.Read<float>(), readable.ReadLittleEndian<float>());
Assert.Equal(span.Read<double>(), readable.ReadLittleEndian<double>());
await output.FlushAsync();
}
}