@Test public void testLarge2ByteLiteralLengthAndCopyOffset() { ByteBuf compressed = Unpooled.buffer(); ByteBuf actualDecompressed = Unpooled.buffer(); ByteBuf expectedDecompressed = Unpooled.buffer().writeByte(0x01).writeZero(0x8000).writeByte(0x01); try { compressed.writeByte(0x82).writeByte(0x80).writeByte(0x02); compressed.writeByte(61 << 2); compressed.writeShortLE(0x8000); compressed.writeByte(0x01).writeZero(0x8000); compressed.writeByte(0x02); compressed.writeShortLE(0x8001); snappy.decode(compressed, actualDecompressed); assertEquals(expectedDecompressed, actualDecompressed); } finally { compressed.release(); actualDecompressed.release(); expectedDecompressed.release(); } }
public void decode(ByteBuf in, ByteBuf out) { while (in.isReadable()) { switch (state) { case READY: state = State.READING_PREAMBLE; case READING_PREAMBLE: int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; } if (uncompressedLength == 0) { state = State.READY; return; } out.ensureWritable(uncompressedLength); state = State.READING_TAG; case READING_TAG: if (!in.isReadable()) { return; } tag = in.readByte(); switch (tag & 0x03) { case LITERAL: state = State.READING_LITERAL; break; case COPY_1_BYTE_OFFSET: case COPY_2_BYTE_OFFSET: case COPY_4_BYTE_OFFSET: state = State.READING_COPY; break; } break; case READING_LITERAL: int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += literalWritten; } else { return; } break; case READING_COPY: int decodeWritten; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_2_BYTE_OFFSET: decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_4_BYTE_OFFSET: decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; } } } }
Snappy { public void decode(ByteBuf in, ByteBuf out) { while (in.isReadable()) { switch (state) { case READY: state = State.READING_PREAMBLE; case READING_PREAMBLE: int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; } if (uncompressedLength == 0) { state = State.READY; return; } out.ensureWritable(uncompressedLength); state = State.READING_TAG; case READING_TAG: if (!in.isReadable()) { return; } tag = in.readByte(); switch (tag & 0x03) { case LITERAL: state = State.READING_LITERAL; break; case COPY_1_BYTE_OFFSET: case COPY_2_BYTE_OFFSET: case COPY_4_BYTE_OFFSET: state = State.READING_COPY; break; } break; case READING_LITERAL: int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += literalWritten; } else { return; } break; case READING_COPY: int decodeWritten; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_2_BYTE_OFFSET: decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_4_BYTE_OFFSET: decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; } } } } }
Snappy { public void decode(ByteBuf in, ByteBuf out) { while (in.isReadable()) { switch (state) { case READY: state = State.READING_PREAMBLE; case READING_PREAMBLE: int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; } if (uncompressedLength == 0) { state = State.READY; return; } out.ensureWritable(uncompressedLength); state = State.READING_TAG; case READING_TAG: if (!in.isReadable()) { return; } tag = in.readByte(); switch (tag & 0x03) { case LITERAL: state = State.READING_LITERAL; break; case COPY_1_BYTE_OFFSET: case COPY_2_BYTE_OFFSET: case COPY_4_BYTE_OFFSET: state = State.READING_COPY; break; } break; case READING_LITERAL: int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += literalWritten; } else { return; } break; case READING_COPY: int decodeWritten; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_2_BYTE_OFFSET: decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_4_BYTE_OFFSET: decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; } } } }  }
Snappy { public void decode(ByteBuf in, ByteBuf out) { while (in.isReadable()) { switch (state) { case READY: state = State.READING_PREAMBLE; case READING_PREAMBLE: int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; } if (uncompressedLength == 0) { state = State.READY; return; } out.ensureWritable(uncompressedLength); state = State.READING_TAG; case READING_TAG: if (!in.isReadable()) { return; } tag = in.readByte(); switch (tag & 0x03) { case LITERAL: state = State.READING_LITERAL; break; case COPY_1_BYTE_OFFSET: case COPY_2_BYTE_OFFSET: case COPY_4_BYTE_OFFSET: state = State.READING_COPY; break; } break; case READING_LITERAL: int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += literalWritten; } else { return; } break; case READING_COPY: int decodeWritten; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_2_BYTE_OFFSET: decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_4_BYTE_OFFSET: decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; } } } }  void reset(); void encode(final ByteBuf in, final ByteBuf out, final int length); void decode(ByteBuf in, ByteBuf out); }
Snappy { public void decode(ByteBuf in, ByteBuf out) { while (in.isReadable()) { switch (state) { case READY: state = State.READING_PREAMBLE; case READING_PREAMBLE: int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; } if (uncompressedLength == 0) { state = State.READY; return; } out.ensureWritable(uncompressedLength); state = State.READING_TAG; case READING_TAG: if (!in.isReadable()) { return; } tag = in.readByte(); switch (tag & 0x03) { case LITERAL: state = State.READING_LITERAL; break; case COPY_1_BYTE_OFFSET: case COPY_2_BYTE_OFFSET: case COPY_4_BYTE_OFFSET: state = State.READING_COPY; break; } break; case READING_LITERAL: int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += literalWritten; } else { return; } break; case READING_COPY: int decodeWritten; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_2_BYTE_OFFSET: decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; case COPY_4_BYTE_OFFSET: decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; written += decodeWritten; } else { return; } break; } } } }  void reset(); void encode(final ByteBuf in, final ByteBuf out, final int length); void decode(ByteBuf in, ByteBuf out);  }