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()); }
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; }
public static ValueTask WriteAsync(this Stream stream, BucketBytes bucketBytes, CancellationToken cancellationToken = default) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } #if !NETFRAMEWORK return(stream.WriteAsync(bucketBytes.Memory, cancellationToken)); #else var(q, r) = bucketBytes; if (q is not null) { return(new ValueTask(stream.WriteAsync(q, r, bucketBytes.Length, cancellationToken))); } else { q = bucketBytes.ToArray(); return(new ValueTask(stream.WriteAsync(q, 0, bucketBytes.Length, cancellationToken))); } #endif }