/// <summary> /// Reads a 1-bit <see cref="bool"/> without advancing the read position. /// </summary> public static bool PeekBool(this IBitBuffer buffer) { if (!buffer.HasEnoughBits(1)) { throw new EndOfMessageException(); } return(NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, 1) > 0); }
/// <summary> /// Reads the specified number of bits into a <see cref="byte"/> without advancing the read position. /// </summary> public static byte PeekByte(this IBitBuffer buffer, int bitCount) { if (!buffer.HasEnoughBits(bitCount)) { throw new EndOfMessageException(); } return(NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, bitCount)); }
public static sbyte PeekSByte(this IBitBuffer buffer) { if (!buffer.HasEnoughBits(8)) { throw new EndOfMessageException(); } return((sbyte)NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, 8)); }
/// <summary> /// Tries to read the specified number of bits without advancing the read position. /// </summary> /// <param name="destination">The destination span.</param> /// <param name="bitCount">The number of bits to read.</param> public static bool TryPeekBits(this IBitBuffer buffer, Span <byte> destination, int bitCount) { if (!buffer.HasEnoughBits(bitCount)) { return(false); } NetBitWriter.CopyBits(buffer.GetBuffer(), buffer.BitPosition, bitCount, destination, 0); return(true); }
/// <summary> /// Reads 1 to 8 bits into a <see cref="byte"/>. /// </summary> public static bool ReadByte(this IBitBuffer buffer, int bitCount, out byte result) { if (!buffer.HasEnoughBits(bitCount)) { result = default; return(false); } result = NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, bitCount); buffer.BitPosition += bitCount; return(true); }
/// <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); }