Do not attempt to compress trailing data that is less than the MIN_COMPRESSIBLE_LENGTH

This commit is contained in:
Luke Wood 2013-02-22 14:09:29 +00:00
parent 7a8e9d7993
commit 0bfa9159e3

View File

@ -60,6 +60,12 @@ public class SnappyFramedEncoder extends ByteToByteEncoder {
if (dataLength > MIN_COMPRESSIBLE_LENGTH) { if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
for (;;) { for (;;) {
final int lengthIdx = out.writerIndex() + 1; final int lengthIdx = out.writerIndex() + 1;
if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
ByteBuf slice = in.readSlice(dataLength);
writeUnencodedChunk(slice, out, dataLength);
break;
}
out.writeInt(0); out.writeInt(0);
if (dataLength >= 32768) { if (dataLength >= 32768) {
ByteBuf slice = in.readSlice(32767); ByteBuf slice = in.readSlice(32767);
@ -76,12 +82,16 @@ public class SnappyFramedEncoder extends ByteToByteEncoder {
} }
} }
} else { } else {
writeUnencodedChunk(in, out, dataLength);
}
}
private static void writeUnencodedChunk(ByteBuf in, ByteBuf out, int dataLength) {
out.writeByte(1); out.writeByte(1);
writeChunkLength(out, dataLength + 4); writeChunkLength(out, dataLength + 4);
calculateAndWriteChecksum(in, out); calculateAndWriteChecksum(in, out);
out.writeBytes(in, dataLength); out.writeBytes(in, dataLength);
} }
}
private static void setChunkLength(ByteBuf out, int lengthIdx) { private static void setChunkLength(ByteBuf out, int lengthIdx) {
int chunkLength = out.writerIndex() - lengthIdx - 3; int chunkLength = out.writerIndex() - lengthIdx - 3;