From ee192f0321c8f18cf19077cc82a3a0a3bc211727 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 1 Oct 2013 09:57:20 +0200 Subject: [PATCH] [#1880] Use ByteBufAllocator when read bytes into new chunks --- .../java/io/netty/buffer/ByteBufUtil.java | 17 +++++++++++++ .../handler/codec/http/HttpObjectDecoder.java | 20 +++++++++------- .../codec/http/HttpContentCompressorTest.java | 2 ++ .../codec/http/HttpContentEncoderTest.java | 24 ++++++++++++++++++- .../codec/http/HttpObjectAggregatorTest.java | 1 + .../codec/http/HttpResponseDecoderTest.java | 3 +++ .../codec/http/HttpServerCodecTest.java | 1 + 7 files changed, 58 insertions(+), 10 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java index 8e31aaa5be..8ce61109e9 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java @@ -257,6 +257,23 @@ public final class ByteBufUtil { return Long.reverseBytes(value); } + /** + * Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}. + */ + public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length) { + boolean release = true; + ByteBuf dst = alloc.buffer(length); + try { + buffer.readBytes(dst); + release = false; + return dst; + } finally { + if (release) { + dst.release(); + } + } + } + private static int firstIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) { fromIndex = Math.max(fromIndex, 0); if (fromIndex >= toIndex || buffer.capacity() == 0) { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java index eb18298256..7a54a435e8 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java @@ -25,6 +25,8 @@ import io.netty.handler.codec.TooLongFrameException; import java.util.List; +import static io.netty.buffer.ByteBufUtil.readBytes; + /** * Decodes {@link ByteBuf}s into {@link HttpMessage}s and * {@link HttpContent}s. @@ -265,7 +267,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder maxChunkSize) { toRead = maxChunkSize; } - ByteBuf content = buffer.readBytes(toRead); + ByteBuf content = readBytes(ctx.alloc(), buffer, toRead); if (!buffer.isReadable()) { reset(); out.add(new DefaultLastHttpContent(content)); @@ -284,7 +286,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder chunkSize) { toRead = (int) chunkSize; } - ByteBuf content = buffer.readBytes(toRead); + ByteBuf content = readBytes(ctx.alloc(), buffer, toRead); if (chunkSize > toRead) { chunkSize -= toRead; } else { @@ -348,7 +350,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder readLimit) { toRead = readLimit; } - HttpContent chunk = new DefaultHttpContent(buffer.readBytes(toRead)); + HttpContent chunk = new DefaultHttpContent(readBytes(ctx.alloc(), buffer, toRead)); if (chunkSize > toRead) { chunkSize -= toRead; } else { @@ -553,7 +555,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder out) { + private void readFixedLengthContent(ChannelHandlerContext ctx, ByteBuf buffer, List out) { //we have a content-length so we just read the correct number of bytes long length = HttpHeaders.getContentLength(message, -1); assert length <= Integer.MAX_VALUE; @@ -564,11 +566,11 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder