Use the configured ByteBufAllocator in SpdyFrameEncoder

This commit is contained in:
Norman Maurer 2013-07-14 23:38:02 +02:00 committed by Trustin Lee
parent 2de491aa7f
commit e1c78b471b
5 changed files with 99 additions and 85 deletions

View File

@ -16,13 +16,11 @@
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.UnsupportedMessageTypeException; import io.netty.handler.codec.UnsupportedMessageTypeException;
import io.netty.util.CharsetUtil;
import java.util.Set; import java.util.Set;
@ -90,7 +88,8 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<SpdyFrame> {
} else if (msg instanceof SpdySynStreamFrame) { } else if (msg instanceof SpdySynStreamFrame) {
SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg; SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg;
ByteBuf data = headerBlockEncoder.encode(spdySynStreamFrame); ByteBuf data = headerBlockEncoder.encode(ctx, spdySynStreamFrame);
try {
byte flags = spdySynStreamFrame.isLast() ? SPDY_FLAG_FIN : 0; byte flags = spdySynStreamFrame.isLast() ? SPDY_FLAG_FIN : 0;
if (spdySynStreamFrame.isUnidirectional()) { if (spdySynStreamFrame.isUnidirectional()) {
flags |= SPDY_FLAG_UNIDIRECTIONAL; flags |= SPDY_FLAG_UNIDIRECTIONAL;
@ -123,11 +122,15 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<SpdyFrame> {
out.writeShort(0); out.writeShort(0);
} }
out.writeBytes(data, data.readerIndex(), headerBlockLength); out.writeBytes(data, data.readerIndex(), headerBlockLength);
} finally {
data.release();
}
} else if (msg instanceof SpdySynReplyFrame) { } else if (msg instanceof SpdySynReplyFrame) {
SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg; SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg;
ByteBuf data = headerBlockEncoder.encode(spdySynReplyFrame); ByteBuf data = headerBlockEncoder.encode(ctx, spdySynReplyFrame);
try {
byte flags = spdySynReplyFrame.isLast() ? SPDY_FLAG_FIN : 0; byte flags = spdySynReplyFrame.isLast() ? SPDY_FLAG_FIN : 0;
int headerBlockLength = data.readableBytes(); int headerBlockLength = data.readableBytes();
int length; int length;
@ -150,6 +153,9 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<SpdyFrame> {
} }
} }
out.writeBytes(data, data.readerIndex(), headerBlockLength); out.writeBytes(data, data.readerIndex(), headerBlockLength);
} finally {
data.release();
}
} else if (msg instanceof SpdyRstStreamFrame) { } else if (msg instanceof SpdyRstStreamFrame) {
@ -224,7 +230,8 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<SpdyFrame> {
} else if (msg instanceof SpdyHeadersFrame) { } else if (msg instanceof SpdyHeadersFrame) {
SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg; SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg;
ByteBuf data = headerBlockEncoder.encode(spdyHeadersFrame); ByteBuf data = headerBlockEncoder.encode(ctx, spdyHeadersFrame);
try {
byte flags = spdyHeadersFrame.isLast() ? SPDY_FLAG_FIN : 0; byte flags = spdyHeadersFrame.isLast() ? SPDY_FLAG_FIN : 0;
int headerBlockLength = data.readableBytes(); int headerBlockLength = data.readableBytes();
int length; int length;
@ -243,6 +250,9 @@ public class SpdyFrameEncoder extends MessageToByteEncoder<SpdyFrame> {
out.writeShort(0); out.writeShort(0);
} }
out.writeBytes(data, data.readerIndex(), headerBlockLength); out.writeBytes(data, data.readerIndex(), headerBlockLength);
} finally {
data.release();
}
} else if (msg instanceof SpdyWindowUpdateFrame) { } else if (msg instanceof SpdyWindowUpdateFrame) {

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
abstract class SpdyHeaderBlockEncoder { abstract class SpdyHeaderBlockEncoder {
@ -32,6 +33,6 @@ abstract class SpdyHeaderBlockEncoder {
} }
} }
abstract ByteBuf encode(SpdyHeadersFrame frame) throws Exception; abstract ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception;
abstract void end(); abstract void end();
} }

View File

@ -15,14 +15,15 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
import com.jcraft.jzlib.Deflater; import com.jcraft.jzlib.Deflater;
import com.jcraft.jzlib.JZlib; import com.jcraft.jzlib.JZlib;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.compression.CompressionException; import io.netty.handler.codec.compression.CompressionException;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder { class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder {
private final Deflater z = new Deflater(); private final Deflater z = new Deflater();
@ -97,7 +98,7 @@ class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder {
} }
@Override @Override
public synchronized ByteBuf encode(SpdyHeadersFrame frame) throws Exception { public synchronized ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception {
if (frame == null) { if (frame == null) {
throw new IllegalArgumentException("frame"); throw new IllegalArgumentException("frame");
} }
@ -106,12 +107,12 @@ class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder {
return Unpooled.EMPTY_BUFFER; return Unpooled.EMPTY_BUFFER;
} }
ByteBuf decompressed = super.encode(frame); ByteBuf decompressed = super.encode(ctx, frame);
if (decompressed.readableBytes() == 0) { if (decompressed.readableBytes() == 0) {
return Unpooled.EMPTY_BUFFER; return Unpooled.EMPTY_BUFFER;
} }
ByteBuf compressed = Unpooled.buffer(); ByteBuf compressed = ctx.alloc().buffer();
setInput(decompressed); setInput(decompressed);
encode(compressed); encode(compressed);
return compressed; return compressed;

View File

@ -15,10 +15,11 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import java.util.Set;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import java.util.Set;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
@ -51,7 +52,7 @@ public class SpdyHeaderBlockRawEncoder extends SpdyHeaderBlockEncoder {
} }
@Override @Override
public ByteBuf encode(SpdyHeadersFrame frame) throws Exception { public ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception {
Set<String> names = frame.headers().names(); Set<String> names = frame.headers().names();
int numHeaders = names.size(); int numHeaders = names.size();
if (numHeaders == 0) { if (numHeaders == 0) {

View File

@ -15,13 +15,14 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import java.util.zip.Deflater; import java.util.zip.Deflater;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder { class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
private final byte[] out = new byte[8192]; private final byte[] out = new byte[8192];
@ -58,7 +59,7 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
} }
@Override @Override
public synchronized ByteBuf encode(SpdyHeadersFrame frame) throws Exception { public synchronized ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception {
if (frame == null) { if (frame == null) {
throw new IllegalArgumentException("frame"); throw new IllegalArgumentException("frame");
} }
@ -67,12 +68,12 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
return Unpooled.EMPTY_BUFFER; return Unpooled.EMPTY_BUFFER;
} }
ByteBuf decompressed = super.encode(frame); ByteBuf decompressed = super.encode(ctx, frame);
if (decompressed.readableBytes() == 0) { if (decompressed.readableBytes() == 0) {
return Unpooled.EMPTY_BUFFER; return Unpooled.EMPTY_BUFFER;
} }
ByteBuf compressed = Unpooled.buffer(); ByteBuf compressed = ctx.alloc().buffer();
setInput(decompressed); setInput(decompressed);
encode(compressed); encode(compressed);
return compressed; return compressed;