Exemple #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);
        }
Exemple #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));
        }
Exemple #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));
        }
Exemple #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);
        }
        /// <summary>
        /// Writes a certain amount of bits from a span.
        /// </summary>
        /// <param name="bitOffset">The offset in bits into <paramref name="source"/>.</param>
        /// <param name="bitCount">The amount of bits to copy from <paramref name="source"/>.</param>
        public static void Write(this IBitBuffer buffer, ReadOnlySpan <byte> source, int bitOffset, int bitCount)
        {
            if (source.IsEmpty)
            {
                return;
            }

            buffer.EnsureEnoughBitCapacity(bitCount);
            NetBitWriter.CopyBits(source, bitOffset, bitCount, buffer.GetBuffer(), buffer.BitPosition);
            buffer.IncrementBitPosition(bitCount);
        }
        /// <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);
        }
Exemple #7
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);
        }
Exemple #8
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);
        }
 /// <summary>
 /// Write a <see cref="byte"/>.
 /// </summary>
 public static void Write(this IBitBuffer buffer, byte value)
 {
     buffer.EnsureEnoughBitCapacity(8);
     NetBitWriter.WriteByte(value, 8, buffer.GetBuffer(), buffer.BitPosition);
     buffer.IncrementBitPosition(8);
 }
 /// <summary>
 /// Writes a <see cref="bool"/> value using 1 bit.
 /// </summary>
 public static void WriteBit(this IBitBuffer buffer, bool value)
 {
     buffer.EnsureEnoughBitCapacity(1);
     NetBitWriter.WriteByte(value ? (byte)1 : (byte)0, 1, buffer.GetBuffer(), buffer.BitPosition);
     buffer.IncrementBitPosition(1);
 }
        /// <summary>
        /// Append bits from a <see cref="IBitBuffer"/> to this buffer.
        /// The position and length of the source buffer are considered.
        /// </summary>
        public static void Write(this IBitBuffer buffer, IBitBuffer sourceBuffer)
        {
            int bitPosition = sourceBuffer.BitPosition;

            buffer.Write(sourceBuffer.GetBuffer(), bitPosition, sourceBuffer.BitLength - bitPosition);
        }
 /// <summary>
 /// Writes a <see cref="byte"/> using 1 to 8 bits.
 /// </summary>
 public static void Write(this IBitBuffer buffer, byte source, int bitCount)
 {
     buffer.EnsureEnoughBitCapacity(bitCount, maxBitCount: 8);
     NetBitWriter.WriteByte(source, bitCount, buffer.GetBuffer(), buffer.BitPosition);
     buffer.IncrementBitPosition(bitCount);
 }