private static void writeHuffmanTreeToStream(Stream fileStream)
        {
            BitBuffer bitBuffer = new BitBuffer(1, fileStream);

            fileStream.WriteByte((byte)frequencyTable.TableSize);

            for (int i = 0; i < frequencyTable.MaxTableSize; i++)
            {
                if (frequencyTable.Hits[i] != 0)
                {
                    fileStream.WriteByte((byte)i);
                    fileStream.WriteByte((byte)frequencyTable.Codewords[i].Length);
                }
            }

            for (int i = 0; i < frequencyTable.MaxTableSize; i++)
            {
                if (frequencyTable.Hits[i] != 0)
                {
                    bitBuffer.AddToBuffer(frequencyTable.Codewords[i]);
                }
            }

            bitBuffer.Flush();
        }
        public static void EncodeStream(Stream inStream, Stream outStream)
        {
            int i = 0; // counter
            int newFreq;
            // the list of all Huffman Tree nodes
            List<TreeNode<int, byte>> huffmanTree = new List<TreeNode<int, byte>>();
            // the root of Huffman Tree
            TreeNode<int, byte> huffmanTreeRoot = new TreeNode<int, byte>(0, 0);

            frequencyTable = new FrequencyTable();
            /*frequencyTable = new FrequencyEntry[256 ];
            for (i = 0; i < frequencyTable.Length; ++i)
                frequencyTable[i] = new FrequencyEntry();*/

            calculateFrequency(inStream);

            i = 0;
            // construct a forest
            for (i = 0; i < frequencyTable.MaxTableSize; i++)
            {
                if (frequencyTable.Hits[i] != 0)
                    // add a new node to Huffman Tree, where character hits is the key
                    // and the character itself is the value
                    huffmanTree.Add(new TreeNode<int, byte>(frequencyTable.Hits[i], (byte)i));
            }

            // link the nodes
            while (huffmanTree.Count > 1)
            {
                huffmanTree.Sort(compareTreeNodes);

                newFreq = huffmanTree[0].Key + huffmanTree[1].Key;

                TreeNode<int, byte> node = new TreeNode<int, byte>(newFreq, 0);
                node.Left = huffmanTree[0];
                node.Right = huffmanTree[1];

                node.Left.Parent = node;
                node.Right.Parent = node;

                huffmanTree.RemoveAt(0);
                huffmanTree.RemoveAt(0);

                huffmanTree.Add(node);

                huffmanTreeRoot = node;
            }

            //calculate codewords
            traverseTree(huffmanTreeRoot, "");

            //wrtie table
            writeHuffmanTreeToStream(outStream);

            //
            outStream.WriteByte((byte)(inStream.Length % 256));

            //wrtie encoded stream
            //string buffer = "", bits = "";
            //string pooledBuffer = "";
            BitBuffer bitBuffer = new BitBuffer(1, outStream);
            int nextChar = 0;

            inStream.Seek(0, SeekOrigin.Begin);
            nextChar = inStream.ReadByte();
            while (nextChar != -1)
            {
                bitBuffer.AddToBuffer(frequencyTable.Codewords[nextChar]);
                //pooledBuffer += frequencyTable.Codewords[nextChar];

                /*if (buffer.Length >= 8)
                {
                    bits = buffer.Substring(0, 8);
                    buffer = buffer.Substring(8);
                    outStream.WriteByte(encodeBitString(bits));
                }*/
                nextChar = inStream.ReadByte();
            }

            bitBuffer.Flush();
            //if (buffer.Length > 0)
            //  outStream.WriteByte(encodeBitString(buffer));
        }