/// <summary> /// Writes bytes from a span. /// </summary> public static void Write(this IBitBuffer buffer, ReadOnlySpan <byte> source) { if (!buffer.IsByteAligned()) { buffer.Write(source, 0, source.Length * 8); return; } buffer.EnsureEnoughBitCapacity(source.Length * 8); source.CopyTo(buffer.GetBuffer().AsSpan(buffer.BytePosition)); buffer.IncrementBitPosition(source.Length * 8); }
/// <summary> /// Reads bytes into the given span. /// </summary> /// <param name="destination">The destination span.</param> public static int StreamRead(this IBitBuffer buffer, Span <byte> destination) { if (buffer.IsByteAligned()) { int remainingBytes = Math.Min(buffer.ByteLength - buffer.BytePosition, destination.Length); buffer.Read(destination.Slice(0, remainingBytes)); return(remainingBytes); } else { int remainingBits = Math.Min(buffer.BitLength - buffer.BitPosition, destination.Length * 8); buffer.ReadBits(destination, remainingBits); return(NetUtility.DivBy8(remainingBits)); } }
/// <summary> /// Tries to read the specified number of bytes without advancing the read position. /// </summary> /// <param name="destination">The destination span.</param> public static bool TryPeek(this IBitBuffer buffer, Span <byte> destination) { if (!buffer.IsByteAligned()) { return(buffer.TryPeekBits(destination, destination.Length * 8)); } if (!buffer.HasEnoughBits(destination.Length)) { return(false); } buffer.GetBuffer().AsSpan(buffer.BytePosition, destination.Length).CopyTo(destination); return(true); }