コード例 #1
0
        /// <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);
        }
コード例 #2
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));
        }
コード例 #3
0
        public static sbyte PeekSByte(this IBitBuffer buffer)
        {
            if (!buffer.HasEnoughBits(8))
            {
                throw new EndOfMessageException();
            }

            return((sbyte)NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, 8));
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
        /// <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);
        }