Microsoft.Protocols.TestTools.StackSdk.Compression.Mppc.Compressor.EncodeData C# (CSharp) Method

EncodeData() private method

encode data of variable length
private EncodeData ( uint data, int dataBitsCount ) : void
data uint the data
dataBitsCount int how many bits the data used
return void
        private void EncodeData(uint data, int dataBitsCount)
        {
            if (dataBitsCount <= 0)
            {
                throw new ArgumentException("Can't encode data whose bit length is less than or equal to 0",
                    "dataBitsCount");
            }

            byte temp = 0;

            // when the data's bit size is larger than 8 bits
            // we will encode the data every 8 bits
            while (dataBitsCount >= oneByteBitsCount)
            {
                remain = (remain << oneByteBitsCount) | (data >> (dataBitsCount - oneByteBitsCount));
                dataBitsCount = dataBitsCount - oneByteBitsCount;
                // data & the value whose form is like 00000111111
                data = data & (uint)((1 << dataBitsCount) - 1);
                temp = (byte)(remain >> remainBitsCount);
                outputStream.WriteByte(temp);
                // remain & the value whose form is like 00000111111
                // this action will eliminate the bits which has been written to
                // output stream
                remain = remain & (uint)((1 << remainBitsCount) - 1);
            }

            // if there is un-encoded data, we write it to
            // the "remain".
            if (dataBitsCount > 0)
            {
                remain = (remain << dataBitsCount) | data;
                remainBitsCount += dataBitsCount;
            }

            // if the length of remain is larger than 8-bits after upper
            // operation, we will write it to ouputstream one by one.
            while (remainBitsCount >= oneByteBitsCount)
            {
                remainBitsCount -= oneByteBitsCount;
                temp = (byte)(remain >> (remainBitsCount));
                outputStream.WriteByte(temp);
                // this action will eliminate the bits which has been written to
                // output stream from remain
                remain = remain & (uint)((1 << remainBitsCount) - 1);
            }
        }