Rename SnappyFramedEncoder/Decoder to SnappyFrameEncoder/Decoder
Related issue: #2766 Motivation: Forgot to rename them before the final release by mistake. Modifications: Rename and then re-introduce the deprecated version that extends the renamed class. Result: Better naming
This commit is contained in:
parent
c8841bc9de
commit
1971bd1da6
|
@ -0,0 +1,212 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.ByteBufUtil;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static io.netty.handler.codec.compression.Snappy.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uncompresses a {@link ByteBuf} encoded with the Snappy framing format.
|
||||||
|
*
|
||||||
|
* See http://code.google.com/p/snappy/source/browse/trunk/framing_format.txt
|
||||||
|
*
|
||||||
|
* Note that by default, validation of the checksum header in each chunk is
|
||||||
|
* DISABLED for performance improvements. If performance is less of an issue,
|
||||||
|
* or if you would prefer the safety that checksum validation brings, please
|
||||||
|
* use the {@link #SnappyFrameDecoder(boolean)} constructor with the argument
|
||||||
|
* set to {@code true}.
|
||||||
|
*/
|
||||||
|
public class SnappyFrameDecoder extends ByteToMessageDecoder {
|
||||||
|
|
||||||
|
private enum ChunkType {
|
||||||
|
STREAM_IDENTIFIER,
|
||||||
|
COMPRESSED_DATA,
|
||||||
|
UNCOMPRESSED_DATA,
|
||||||
|
RESERVED_UNSKIPPABLE,
|
||||||
|
RESERVED_SKIPPABLE
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final byte[] SNAPPY = { 's', 'N', 'a', 'P', 'p', 'Y' };
|
||||||
|
private static final int MAX_UNCOMPRESSED_DATA_SIZE = 65536 + 4;
|
||||||
|
|
||||||
|
private final Snappy snappy = new Snappy();
|
||||||
|
private final boolean validateChecksums;
|
||||||
|
|
||||||
|
private boolean started;
|
||||||
|
private boolean corrupted;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new snappy-framed decoder with validation of checksums
|
||||||
|
* turned OFF. To turn checksum validation on, please use the alternate
|
||||||
|
* {@link #SnappyFrameDecoder(boolean)} constructor.
|
||||||
|
*/
|
||||||
|
public SnappyFrameDecoder() {
|
||||||
|
this(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new snappy-framed decoder with validation of checksums
|
||||||
|
* as specified.
|
||||||
|
*
|
||||||
|
* @param validateChecksums
|
||||||
|
* If true, the checksum field will be validated against the actual
|
||||||
|
* uncompressed data, and if the checksums do not match, a suitable
|
||||||
|
* {@link DecompressionException} will be thrown
|
||||||
|
*/
|
||||||
|
public SnappyFrameDecoder(boolean validateChecksums) {
|
||||||
|
this.validateChecksums = validateChecksums;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||||
|
if (corrupted) {
|
||||||
|
in.skipBytes(in.readableBytes());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
int idx = in.readerIndex();
|
||||||
|
final int inSize = in.readableBytes();
|
||||||
|
if (inSize < 4) {
|
||||||
|
// We need to be at least able to read the chunk type identifier (one byte),
|
||||||
|
// and the length of the chunk (3 bytes) in order to proceed
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int chunkTypeVal = in.getUnsignedByte(idx);
|
||||||
|
final ChunkType chunkType = mapChunkType((byte) chunkTypeVal);
|
||||||
|
final int chunkLength = ByteBufUtil.swapMedium(in.getUnsignedMedium(idx + 1));
|
||||||
|
|
||||||
|
switch (chunkType) {
|
||||||
|
case STREAM_IDENTIFIER:
|
||||||
|
if (chunkLength != SNAPPY.length) {
|
||||||
|
throw new DecompressionException("Unexpected length of stream identifier: " + chunkLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inSize < 4 + SNAPPY.length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] identifier = new byte[chunkLength];
|
||||||
|
in.skipBytes(4).readBytes(identifier);
|
||||||
|
|
||||||
|
if (!Arrays.equals(identifier, SNAPPY)) {
|
||||||
|
throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy " +
|
||||||
|
"protocol version?");
|
||||||
|
}
|
||||||
|
|
||||||
|
started = true;
|
||||||
|
break;
|
||||||
|
case RESERVED_SKIPPABLE:
|
||||||
|
if (!started) {
|
||||||
|
throw new DecompressionException("Received RESERVED_SKIPPABLE tag before STREAM_IDENTIFIER");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inSize < 4 + chunkLength) {
|
||||||
|
// TODO: Don't keep skippable bytes
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.skipBytes(4 + chunkLength);
|
||||||
|
break;
|
||||||
|
case RESERVED_UNSKIPPABLE:
|
||||||
|
// The spec mandates that reserved unskippable chunks must immediately
|
||||||
|
// return an error, as we must assume that we cannot decode the stream
|
||||||
|
// correctly
|
||||||
|
throw new DecompressionException(
|
||||||
|
"Found reserved unskippable chunk type: 0x" + Integer.toHexString(chunkTypeVal));
|
||||||
|
case UNCOMPRESSED_DATA:
|
||||||
|
if (!started) {
|
||||||
|
throw new DecompressionException("Received UNCOMPRESSED_DATA tag before STREAM_IDENTIFIER");
|
||||||
|
}
|
||||||
|
if (chunkLength > MAX_UNCOMPRESSED_DATA_SIZE) {
|
||||||
|
throw new DecompressionException("Received UNCOMPRESSED_DATA larger than 65540 bytes");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inSize < 4 + chunkLength) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.skipBytes(4);
|
||||||
|
if (validateChecksums) {
|
||||||
|
int checksum = ByteBufUtil.swapInt(in.readInt());
|
||||||
|
validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4);
|
||||||
|
} else {
|
||||||
|
in.skipBytes(4);
|
||||||
|
}
|
||||||
|
out.add(in.readSlice(chunkLength - 4).retain());
|
||||||
|
break;
|
||||||
|
case COMPRESSED_DATA:
|
||||||
|
if (!started) {
|
||||||
|
throw new DecompressionException("Received COMPRESSED_DATA tag before STREAM_IDENTIFIER");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inSize < 4 + chunkLength) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.skipBytes(4);
|
||||||
|
int checksum = ByteBufUtil.swapInt(in.readInt());
|
||||||
|
ByteBuf uncompressed = ctx.alloc().buffer(0);
|
||||||
|
if (validateChecksums) {
|
||||||
|
int oldWriterIndex = in.writerIndex();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
out.add(uncompressed);
|
||||||
|
snappy.reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
corrupted = true;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes the chunk type from the type tag byte.
|
||||||
|
*
|
||||||
|
* @param type The tag byte extracted from the stream
|
||||||
|
* @return The appropriate {@link ChunkType}, defaulting to {@link ChunkType#RESERVED_UNSKIPPABLE}
|
||||||
|
*/
|
||||||
|
private static ChunkType mapChunkType(byte type) {
|
||||||
|
if (type == 0) {
|
||||||
|
return ChunkType.COMPRESSED_DATA;
|
||||||
|
} else if (type == 1) {
|
||||||
|
return ChunkType.UNCOMPRESSED_DATA;
|
||||||
|
} else if (type == (byte) 0xff) {
|
||||||
|
return ChunkType.STREAM_IDENTIFIER;
|
||||||
|
} else if ((type & 0x80) == 0x80) {
|
||||||
|
return ChunkType.RESERVED_SKIPPABLE;
|
||||||
|
} else {
|
||||||
|
return ChunkType.RESERVED_UNSKIPPABLE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,124 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.ByteBufUtil;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
|
||||||
|
import static io.netty.handler.codec.compression.Snappy.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compresses a {@link ByteBuf} using the Snappy framing format.
|
||||||
|
*
|
||||||
|
* See http://code.google.com/p/snappy/source/browse/trunk/framing_format.txt
|
||||||
|
*/
|
||||||
|
public class SnappyFrameEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||||
|
/**
|
||||||
|
* The minimum amount that we'll consider actually attempting to compress.
|
||||||
|
* This value is preamble + the minimum length our Snappy service will
|
||||||
|
* compress (instead of just emitting a literal).
|
||||||
|
*/
|
||||||
|
private static final int MIN_COMPRESSIBLE_LENGTH = 18;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All streams should start with the "Stream identifier", containing chunk
|
||||||
|
* type 0xff, a length field of 0x6, and 'sNaPpY' in ASCII.
|
||||||
|
*/
|
||||||
|
private static final byte[] STREAM_START = {
|
||||||
|
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59
|
||||||
|
};
|
||||||
|
|
||||||
|
private final Snappy snappy = new Snappy();
|
||||||
|
private boolean started;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
|
||||||
|
if (!in.isReadable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!started) {
|
||||||
|
started = true;
|
||||||
|
out.writeBytes(STREAM_START);
|
||||||
|
}
|
||||||
|
|
||||||
|
int dataLength = in.readableBytes();
|
||||||
|
if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
|
||||||
|
for (;;) {
|
||||||
|
final int lengthIdx = out.writerIndex() + 1;
|
||||||
|
if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
|
||||||
|
ByteBuf slice = in.readSlice(dataLength);
|
||||||
|
writeUnencodedChunk(slice, out, dataLength);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
out.writeInt(0);
|
||||||
|
if (dataLength > Short.MAX_VALUE) {
|
||||||
|
ByteBuf slice = in.readSlice(Short.MAX_VALUE);
|
||||||
|
calculateAndWriteChecksum(slice, out);
|
||||||
|
snappy.encode(slice, out, Short.MAX_VALUE);
|
||||||
|
setChunkLength(out, lengthIdx);
|
||||||
|
dataLength -= Short.MAX_VALUE;
|
||||||
|
} else {
|
||||||
|
ByteBuf slice = in.readSlice(dataLength);
|
||||||
|
calculateAndWriteChecksum(slice, out);
|
||||||
|
snappy.encode(slice, out, dataLength);
|
||||||
|
setChunkLength(out, lengthIdx);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
writeUnencodedChunk(in, out, dataLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void writeUnencodedChunk(ByteBuf in, ByteBuf out, int dataLength) {
|
||||||
|
out.writeByte(1);
|
||||||
|
writeChunkLength(out, dataLength + 4);
|
||||||
|
calculateAndWriteChecksum(in, out);
|
||||||
|
out.writeBytes(in, dataLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setChunkLength(ByteBuf out, int lengthIdx) {
|
||||||
|
int chunkLength = out.writerIndex() - lengthIdx - 3;
|
||||||
|
if (chunkLength >>> 24 != 0) {
|
||||||
|
throw new CompressionException("compressed data too large: " + chunkLength);
|
||||||
|
}
|
||||||
|
out.setMedium(lengthIdx, ByteBufUtil.swapMedium(chunkLength));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes the 2-byte chunk length to the output buffer.
|
||||||
|
*
|
||||||
|
* @param out The buffer to write to
|
||||||
|
* @param chunkLength The length to write
|
||||||
|
*/
|
||||||
|
private static void writeChunkLength(ByteBuf out, int chunkLength) {
|
||||||
|
out.writeMedium(ByteBufUtil.swapMedium(chunkLength));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.writeInt(ByteBufUtil.swapInt(calculateChecksum(slice)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012 The Netty Project
|
* Copyright 2014 The Netty Project
|
||||||
*
|
*
|
||||||
* The Netty Project licenses this file to you under the Apache License,
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
@ -13,199 +13,13 @@
|
||||||
* License for the specific language governing permissions and limitations
|
* License for the specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.netty.handler.codec.compression;
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
import static io.netty.handler.codec.compression.Snappy.validateChecksum;
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.ByteBufUtil;
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uncompresses a {@link ByteBuf} encoded with the Snappy framing format.
|
* @deprecated Use {@link SnappyFrameDecoder} instead.
|
||||||
*
|
|
||||||
* See http://code.google.com/p/snappy/source/browse/trunk/framing_format.txt
|
|
||||||
*
|
|
||||||
* Note that by default, validation of the checksum header in each chunk is
|
|
||||||
* DISABLED for performance improvements. If performance is less of an issue,
|
|
||||||
* or if you would prefer the safety that checksum validation brings, please
|
|
||||||
* use the {@link #SnappyFramedDecoder(boolean)} constructor with the argument
|
|
||||||
* set to {@code true}.
|
|
||||||
*/
|
*/
|
||||||
public class SnappyFramedDecoder extends ByteToMessageDecoder {
|
@Deprecated
|
||||||
|
public class SnappyFramedDecoder extends SnappyFrameDecoder {
|
||||||
private enum ChunkType {
|
// Nothing new. Just stting here for backward compatibility.
|
||||||
STREAM_IDENTIFIER,
|
|
||||||
COMPRESSED_DATA,
|
|
||||||
UNCOMPRESSED_DATA,
|
|
||||||
RESERVED_UNSKIPPABLE,
|
|
||||||
RESERVED_SKIPPABLE
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final byte[] SNAPPY = { 's', 'N', 'a', 'P', 'p', 'Y' };
|
|
||||||
private static final int MAX_UNCOMPRESSED_DATA_SIZE = 65536 + 4;
|
|
||||||
|
|
||||||
private final Snappy snappy = new Snappy();
|
|
||||||
private final boolean validateChecksums;
|
|
||||||
|
|
||||||
private boolean started;
|
|
||||||
private boolean corrupted;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new snappy-framed decoder with validation of checksums
|
|
||||||
* turned OFF. To turn checksum validation on, please use the alternate
|
|
||||||
* {@link #SnappyFramedDecoder(boolean)} constructor.
|
|
||||||
*/
|
|
||||||
public SnappyFramedDecoder() {
|
|
||||||
this(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new snappy-framed decoder with validation of checksums
|
|
||||||
* as specified.
|
|
||||||
*
|
|
||||||
* @param validateChecksums
|
|
||||||
* If true, the checksum field will be validated against the actual
|
|
||||||
* uncompressed data, and if the checksums do not match, a suitable
|
|
||||||
* {@link DecompressionException} will be thrown
|
|
||||||
*/
|
|
||||||
public SnappyFramedDecoder(boolean validateChecksums) {
|
|
||||||
this.validateChecksums = validateChecksums;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
|
||||||
if (corrupted) {
|
|
||||||
in.skipBytes(in.readableBytes());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
int idx = in.readerIndex();
|
|
||||||
final int inSize = in.readableBytes();
|
|
||||||
if (inSize < 4) {
|
|
||||||
// We need to be at least able to read the chunk type identifier (one byte),
|
|
||||||
// and the length of the chunk (3 bytes) in order to proceed
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final int chunkTypeVal = in.getUnsignedByte(idx);
|
|
||||||
final ChunkType chunkType = mapChunkType((byte) chunkTypeVal);
|
|
||||||
final int chunkLength = ByteBufUtil.swapMedium(in.getUnsignedMedium(idx + 1));
|
|
||||||
|
|
||||||
switch (chunkType) {
|
|
||||||
case STREAM_IDENTIFIER:
|
|
||||||
if (chunkLength != SNAPPY.length) {
|
|
||||||
throw new DecompressionException("Unexpected length of stream identifier: " + chunkLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inSize < 4 + SNAPPY.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] identifier = new byte[chunkLength];
|
|
||||||
in.skipBytes(4).readBytes(identifier);
|
|
||||||
|
|
||||||
if (!Arrays.equals(identifier, SNAPPY)) {
|
|
||||||
throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy " +
|
|
||||||
"protocol version?");
|
|
||||||
}
|
|
||||||
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
case RESERVED_SKIPPABLE:
|
|
||||||
if (!started) {
|
|
||||||
throw new DecompressionException("Received RESERVED_SKIPPABLE tag before STREAM_IDENTIFIER");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inSize < 4 + chunkLength) {
|
|
||||||
// TODO: Don't keep skippable bytes
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
in.skipBytes(4 + chunkLength);
|
|
||||||
break;
|
|
||||||
case RESERVED_UNSKIPPABLE:
|
|
||||||
// The spec mandates that reserved unskippable chunks must immediately
|
|
||||||
// return an error, as we must assume that we cannot decode the stream
|
|
||||||
// correctly
|
|
||||||
throw new DecompressionException(
|
|
||||||
"Found reserved unskippable chunk type: 0x" + Integer.toHexString(chunkTypeVal));
|
|
||||||
case UNCOMPRESSED_DATA:
|
|
||||||
if (!started) {
|
|
||||||
throw new DecompressionException("Received UNCOMPRESSED_DATA tag before STREAM_IDENTIFIER");
|
|
||||||
}
|
|
||||||
if (chunkLength > MAX_UNCOMPRESSED_DATA_SIZE) {
|
|
||||||
throw new DecompressionException("Received UNCOMPRESSED_DATA larger than 65540 bytes");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inSize < 4 + chunkLength) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
in.skipBytes(4);
|
|
||||||
if (validateChecksums) {
|
|
||||||
int checksum = ByteBufUtil.swapInt(in.readInt());
|
|
||||||
validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4);
|
|
||||||
} else {
|
|
||||||
in.skipBytes(4);
|
|
||||||
}
|
|
||||||
out.add(in.readSlice(chunkLength - 4).retain());
|
|
||||||
break;
|
|
||||||
case COMPRESSED_DATA:
|
|
||||||
if (!started) {
|
|
||||||
throw new DecompressionException("Received COMPRESSED_DATA tag before STREAM_IDENTIFIER");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inSize < 4 + chunkLength) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
in.skipBytes(4);
|
|
||||||
int checksum = ByteBufUtil.swapInt(in.readInt());
|
|
||||||
ByteBuf uncompressed = ctx.alloc().buffer(0);
|
|
||||||
if (validateChecksums) {
|
|
||||||
int oldWriterIndex = in.writerIndex();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
out.add(uncompressed);
|
|
||||||
snappy.reset();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
corrupted = true;
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decodes the chunk type from the type tag byte.
|
|
||||||
*
|
|
||||||
* @param type The tag byte extracted from the stream
|
|
||||||
* @return The appropriate {@link ChunkType}, defaulting to {@link ChunkType#RESERVED_UNSKIPPABLE}
|
|
||||||
*/
|
|
||||||
private static ChunkType mapChunkType(byte type) {
|
|
||||||
if (type == 0) {
|
|
||||||
return ChunkType.COMPRESSED_DATA;
|
|
||||||
} else if (type == 1) {
|
|
||||||
return ChunkType.UNCOMPRESSED_DATA;
|
|
||||||
} else if (type == (byte) 0xff) {
|
|
||||||
return ChunkType.STREAM_IDENTIFIER;
|
|
||||||
} else if ((type & 0x80) == 0x80) {
|
|
||||||
return ChunkType.RESERVED_SKIPPABLE;
|
|
||||||
} else {
|
|
||||||
return ChunkType.RESERVED_UNSKIPPABLE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012 The Netty Project
|
* Copyright 2014 The Netty Project
|
||||||
*
|
*
|
||||||
* The Netty Project licenses this file to you under the Apache License,
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
@ -13,112 +13,13 @@
|
||||||
* License for the specific language governing permissions and limitations
|
* License for the specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.netty.handler.codec.compression;
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.ByteBufUtil;
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.handler.codec.MessageToByteEncoder;
|
|
||||||
|
|
||||||
import static io.netty.handler.codec.compression.Snappy.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compresses a {@link ByteBuf} using the Snappy framing format.
|
* @deprecated Use {@link SnappyFrameEncoder} instead.
|
||||||
*
|
|
||||||
* See http://code.google.com/p/snappy/source/browse/trunk/framing_format.txt
|
|
||||||
*/
|
*/
|
||||||
public class SnappyFramedEncoder extends MessageToByteEncoder<ByteBuf> {
|
@Deprecated
|
||||||
/**
|
public class SnappyFramedEncoder extends SnappyFrameEncoder {
|
||||||
* The minimum amount that we'll consider actually attempting to compress.
|
// Nothing new. Just stting here for backward compatibility.
|
||||||
* This value is preamble + the minimum length our Snappy service will
|
|
||||||
* compress (instead of just emitting a literal).
|
|
||||||
*/
|
|
||||||
private static final int MIN_COMPRESSIBLE_LENGTH = 18;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All streams should start with the "Stream identifier", containing chunk
|
|
||||||
* type 0xff, a length field of 0x6, and 'sNaPpY' in ASCII.
|
|
||||||
*/
|
|
||||||
private static final byte[] STREAM_START = {
|
|
||||||
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59
|
|
||||||
};
|
|
||||||
|
|
||||||
private final Snappy snappy = new Snappy();
|
|
||||||
private boolean started;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
|
|
||||||
if (!in.isReadable()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!started) {
|
|
||||||
started = true;
|
|
||||||
out.writeBytes(STREAM_START);
|
|
||||||
}
|
|
||||||
|
|
||||||
int dataLength = in.readableBytes();
|
|
||||||
if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
|
|
||||||
for (;;) {
|
|
||||||
final int lengthIdx = out.writerIndex() + 1;
|
|
||||||
if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
|
|
||||||
ByteBuf slice = in.readSlice(dataLength);
|
|
||||||
writeUnencodedChunk(slice, out, dataLength);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
out.writeInt(0);
|
|
||||||
if (dataLength > Short.MAX_VALUE) {
|
|
||||||
ByteBuf slice = in.readSlice(Short.MAX_VALUE);
|
|
||||||
calculateAndWriteChecksum(slice, out);
|
|
||||||
snappy.encode(slice, out, Short.MAX_VALUE);
|
|
||||||
setChunkLength(out, lengthIdx);
|
|
||||||
dataLength -= Short.MAX_VALUE;
|
|
||||||
} else {
|
|
||||||
ByteBuf slice = in.readSlice(dataLength);
|
|
||||||
calculateAndWriteChecksum(slice, out);
|
|
||||||
snappy.encode(slice, out, dataLength);
|
|
||||||
setChunkLength(out, lengthIdx);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
writeUnencodedChunk(in, out, dataLength);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void writeUnencodedChunk(ByteBuf in, ByteBuf out, int dataLength) {
|
|
||||||
out.writeByte(1);
|
|
||||||
writeChunkLength(out, dataLength + 4);
|
|
||||||
calculateAndWriteChecksum(in, out);
|
|
||||||
out.writeBytes(in, dataLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void setChunkLength(ByteBuf out, int lengthIdx) {
|
|
||||||
int chunkLength = out.writerIndex() - lengthIdx - 3;
|
|
||||||
if (chunkLength >>> 24 != 0) {
|
|
||||||
throw new CompressionException("compressed data too large: " + chunkLength);
|
|
||||||
}
|
|
||||||
out.setMedium(lengthIdx, ByteBufUtil.swapMedium(chunkLength));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes the 2-byte chunk length to the output buffer.
|
|
||||||
*
|
|
||||||
* @param out The buffer to write to
|
|
||||||
* @param chunkLength The length to write
|
|
||||||
*/
|
|
||||||
private static void writeChunkLength(ByteBuf out, int chunkLength) {
|
|
||||||
out.writeMedium(ByteBufUtil.swapMedium(chunkLength));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.writeInt(ByteBufUtil.swapInt(calculateChecksum(slice)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,12 +24,12 @@ import org.junit.Test;
|
||||||
import static io.netty.util.ReferenceCountUtil.releaseLater;
|
import static io.netty.util.ReferenceCountUtil.releaseLater;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class SnappyFramedDecoderTest {
|
public class SnappyFrameDecoderTest {
|
||||||
private EmbeddedChannel channel;
|
private EmbeddedChannel channel;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initChannel() {
|
public void initChannel() {
|
||||||
channel = new EmbeddedChannel(new SnappyFramedDecoder());
|
channel = new EmbeddedChannel(new SnappyFrameDecoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = DecompressionException.class)
|
@Test(expected = DecompressionException.class)
|
||||||
|
@ -133,7 +133,7 @@ public class SnappyFramedDecoderTest {
|
||||||
|
|
||||||
@Test(expected = DecompressionException.class)
|
@Test(expected = DecompressionException.class)
|
||||||
public void testInvalidChecksumThrowsException() throws Exception {
|
public void testInvalidChecksumThrowsException() throws Exception {
|
||||||
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFramedDecoder(true));
|
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));
|
||||||
|
|
||||||
// checksum here is presented as 0
|
// checksum here is presented as 0
|
||||||
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
|
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
|
||||||
|
@ -146,7 +146,7 @@ public class SnappyFramedDecoderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidChecksumDoesNotThrowException() throws Exception {
|
public void testInvalidChecksumDoesNotThrowException() throws Exception {
|
||||||
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFramedDecoder(true));
|
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));
|
||||||
|
|
||||||
// checksum here is presented as a282986f (little endian)
|
// checksum here is presented as a282986f (little endian)
|
||||||
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
|
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
|
|
@ -25,12 +25,12 @@ import org.junit.Test;
|
||||||
import static io.netty.util.ReferenceCountUtil.*;
|
import static io.netty.util.ReferenceCountUtil.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class SnappyFramedEncoderTest {
|
public class SnappyFrameEncoderTest {
|
||||||
private EmbeddedChannel channel;
|
private EmbeddedChannel channel;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
channel = new EmbeddedChannel(new SnappyFramedEncoder());
|
channel = new EmbeddedChannel(new SnappyFrameEncoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
|
@ -33,12 +33,12 @@ public class SnappyIntegrationTest extends IntegrationTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected EmbeddedChannel createEncoderEmbeddedChannel() {
|
protected EmbeddedChannel createEncoderEmbeddedChannel() {
|
||||||
return new EmbeddedChannel(new SnappyFramedEncoder());
|
return new EmbeddedChannel(new SnappyFrameEncoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected EmbeddedChannel createDecoderEmbeddedChannel() {
|
protected EmbeddedChannel createDecoderEmbeddedChannel() {
|
||||||
return new EmbeddedChannel(new SnappyFramedDecoder());
|
return new EmbeddedChannel(new SnappyFrameDecoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue
Block a user