Snappy { public void encode(final ByteBuf in, final ByteBuf out, final int length) { for (int i = 0;; i ++) { int b = length >>> i * 7; if ((b & 0xFFFFFF80) != 0) { out.writeByte(b & 0x7f | 0x80); } else { out.writeByte(b); break; } } int inIndex = in.readerIndex(); final int baseIndex = inIndex; final short[] table = getHashTable(length); final int shift = Integer.numberOfLeadingZeros(table.length) + 1; int nextEmit = inIndex; if (length - inIndex >= MIN_COMPRESSIBLE_BYTES) { int nextHash = hash(in, ++inIndex, shift); outer: while (true) { int skip = 32; int candidate; int nextIndex = inIndex; do { inIndex = nextIndex; int hash = nextHash; int bytesBetweenHashLookups = skip++ >> 5; nextIndex = inIndex + bytesBetweenHashLookups; if (nextIndex > length - 4) { break outer; } nextHash = hash(in, nextIndex, shift); candidate = baseIndex + table[hash]; table[hash] = (short) (inIndex - baseIndex); } while (in.getInt(inIndex) != in.getInt(candidate)); encodeLiteral(in, out, inIndex - nextEmit); int insertTail; do { int base = inIndex; int matched = 4 + findMatchingLength(in, candidate + 4, inIndex + 4, length); inIndex += matched; int offset = base - candidate; encodeCopy(out, offset, matched); in.readerIndex(in.readerIndex() + matched); insertTail = inIndex - 1; nextEmit = inIndex; if (inIndex >= length - 4) { break outer; } int prevHash = hash(in, insertTail, shift); table[prevHash] = (short) (inIndex - baseIndex - 1); int currentHash = hash(in, insertTail + 1, shift); candidate = baseIndex + table[currentHash]; table[currentHash] = (short) (inIndex - baseIndex); } while (in.getInt(insertTail + 1) == in.getInt(candidate)); nextHash = hash(in, insertTail + 2, shift); ++inIndex; } } if (nextEmit < length) { encodeLiteral(in, out, length - nextEmit); } }  void reset(); void encode(final ByteBuf in, final ByteBuf out, final int length); void decode(ByteBuf in, ByteBuf out);  }
@Test public void encodeShortTextIsLiteral() throws Exception { ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x6e, 0x65, 0x74, 0x74, 0x79 }); ByteBuf out = Unpooled.buffer(7); snappy.encode(in, out, 5); ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 0x05, 0x04 << 2, 0x6e, 0x65, 0x74, 0x74, 0x79 }); assertEquals("Encoded literal was invalid", expected, out); in.release(); out.release(); expected.release(); }