Пример #1
0
        public BucketBytes AsBytes(BucketBytes appendBytes)
        {
            if (IsEmpty)
            {
                return(appendBytes);
            }
            else if (appendBytes.IsEmpty)
            {
                return(IsEmpty ? BucketBytes.Empty : AsBytes());
            }

            if (_expected > 0)
            {
                _expected = 0;

                if (Length > 0)
                {
                    byte[] bb = (byte[])_bytes !;

                    _bytes = _bytes !.Take(Length);
                    if (Length + appendBytes.Length <= bb.Length)
                    {
                        appendBytes.CopyTo(bb.AsSpan(Length));
                        return(new BucketBytes(bb, 0, Length + appendBytes.Length));
                    }
                }
            }

            return(_bytes !.Concat(appendBytes.ToArray()).ToArray());
        }
Пример #2
0
        public void Append(BucketBytes bytes)
        {
            if (bytes.Length == 0)
            {
                return;
            }

            if (_expected > 0)
            {
                byte[]? bb = _bytes as byte[];

                if (bytes.Length + (bb?.Length ?? 0) <= _expected)
                {
                    _bytes = bb ??= new byte[_expected];

                    bytes.CopyTo(bb.AsSpan(Length));
                    Length += bytes.Length;
                    return;
                }
                else if (Length == 0)
                {
                    _bytes    = bytes !.ToArray();
                    Length    = bytes.Length;
                    _expected = 0;
                    return;
                }
                else
                {
                    _bytes    = _bytes !.Take(Length);
                    _expected = 0;
                    // And fall through
                }
            }

            if (_bytes is null)
            {
                _bytes = bytes.ToArray();
            }
            else
            {
                _bytes = _bytes.Concat(bytes.ToArray());
            }

            Length += bytes.Length;
        }