public byte[] Encode(byte[] input) { var buffer = new byte[0xffff]; var bufferIdx = 0; using (var ms = new MemoryStream()) { using (var bw = new BitWriter(ms)) { int i = 0; string w = ""; while (i < input.Length) { bufferIdx = 0; buffer[bufferIdx++] = input[i++]; int cur = -1, prev = -1; while ((cur = lookup.FindEntry(buffer, 0, bufferIdx)) != -1 && i < input.Length) { buffer[bufferIdx++] = input[i++]; prev = cur; } if (cur == -1) { //Debug.WriteLine("E: " + prev); bw.WriteBits((uint)prev, (uint)lookup.CodeLen); lookup.AppendChar(buffer[bufferIdx - 1], prev); i--; } else { //Debug.WriteLine("E: " + cur); bw.WriteBits((uint)cur, (uint)lookup.CodeLen); } lookup.EnsureCodeLen2(); if (lookup.Count > 0xFFF) { lookup.Reset(); } } } return(ms.ToArray()); } }