@Test public void encodeLongTextUsesCopy() throws Exception { ByteBuf in = Unpooled.wrappedBuffer( ("Netty has been designed carefully with the experiences " + "earned from the implementation of a lot of protocols " + "such as FTP, SMTP, HTTP, and various binary and " + "text-based legacy protocols").getBytes("US-ASCII") ); ByteBuf out = Unpooled.buffer(180); snappy.encode(in, out, in.readableBytes()); ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { -0x49, 0x01, -0x10, 0x42, 0x4e, 0x65, 0x74, 0x74, 0x79, 0x20, 0x68, 0x61, 0x73, 0x20, 0x62, 0x65, 0x65, 0x6e, 0x20, 0x64, 0x65, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x63, 0x61, 0x72, 0x65, 0x66, 0x75, 0x6c, 0x6c, 0x79, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x20, 0x65, 0x78, 0x70, 0x65, 0x72, 0x69, 0x65, 0x6e, 0x63, 0x65, 0x73, 0x20, 0x65, 0x61, 0x72, 0x6e, 0x65, 0x64, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x01, 0x1C, -0x10, 0x66, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x6c, 0x6f, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x73, 0x20, 0x73, 0x75, 0x63, 0x68, 0x20, 0x61, 0x73, 0x20, 0x46, 0x54, 0x50, 0x2c, 0x20, 0x53, 0x4d, 0x54, 0x50, 0x2c, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x76, 0x61, 0x72, 0x69, 0x6f, 0x75, 0x73, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x65, 0x78, 0x74, 0x2d, 0x62, 0x61, 0x73, 0x65, 0x64, 0x20, 0x6c, 0x65, 0x67, 0x61, 0x63, 0x79, 0x20, 0x15, 0x4c }); assertEquals("Encoded result was incorrect", expected, out); }
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 = 32 - (int) Math.floor(Math.log(table.length) / Math.log(2)); 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); } }
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 = 32 - (int) Math.floor(Math.log(table.length) / Math.log(2)); 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); } } }
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 = 32 - (int) Math.floor(Math.log(table.length) / Math.log(2)); 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); } }  }
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 = 32 - (int) Math.floor(Math.log(table.length) / Math.log(2)); 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); static int calculateChecksum(ByteBuf data); static int calculateChecksum(ByteBuf data, int offset, int length); }
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 = 32 - (int) Math.floor(Math.log(table.length) / Math.log(2)); 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); static int calculateChecksum(ByteBuf data); static int calculateChecksum(ByteBuf data, int offset, int length);  }