public override string Encode(byte[] data)
{
if (data == null || data.Length == 0)
return "";
int mainBitsLength = (data.Length * 8 / BlockBitsCount) * BlockBitsCount;
int tailBitsLength = data.Length * 8 - mainBitsLength;
int globalBitsLength = mainBitsLength + tailBitsLength;
int mainCharsCount = mainBitsLength * BlockCharsCount / BlockBitsCount;
int tailCharsCount = (tailBitsLength * BlockCharsCount + BlockBitsCount - 1) / BlockBitsCount;
int globalCharsCount = mainCharsCount + tailCharsCount;
int iterationCount = mainCharsCount / BlockCharsCount;
var result = new char[globalCharsCount];
if (!Parallel)
EncodeBlock(data, result, 0, iterationCount);
else
{
int processorCount = Math.Min(iterationCount, Environment.ProcessorCount);
System.Threading.Tasks.Parallel.For(0, processorCount, i =>
{
int beginInd = i * iterationCount / processorCount;
int endInd = (i + 1) * iterationCount / processorCount;
EncodeBlock(data, result, beginInd, endInd);
});
}
if (tailBitsLength != 0)
{
ulong bits = GetBits64(data, mainBitsLength, tailBitsLength);
BitsToChars(result, mainCharsCount, tailCharsCount, bits);
}
return new string(result);
}