private void EncodeLength(int length, int maxBit = 16)
{
// the range of length is from 0 to 65535, the max bits it may use is 16
int highestBitOnePosition = GetHighestBitOnePos(length, 16);
// get the length bit size without symbol
int bitCountWithoutSymbol = ((highestBitOnePosition < 3) ? 0 : (highestBitOnePosition - 1));
int lengthBitCount = ((highestBitOnePosition < 3) ? 1 : (bitCountWithoutSymbol * 2));
// the last bit of symbol is zero, and other bits are one.
int lengthSymbol = ((1 << bitCountWithoutSymbol) - 2);
// the symbol length should be caculated differently when length < 3
lengthSymbol = ((lengthSymbol > 0) ? lengthSymbol : 0);
// length has the {1+0} form prefix.
int temp = (lengthSymbol << bitCountWithoutSymbol) | (length & ((1 << bitCountWithoutSymbol) - 1));
//write data to stream
EncodeData((uint)temp, lengthBitCount);
}