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(); } }
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(); // ==================== }