private void CompressCore(byte[] input, int maxBit =16)
{
int compressIndex = 0;
int lookAheadBytesCount = 0;
//we don't need to find match for the last two bytes, because it
//alway can't be found.
while (compressIndex < (input.Length - (minimumEncodeLength - 1)))
{
OffsetLengthPair match = FindMatchInSlidingWindow(input, compressIndex);
if (match.Length == 0)
{
//if there is no match, move forward one byte
lookAheadBytesCount = 1;
EncodeLiteral(input[compressIndex]);
}
else
{
lookAheadBytesCount = match.Length;
EncodeOffsetLengthPair(match, maxBit);
}
byte[] lookAheadBuffer = new byte[lookAheadBytesCount];
Array.Copy(input, compressIndex, lookAheadBuffer, 0, lookAheadBuffer.Length);
hashTable.Update(lookAheadBuffer);
slidingWindow.Update(lookAheadBuffer);
compressIndex += lookAheadBytesCount;
}
// upate hash table and sliding window
byte[] toBeUptated = new byte[input.Length - compressIndex];
Array.Copy(input, compressIndex, toBeUptated, 0, toBeUptated.Length);
hashTable.Update(toBeUptated);
slidingWindow.Update(toBeUptated);
while (compressIndex < input.Length)
{
EncodeLiteral(input[compressIndex]);
compressIndex++;
}
//write the last byte to output stream if it exists
if (remainBitsCount > 0)
{
outputStream.WriteByte((byte)(remain << (oneByteBitsCount - remainBitsCount)));
remainBitsCount = 0;
}
outputStream.Flush();
}