[#1714] Make sure ByteArrayDecoder also works correctly with pooled buffers

This commit is contained in:
Norman Maurer 2013-08-09 08:40:43 +02:00
parent 1547876e97
commit 26ddf0849f

View File

@ -51,22 +51,9 @@ import java.util.List;
public class ByteArrayDecoder extends MessageToMessageDecoder<ByteBuf> { public class ByteArrayDecoder extends MessageToMessageDecoder<ByteBuf> {
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
byte[] array; // copy the ByteBuf content to a byte array
if (msg.hasArray()) { byte[] array = new byte[msg.readableBytes()];
if (msg.arrayOffset() == 0 && msg.readableBytes() == msg.capacity()) { msg.getBytes(0, array);
// we have no offset and the length is the same as the capacity. Its safe to reuse
// the array without copy it first
array = msg.array();
} else {
// copy the ChannelBuffer to a byte array
array = new byte[msg.readableBytes()];
msg.getBytes(0, array);
}
} else {
// copy the ChannelBuffer to a byte array
array = new byte[msg.readableBytes()];
msg.getBytes(0, array);
}
out.add(array); out.add(array);
} }