diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java index 1bf24bfb67..4425b35a2d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java @@ -93,42 +93,46 @@ public abstract class HttpObjectEncoder extends MessageTo state = ST_INIT; } } else if (state == ST_CONTENT_CHUNK) { - if (contentLength > 0) { - byte[] length = Integer.toHexString(contentLength).getBytes(CharsetUtil.US_ASCII); - ByteBuf buf = ctx.alloc().buffer(length.length + 2); - buf.writeBytes(length); - buf.writeBytes(CRLF); - out.add(buf); - out.add(encodeAndRetain(msg)); - out.add(CRLF_BUF.duplicate()); - } - - if (msg instanceof LastHttpContent) { - HttpHeaders headers = ((LastHttpContent) msg).trailingHeaders(); - if (headers.isEmpty()) { - out.add(ZERO_CRLF_CRLF_BUF.duplicate()); - } else { - ByteBuf buf = ctx.alloc().buffer(); - buf.writeBytes(ZERO_CRLF); - encodeHeaders(buf, headers); - buf.writeBytes(CRLF); - out.add(buf); - } - - state = ST_INIT; - } else { - if (contentLength == 0) { - // Need to produce some output otherwise an - // IllegalstateException will be thrown - out.add(EMPTY_BUFFER); - } - } + encodeChunkedContent(ctx, msg, contentLength, out); } else { throw new Error(); } } } + private void encodeChunkedContent(ChannelHandlerContext ctx, Object msg, int contentLength, List out) { + if (contentLength > 0) { + byte[] length = Integer.toHexString(contentLength).getBytes(CharsetUtil.US_ASCII); + ByteBuf buf = ctx.alloc().buffer(length.length + 2); + buf.writeBytes(length); + buf.writeBytes(CRLF); + out.add(buf); + out.add(encodeAndRetain(msg)); + out.add(CRLF_BUF.duplicate()); + } + + if (msg instanceof LastHttpContent) { + HttpHeaders headers = ((LastHttpContent) msg).trailingHeaders(); + if (headers.isEmpty()) { + out.add(ZERO_CRLF_CRLF_BUF.duplicate()); + } else { + ByteBuf buf = ctx.alloc().buffer(); + buf.writeBytes(ZERO_CRLF); + encodeHeaders(buf, headers); + buf.writeBytes(CRLF); + out.add(buf); + } + + state = ST_INIT; + } else { + if (contentLength == 0) { + // Need to produce some output otherwise an + // IllegalstateException will be thrown + out.add(EMPTY_BUFFER); + } + } + } + @Override public boolean acceptOutboundMessage(Object msg) throws Exception { return msg instanceof HttpObject || msg instanceof ByteBuf || msg instanceof FileRegion;