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

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.PlatformDependent;
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();
}

View File

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

View File

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

View File

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