protected internal override ReadOnlySequence <byte> _GetSequence(int index, int count) { if (0u >= (uint)count) { return(ReadOnlySequence <byte> .Empty); } var buffers = ThreadLocalList <ReadOnlyMemory <byte> > .NewInstance(_componentCount); try { int i = ToComponentIndex0(index); while (count > 0) { ComponentEntry c = _components[i]; IByteBuffer s = c.Buffer; int localLength = Math.Min(count, c.EndOffset - index); switch (s.IoBufferCount) { case 0: ThrowHelper.ThrowNotSupportedException(); break; case 1: if (s.IsSingleIoBuffer) { buffers.Add(s.GetReadableMemory(c.Idx(index), localLength)); } else { var sequence0 = s.GetSequence(c.Idx(index), localLength); foreach (var memory in sequence0) { buffers.Add(memory); } } break; default: var sequence = s.GetSequence(c.Idx(index), localLength); foreach (var memory in sequence) { buffers.Add(memory); } break; } index += localLength; count -= localLength; i++; } return(ReadOnlyBufferSegment.Create(buffers)); } finally { buffers.Return(); } }
protected internal override Span <byte> _GetSpan(int index, int count) { if (0u >= (uint)count) { return(Span <byte> .Empty); } switch (_componentCount) { case 0: return(Span <byte> .Empty); case 1: ComponentEntry c = _components[0]; return(c.Buffer.GetSpan(index, count)); default: var idx = ToComponentIndex0(index); if (idx == ToComponentIndex0(index + count - 1)) { ComponentEntry c1 = _components[idx]; return(c1.Buffer.GetSpan(c1.Idx(index), count)); } throw ThrowHelper.GetNotSupportedException(); } }
protected internal override ReadOnlySpan <byte> _GetReadableSpan(int index, int count) { if (0u >= (uint)count) { return(ReadOnlySpan <byte> .Empty); } switch (_componentCount) { case 0: return(ReadOnlySpan <byte> .Empty); case 1: //ComponentEntry c = _components[0]; //return c.Buffer.GetReadableSpan(index, count); ComponentEntry c = _components[0]; IByteBuffer buf = c.Buffer; if (buf.IsSingleIoBuffer) { return(buf.GetReadableSpan(c.Idx(index), count)); } break; } var merged = new Memory <byte>(new byte[count]); var buffers = GetSequence(index, count); int offset = 0; foreach (var buf in buffers) { Debug.Assert(merged.Length - offset >= buf.Length); buf.CopyTo(merged.Slice(offset)); offset += buf.Length; } return(merged.Span); }
protected internal override void _GetBytes(int index, Memory <byte> destination, int length) { CheckIndex(index, length); if (0u >= (uint)length) { return; } var srcIndex = 0; int i = ToComponentIndex0(index); while (length > 0) { ComponentEntry c = _components[i]; int localLength = Math.Min(length, c.EndOffset - index); _ = c.Buffer.GetBytes(c.Idx(index), destination.Slice(srcIndex, localLength)); index += localLength; srcIndex += localLength; length -= localLength; i++; } }