Refine search
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (corrupted) { in.skipBytes(in.readableBytes()); return; int idx = in.readerIndex(); final int inSize = in.readableBytes(); if (inSize < 4) { if (validateChecksums) { int checksum = in.readIntLE(); validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4); } else { in.skipBytes(4); try { in.writerIndex(in.readerIndex() + chunkLength - 4); snappy.decode(in, uncompressed); } finally { in.writerIndex(oldWriterIndex); validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex()); } else { snappy.decode(in.readSlice(chunkLength - 4), uncompressed); snappy.reset(); break;
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { if (!in.isReadable()) { return; out.writeBytes(STREAM_START); int dataLength = in.readableBytes(); if (dataLength > MIN_COMPRESSIBLE_LENGTH) { for (;;) { ByteBuf slice = in.readSlice(Short.MAX_VALUE); calculateAndWriteChecksum(slice, out); snappy.encode(slice, out, Short.MAX_VALUE); setChunkLength(out, lengthIdx); dataLength -= Short.MAX_VALUE; ByteBuf slice = in.readSlice(dataLength); calculateAndWriteChecksum(slice, out); snappy.encode(slice, out, dataLength); setChunkLength(out, lengthIdx); break;
/** * Writes a literal to the supplied output buffer by directly copying from * the input buffer. The literal is taken from the current readerIndex * up to the supplied length. * * @param in The input buffer to copy from * @param out The output buffer to copy to * @param length The length of the literal to copy */ static void encodeLiteral(ByteBuf in, ByteBuf out, int length) { if (length < 61) { out.writeByte(length - 1 << 2); } else { int bitLength = bitsToEncode(length - 1); int bytesToEncode = 1 + bitLength / 8; out.writeByte(59 + bytesToEncode << 2); for (int i = 0; i < bytesToEncode; i++) { out.writeByte(length - 1 >> i * 8 & 0x0ff); } } out.writeBytes(in, length); }
final short[] table = getHashTable(length); final int shift = Integer.numberOfLeadingZeros(table.length) + 1; int nextHash = hash(in, ++inIndex, shift); outer: while (true) { int skip = 32; nextHash = hash(in, nextIndex, shift); encodeLiteral(in, out, inIndex - nextEmit); int matched = 4 + findMatchingLength(in, candidate + 4, inIndex + 4, length); inIndex += matched; int offset = base - candidate; encodeCopy(out, offset, matched); 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); nextHash = hash(in, insertTail + 2, shift); ++inIndex; encodeLiteral(in, out, length - nextEmit);
int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; out.ensureWritable(uncompressedLength); state = State.READING_TAG; if (!in.isReadable()) { return; int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG;
@Override public Object decode(ByteBuf buf, State state) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { while (buf.isReadable()) { int chunkSize = buf.readInt(); ByteBuf chunk = buf.readSlice(chunkSize); snappyDecoder.get().decode(chunk, out); snappyDecoder.get().reset(); } return innerCodec.getValueDecoder().decode(out, state); } finally { snappyDecoder.get().reset(); out.release(); } } };
@Override public ByteBuf encode(Object in) throws IOException { ByteBuf buf = innerCodec.getValueEncoder().encode(in); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { int chunksAmount = (int)Math.ceil(buf.readableBytes() / (double)Short.MAX_VALUE); for (int i = 1; i <= chunksAmount; i++) { int chunkSize = Math.min(Short.MAX_VALUE, buf.readableBytes()); ByteBuf chunk = buf.readSlice(chunkSize); int lenIndex = out.writerIndex(); out.writeInt(0); snappyEncoder.get().encode(chunk, out, chunk.readableBytes()); int compressedDataLength = out.writerIndex() - 4 - lenIndex; out.setInt(lenIndex, compressedDataLength); } return out; } finally { buf.release(); snappyEncoder.get().reset(); } } };
/** * Computes the CRC32C checksum of the supplied data and performs the "mask" operation * on the computed checksum * * @param data The input data to calculate the CRC32C checksum of */ static int calculateChecksum(ByteBuf data) { return calculateChecksum(data, data.readerIndex(), data.readableBytes()); }
/** * Calculates and writes the 4-byte checksum to the output buffer * * @param slice The data to calculate the checksum for * @param out The output buffer to write the checksum to */ private static void calculateAndWriteChecksum(ByteBuf slice, ByteBuf out) { out.writeIntLE(calculateChecksum(slice)); } }
/** * Computes the CRC32C checksum of the supplied data, performs the "mask" operation * on the computed checksum, and then compares the resulting masked checksum to the * supplied checksum. * * @param expectedChecksum The checksum decoded from the stream to compare against * @param data The input data to calculate the CRC32C checksum of * @throws DecompressionException If the calculated and supplied checksums do not match */ static void validateChecksum(int expectedChecksum, ByteBuf data, int offset, int length) { final int actualChecksum = calculateChecksum(data, offset, length); if (actualChecksum != expectedChecksum) { throw new DecompressionException( "mismatching checksum: " + Integer.toHexString(actualChecksum) + " (expected: " + Integer.toHexString(expectedChecksum) + ')'); } }
@Override public ByteBuf encode(Object in) throws IOException { ByteBuf buf = innerCodec.getValueEncoder().encode(in); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { int chunksAmount = (int)Math.ceil(buf.readableBytes() / (double)Short.MAX_VALUE); for (int i = 1; i <= chunksAmount; i++) { int chunkSize = Math.min(Short.MAX_VALUE, buf.readableBytes()); ByteBuf chunk = buf.readSlice(chunkSize); int lenIndex = out.writerIndex(); out.writeInt(0); snappyEncoder.get().encode(chunk, out, chunk.readableBytes()); int compressedDataLength = out.writerIndex() - 4 - lenIndex; out.setInt(lenIndex, compressedDataLength); } return out; } finally { buf.release(); snappyEncoder.get().reset(); } } };
/** * Computes the CRC32C checksum of the supplied data and performs the "mask" operation * on the computed checksum * * @param data The input data to calculate the CRC32C checksum of */ static int calculateChecksum(ByteBuf data) { return calculateChecksum(data, data.readerIndex(), data.readableBytes()); }
final short[] table = getHashTable(length); final int shift = Integer.numberOfLeadingZeros(table.length) + 1; int nextHash = hash(in, ++inIndex, shift); outer: while (true) { int skip = 32; nextHash = hash(in, nextIndex, shift); encodeLiteral(in, out, inIndex - nextEmit); int matched = 4 + findMatchingLength(in, candidate + 4, inIndex + 4, length); inIndex += matched; int offset = base - candidate; encodeCopy(out, offset, matched); 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); nextHash = hash(in, insertTail + 2, shift); ++inIndex; encodeLiteral(in, out, length - nextEmit);
@Override public Object decode(ByteBuf buf, State state) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { while (buf.isReadable()) { int chunkSize = buf.readInt(); ByteBuf chunk = buf.readSlice(chunkSize); snappyDecoder.get().decode(chunk, out); snappyDecoder.get().reset(); } return innerCodec.getValueDecoder().decode(out, state); } finally { snappyDecoder.get().reset(); out.release(); } } };
int uncompressedLength = readPreamble(in); if (uncompressedLength == PREAMBLE_NOT_FULL) { return; out.ensureWritable(uncompressedLength); state = State.READING_TAG; if (!in.isReadable()) { return; int literalWritten = decodeLiteral(tag, in, out); if (literalWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; switch (tag & 0x03) { case COPY_1_BYTE_OFFSET: decodeWritten = decodeCopyWith1ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; decodeWritten = decodeCopyWith2ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG; decodeWritten = decodeCopyWith4ByteOffset(tag, in, out, written); if (decodeWritten != NOT_ENOUGH_INPUT) { state = State.READING_TAG;
/** * Calculates and writes the 4-byte checksum to the output buffer * * @param slice The data to calculate the checksum for * @param out The output buffer to write the checksum to */ private static void calculateAndWriteChecksum(ByteBuf slice, ByteBuf out) { out.writeIntLE(calculateChecksum(slice)); } }
/** * Computes the CRC32C checksum of the supplied data, performs the "mask" operation * on the computed checksum, and then compares the resulting masked checksum to the * supplied checksum. * * @param expectedChecksum The checksum decoded from the stream to compare against * @param data The input data to calculate the CRC32C checksum of * @throws DecompressionException If the calculated and supplied checksums do not match */ static void validateChecksum(int expectedChecksum, ByteBuf data, int offset, int length) { final int actualChecksum = calculateChecksum(data, offset, length); if (actualChecksum != expectedChecksum) { throw new DecompressionException( "mismatching checksum: " + Integer.toHexString(actualChecksum) + " (expected: " + Integer.toHexString(expectedChecksum) + ')'); } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (corrupted) { in.skipBytes(in.readableBytes()); return; int idx = in.readerIndex(); final int inSize = in.readableBytes(); if (inSize < 4) { if (validateChecksums) { int checksum = in.readIntLE(); validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4); } else { in.skipBytes(4); try { in.writerIndex(in.readerIndex() + chunkLength - 4); snappy.decode(in, uncompressed); } finally { in.writerIndex(oldWriterIndex); validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex()); } else { snappy.decode(in.readSlice(chunkLength - 4), uncompressed); snappy.reset(); break;
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { if (!in.isReadable()) { return; out.writeBytes(STREAM_START); int dataLength = in.readableBytes(); if (dataLength > MIN_COMPRESSIBLE_LENGTH) { for (;;) { ByteBuf slice = in.readSlice(Short.MAX_VALUE); calculateAndWriteChecksum(slice, out); snappy.encode(slice, out, Short.MAX_VALUE); setChunkLength(out, lengthIdx); dataLength -= Short.MAX_VALUE; ByteBuf slice = in.readSlice(dataLength); calculateAndWriteChecksum(slice, out); snappy.encode(slice, out, dataLength); setChunkLength(out, lengthIdx); break;
@Override public ByteBuf encode(Object in) throws IOException { ByteBuf buf = innerCodec.getValueEncoder().encode(in); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { int chunksAmount = (int)Math.ceil(buf.readableBytes() / (double)Short.MAX_VALUE); for (int i = 1; i <= chunksAmount; i++) { int chunkSize = Math.min(Short.MAX_VALUE, buf.readableBytes()); ByteBuf chunk = buf.readSlice(chunkSize); int lenIndex = out.writerIndex(); out.writeInt(0); snappyEncoder.get().encode(chunk, out, chunk.readableBytes()); int compressedDataLength = out.writerIndex() - 4 - lenIndex; out.setInt(lenIndex, compressedDataLength); } return out; } finally { buf.release(); snappyEncoder.get().reset(); } } };