/// <summary> /// Reads the specified number of bits, clamped between one and <paramref name="maxBitCount"/>, into the given buffer. /// </summary> /// <param name="destination">The destination span.</param> /// <param name="bitCount">The number of bits to read.</param> /// <param name="maxBitCount">The maximum amount of bits to read.</param> public static bool TryReadBits(this IBitBuffer buffer, Span <byte> destination, int bitCount, int maxBitCount) { Debug.Assert(bitCount >= 1); Debug.Assert(bitCount <= maxBitCount); return(buffer.TryReadBits(destination, bitCount)); }
/// <summary> /// Reads the specified number of bits into the given buffer. /// </summary> /// <param name="destination">The destination span.</param> /// <param name="bitCount">The number of bits to read.</param> /// <exception cref="EndOfMessageException"></exception> public static void ReadBits(this IBitBuffer buffer, Span <byte> destination, int bitCount) { if (!buffer.TryReadBits(destination, bitCount)) { throw new EndOfMessageException(); } }
public static bool ReadUInt16(this IBitBuffer buffer, int bitCount, out ushort result) { Span <byte> tmp = stackalloc byte[sizeof(ushort)]; tmp.Clear(); if (!buffer.TryReadBits(tmp, bitCount, tmp.Length * 8)) { result = default; return(false); } result = BinaryPrimitives.ReadUInt16LittleEndian(tmp); return(true); }
/// <summary> /// Tries to read bytes into the given span. /// </summary> /// <param name="destination">The destination span.</param> public static bool TryRead(this IBitBuffer buffer, Span <byte> destination) { if (!buffer.IsByteAligned()) { return(buffer.TryReadBits(destination, destination.Length * 8)); } if (!buffer.HasEnoughBits(destination.Length)) { return(false); } buffer.GetBuffer().AsSpan(buffer.BytePosition, destination.Length).CopyTo(destination); buffer.BitPosition += destination.Length * 8; return(true); }