[#1613] Allow to specify if direct buffers are prefered in ByteToMessageCodec

This commit is contained in:
Norman Maurer 2013-07-19 09:39:06 +02:00
parent 904385117f
commit eb3283c59c

View File

@ -34,17 +34,7 @@ import java.util.List;
public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler { public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
private final TypeParameterMatcher outboundMsgMatcher; private final TypeParameterMatcher outboundMsgMatcher;
private final MessageToByteEncoder<I> encoder = new MessageToByteEncoder<I>() { private final MessageToByteEncoder<I> encoder;
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
return ByteToMessageCodec.this.acceptOutboundMessage(msg);
}
@Override
protected void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception {
ByteToMessageCodec.this.encode(ctx, msg, out);
}
};
private final ByteToMessageDecoder decoder = new ByteToMessageDecoder() { private final ByteToMessageDecoder decoder = new ByteToMessageDecoder() {
@Override @Override
@ -59,22 +49,43 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
}; };
/** /**
* Create a new instance which will try to detect the types to encode out of the type parameter * @see {@link #ByteToMessageCodec(boolean)} with {@code true} as boolean parameter.
* of the class.
*/ */
protected ByteToMessageCodec() { protected ByteToMessageCodec() {
checkForSharableAnnotation(); this(true);
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
} }
/** /**
* Create a new instance. * @see {@link #ByteToMessageCodec(Class, boolean)} with {@code true} as boolean value.
*
* @param outboundMessageType The type of messages to encode
*/ */
protected ByteToMessageCodec(Class<? extends I> outboundMessageType) { protected ByteToMessageCodec(Class<? extends I> outboundMessageType) {
this(outboundMessageType, true);
}
/**
* Create a new instance which will try to detect the types to match out of the type parameter of the class.
*
* @param preferDirect {@code true} if a direct {@link ByteBuf} should be tried to be used as target for
* the encoded messages. If {@code false} is used it will allocate a heap
* {@link ByteBuf}, which is backed by an byte array.
*/
protected ByteToMessageCodec(boolean preferDirect) {
outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I");
encoder = new Encoder(preferDirect);
}
/**
* Create a new instance
*
* @param outboundMessageType The type of messages to match
* @param preferDirect {@code true} if a direct {@link ByteBuf} should be tried to be used as target for
* the encoded messages. If {@code false} is used it will allocate a heap
* {@link ByteBuf}, which is backed by an byte array.
*/
protected ByteToMessageCodec(Class<? extends I> outboundMessageType, boolean preferDirect) {
checkForSharableAnnotation(); checkForSharableAnnotation();
outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType); outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType);
encoder = new Encoder(preferDirect);
} }
private void checkForSharableAnnotation() { private void checkForSharableAnnotation() {
@ -118,4 +129,20 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler {
protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
decode(ctx, in, out); decode(ctx, in, out);
} }
private final class Encoder extends MessageToByteEncoder<I> {
Encoder(boolean preferDirect) {
super(preferDirect);
}
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
return ByteToMessageCodec.this.acceptOutboundMessage(msg);
}
@Override
protected void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception {
ByteToMessageCodec.this.encode(ctx, msg, out);
}
}
} }