ByteBufs are not released properly in SnappyCodec and LZ4Codec #1046

pull/1051/head
Nikita 8 years ago
parent 45236c4b4d
commit a45a476bd3

@ -55,6 +55,10 @@ public class LZ4Codec implements Codec {
public LZ4Codec(Codec innerCodec) {
this.innerCodec = innerCodec;
}
public LZ4Codec(ClassLoader classLoader) {
this(new FstCodec(classLoader));
}
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
@ -79,21 +83,28 @@ public class LZ4Codec implements Codec {
@Override
public ByteBuf encode(Object in) throws IOException {
LZ4Compressor compressor = factory.fastCompressor();
ByteBuf bytes = innerCodec.getValueEncoder().encode(in);
ByteBuffer srcBuf = bytes.internalNioBuffer(bytes.readerIndex(), bytes.readableBytes());
int outMaxLength = compressor.maxCompressedLength(bytes.readableBytes());
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(outMaxLength + DECOMPRESSION_HEADER_SIZE);
out.writeInt(bytes.readableBytes());
ByteBuffer outBuf = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
int pos = outBuf.position();
compressor.compress(srcBuf, outBuf);
int compressedLength = outBuf.position() - pos;
out.writerIndex(out.writerIndex() + compressedLength);
return out;
ByteBuf bytes = null;
try {
LZ4Compressor compressor = factory.fastCompressor();
bytes = innerCodec.getValueEncoder().encode(in);
ByteBuffer srcBuf = bytes.internalNioBuffer(bytes.readerIndex(), bytes.readableBytes());
int outMaxLength = compressor.maxCompressedLength(bytes.readableBytes());
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(outMaxLength + DECOMPRESSION_HEADER_SIZE);
out.writeInt(bytes.readableBytes());
ByteBuffer outBuf = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
int pos = outBuf.position();
compressor.compress(srcBuf, outBuf);
int compressedLength = outBuf.position() - pos;
out.writerIndex(out.writerIndex() + compressedLength);
return out;
} finally {
if (bytes != null) {
bytes.release();
}
}
}
};

@ -60,6 +60,10 @@ public class SnappyCodec implements Codec {
this.innerCodec = innerCodec;
}
public SnappyCodec(ClassLoader classLoader) {
this(new FstCodec(classLoader));
}
private final Decoder<Object> decoder = new Decoder<Object>() {
@Override
@ -67,9 +71,9 @@ public class SnappyCodec implements Codec {
ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
try {
snappyDecoder.get().decode(buf, out);
snappyDecoder.get().reset();
return innerCodec.getValueDecoder().decode(out, state);
} finally {
snappyDecoder.get().reset();
out.release();
}
}
@ -80,10 +84,14 @@ public class SnappyCodec implements Codec {
@Override
public ByteBuf encode(Object in) throws IOException {
ByteBuf buf = innerCodec.getValueEncoder().encode(in);
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(1024*100);
snappyEncoder.get().encode(buf, out, buf.readableBytes());
snappyEncoder.get().reset();
return out;
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(buf.readableBytes() + 128);
try {
snappyEncoder.get().encode(buf, out, buf.readableBytes());
return out;
} finally {
buf.release();
snappyEncoder.get().reset();
}
}
};

Loading…
Cancel
Save