Ejemplo n.º 1
0
        public void DoWork()
        {
            while (true)
            {
                ewhStart.WaitOne();

                if (m_Command == 0) // encode
                {
                    Huffman.Encode(ref m_aIn, out m_aOut);
                }
                else if (m_Command == 1) // decode
                {
                    Huffman.Decode(ref m_aIn, out m_aOut);
                }
                else
                {
                    ewhStop.Set();
                    break;
                }

                ewhStop.Set();
            }
        }
Ejemplo n.º 2
0
        public void Encode(ref byte[] pBufIn, out byte[] pBufOut)
        {
            if (pBufIn.Length < 1024 * 1024)
            {
                byte[] pOut = null;
                Huffman.Encode(ref pBufIn, out pOut);

                pBufOut    = new byte[pOut.Length + 1];
                pBufOut[0] = 0;
                pOut.CopyTo(pBufOut, 1);

                return;
            }

            // === start threads ===
            Start();
            // =====================

            pBufOut = null;

            int i, j;
            int PieSize    = pBufIn.Length / m_ProcessCount;
            int StartIndex = 0;
            int EndIndex   = 0;

            byte[][] aIn = new byte[m_ProcessCount][];

            for (i = 0; i < m_ProcessCount; i++)
            {
                EndIndex = StartIndex + PieSize;

                if (EndIndex >= pBufIn.Length)
                {
                    EndIndex = pBufIn.Length - 1;
                }

                aIn[i] = new byte[EndIndex - StartIndex + 1];

                for (j = 0; j < aIn[i].Length; j++)
                {
                    aIn[i][j] = pBufIn[j + StartIndex];
                }

                StartIndex = EndIndex + 1;
            }

            for (i = 0; i < m_ProcessCount; i++)
            {
                m_threadRoutine[i].ArrayIn   = aIn[i];
                m_threadRoutine[i].m_Command = 0;
                m_threadRoutine[i].ewhStart.Set();
            }

            int outSize = 0;

            for (i = 0; i < m_ProcessCount; i++)
            {
                m_threadRoutine[i].ewhStop.WaitOne();
                outSize += m_threadRoutine[i].ArrayOut.Length;
                outSize += sizeof(int);
            }

            outSize++;

            pBufOut    = new byte[outSize];
            pBufOut[0] = Convert.ToByte(m_ProcessCount);
            StartIndex = 1;

            for (i = 0; i < m_ProcessCount; i++)
            {
                byte[] aOut  = m_threadRoutine[i].ArrayOut;
                byte[] aSize = BitConverter.GetBytes(aOut.Length);

                Array.Copy(aSize, 0, pBufOut, StartIndex, aSize.Length);
                StartIndex += aSize.Length;

                Array.Copy(aOut, 0, pBufOut, StartIndex, aOut.Length);
                StartIndex += aOut.Length;
            }

            // === stop threads ===
            Stop();
            // ====================
        }
Ejemplo n.º 3
0
        public void Decode(ref byte[] pBufIn, out byte[] pBufOut)
        {
            int i;

            pBufOut = null;

            if (pBufIn[0] == 0)
            {
                byte[] aIn = new byte[pBufIn.Length - 1];
                Array.Copy(pBufOut, 1, aIn, 0, aIn.Length);

                Huffman.Decode(ref aIn, out pBufOut);

                return;
            }

            // === start threads ===
            Start();
            // =====================

            int j;
            int StartIndex = 1;

            for (i = 0; i < m_ProcessCount; i++)
            {
                int size = BitConverter.ToInt32(pBufIn, StartIndex);
                StartIndex += sizeof(int);

                byte[] aIn = new byte[size];
                Array.Copy(pBufIn, StartIndex, aIn, 0, aIn.Length);

                StartIndex += size;
                m_threadRoutine[i].ArrayIn = aIn;
            }

            for (i = 0; i < m_ProcessCount; i++)
            {
                m_threadRoutine[i].m_Command = 1;
                m_threadRoutine[i].ewhStart.Set();
            }

            int outSize = 0;

            for (i = 0; i < m_ProcessCount; i++)
            {
                m_threadRoutine[i].ewhStop.WaitOne();
                outSize += m_threadRoutine[i].ArrayOut.Length;
            }

            pBufOut    = new byte[outSize];
            StartIndex = 0;

            for (i = 0; i < m_ProcessCount; i++)
            {
                byte[] aOut = m_threadRoutine[i].ArrayOut;

                Array.Copy(aOut, 0, pBufOut, StartIndex, aOut.Length);
                StartIndex += aOut.Length;
            }

            // === stop threads ===
            Stop();
            // ====================
        }