diff --git a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java index 8c7f6cdd1d..d773545b7b 100644 --- a/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/ByteToMessageCodec.java @@ -58,11 +58,20 @@ public abstract class ByteToMessageCodec extends ChannelDuplexHandler { } }; + /** + * Create a new instance which will try to detect the types to encode out of the type parameter + * of the class. + */ protected ByteToMessageCodec() { checkForSharableAnnotation(); outboundMsgMatcher = TypeParameterMatcher.find(this, ByteToMessageCodec.class, "I"); } + /** + * Create a new instance. + * + * @param outboundMessageType The type of messages to encode + */ protected ByteToMessageCodec(Class outboundMessageType) { checkForSharableAnnotation(); outboundMsgMatcher = TypeParameterMatcher.get(outboundMessageType); @@ -74,6 +83,11 @@ public abstract class ByteToMessageCodec extends ChannelDuplexHandler { } } + /** + * Returns {@code true} if and only if the specified message can be encoded by this codec. + * + * @param msg the message + */ public boolean acceptOutboundMessage(Object msg) throws Exception { return outboundMsgMatcher.match(msg); } diff --git a/codec/src/main/java/io/netty/handler/codec/LengthFieldPrepender.java b/codec/src/main/java/io/netty/handler/codec/LengthFieldPrepender.java index 397549b887..dd8e0e834c 100644 --- a/codec/src/main/java/io/netty/handler/codec/LengthFieldPrepender.java +++ b/codec/src/main/java/io/netty/handler/codec/LengthFieldPrepender.java @@ -129,7 +129,6 @@ public class LengthFieldPrepender extends MessageToByteEncoder { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { - int length = msg.readableBytes() + lengthAdjustment; if (lengthIncludesLengthFieldLength) { length += lengthFieldLength; diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java index 87b17515f8..d1ff1aee5b 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToByteEncoder.java @@ -18,7 +18,9 @@ package io.netty.handler.codec; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandler; import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPromise; import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.TypeParameterMatcher; @@ -46,24 +48,49 @@ public abstract class MessageToByteEncoder extends ChannelOutboundHandlerAdap private final TypeParameterMatcher matcher; private final boolean preferDirect; + /** + * @see {@link #MessageToByteEncoder(boolean)} with {@code true} as boolean parameter. + */ protected MessageToByteEncoder() { this(true); } + /** + * @see {@link #MessageToByteEncoder(Class, boolean)} with {@code true} as boolean value. + */ protected MessageToByteEncoder(Class 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 MessageToByteEncoder(boolean preferDirect) { matcher = TypeParameterMatcher.find(this, MessageToByteEncoder.class, "I"); this.preferDirect = preferDirect; } + /** + * Create a new instance + * + * @param outboundMessageType The tpye 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 MessageToByteEncoder(Class outboundMessageType, boolean preferDirect) { matcher = TypeParameterMatcher.get(outboundMessageType); this.preferDirect = preferDirect; } + /** + * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next + * {@link ChannelOutboundHandler} in the {@link ChannelPipeline}. + */ public boolean acceptOutboundMessage(Object msg) throws Exception { return matcher.match(msg); } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java index c5cd73e13d..08d4568bd6 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageCodec.java @@ -33,17 +33,17 @@ import java.util.List; * *
  *     public class NumberCodec extends
- *             {@link MessageToMessageCodec}<{@link Integer}, {@link Long}, {@link Long}, {@link Integer}> {
+ *             {@link MessageToMessageCodec}<{@link Integer}, {@link Long}> {
  *         {@code @Override}
- *         public {@link Long} decode({@link ChannelHandlerContext} ctx, {@link Integer} msg)
+ *         public {@link Long} decode({@link ChannelHandlerContext} ctx, {@link Integer} msg, List<Object> out)
  *                 throws {@link Exception} {
- *             return msg.longValue();
+ *             out.add(msg.longValue());
  *         }
  *
  *         {@code @Overrride}
- *         public {@link Integer} encode({@link ChannelHandlerContext} ctx, {@link Long} msg)
+ *         public {@link Integer} encode({@link ChannelHandlerContext} ctx, {@link Long} msg, List<Object> out)
  *                 throws {@link Exception} {
- *             return msg.intValue();
+ *             out.add(msg.intValue());
  *         }
  *     }
  * 
@@ -85,11 +85,21 @@ public abstract class MessageToMessageCodec extends Cha private final TypeParameterMatcher inboundMsgMatcher; private final TypeParameterMatcher outboundMsgMatcher; + /** + * Create a new instance which will try to detect the types to decode and encode out of the type parameter + * of the class. + */ protected MessageToMessageCodec() { inboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, "INBOUND_IN"); outboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, "OUTBOUND_IN"); } + /** + * Create a new instance. + * + * @param inboundMessageType The type of messages to decode + * @param outboundMessageType The type of messages to encode + */ protected MessageToMessageCodec( Class inboundMessageType, Class outboundMessageType) { inboundMsgMatcher = TypeParameterMatcher.get(inboundMessageType); diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java index be1c41e5b4..d5c03bc5af 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageDecoder.java @@ -16,7 +16,9 @@ package io.netty.handler.codec; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelPipeline; import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCounted; import io.netty.util.internal.RecyclableArrayList; @@ -52,14 +54,26 @@ public abstract class MessageToMessageDecoder extends ChannelInboundHandlerAd private final TypeParameterMatcher matcher; + /** + * Create a new instance which will try to detect the types to match out of the type parameter of the class. + */ protected MessageToMessageDecoder() { matcher = TypeParameterMatcher.find(this, MessageToMessageDecoder.class, "I"); } + /** + * Create a new instance + * + * @param inboundMessageType The type of messages to match and so decode + */ protected MessageToMessageDecoder(Class inboundMessageType) { matcher = TypeParameterMatcher.get(inboundMessageType); } + /** + * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next + * {@link ChannelInboundHandler} in the {@link ChannelPipeline}. + */ public boolean acceptInboundMessage(Object msg) throws Exception { return matcher.match(msg); } diff --git a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java index 47422ce93b..401f35ed12 100644 --- a/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java +++ b/codec/src/main/java/io/netty/handler/codec/MessageToMessageEncoder.java @@ -16,7 +16,9 @@ package io.netty.handler.codec; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandler; import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPromise; import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCounted; @@ -51,14 +53,26 @@ public abstract class MessageToMessageEncoder extends ChannelOutboundHandlerA private final TypeParameterMatcher matcher; + /** + * Create a new instance which will try to detect the types to match out of the type parameter of the class. + */ protected MessageToMessageEncoder() { matcher = TypeParameterMatcher.find(this, MessageToMessageEncoder.class, "I"); } + /** + * Create a new instance + * + * @param outboundMessageType The type of messages to match and so encode + */ protected MessageToMessageEncoder(Class outboundMessageType) { matcher = TypeParameterMatcher.get(outboundMessageType); } + /** + * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next + * {@link ChannelOutboundHandler} in the {@link ChannelPipeline}. + */ public boolean acceptOutboundMessage(Object msg) throws Exception { return matcher.match(msg); } diff --git a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java index cb118ff09a..639a58aecd 100644 --- a/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/ReplayingDecoder.java @@ -36,14 +36,14 @@ import java.util.List; * availability of the required bytes. For example, the following * {@link ByteToMessageDecoder} implementation: *
- * public class IntegerHeaderFrameDecoder extends {@link ByteToMessageDecoder}<{@link ByteBuf}> {
+ * public class IntegerHeaderFrameDecoder extends {@link ByteToMessageDecoder} {
  *
  *   {@code @Override}
- *   protected ByteBuf decode({@link ChannelHandlerContext} ctx,
- *                           {@link ByteBuf} in) throws Exception {
+ *   protected void decode({@link ChannelHandlerContext} ctx,
+ *                           {@link ByteBuf} in, List<Object> out) throws Exception {
  *
  *     if (in.readableBytes() < 4) {
- *        return null;
+ *        return;
  *     }
  *
  *     in.markReaderIndex();
@@ -51,22 +51,22 @@ import java.util.List;
  *
  *     if (in.readableBytes() < length) {
  *        in.resetReaderIndex();
- *        return null;
+ *        return;
  *     }
  *
- *     return in.readBytes(length);
+ *     out.add(in.readBytes(length));
  *   }
  * }
  * 
* is simplified like the following with {@link ReplayingDecoder}: *
  * public class IntegerHeaderFrameDecoder
- *      extends {@link ReplayingDecoder}<{@link ByteBuf},{@link Void}> {
+ *      extends {@link ReplayingDecoder}<{@link Void}> {
  *
- *   protected Object decode({@link ChannelHandlerContext} ctx,
+ *   protected void decode({@link ChannelHandlerContext} ctx,
  *                           {@link ByteBuf} buf) throws Exception {
  *
- *     return buf.readBytes(buf.readInt());
+ *     out.add(buf.readBytes(buf.readInt()));
  *   }
  * }
  * 
diff --git a/transport/src/main/java/io/netty/channel/SimpleChannelInboundHandler.java b/transport/src/main/java/io/netty/channel/SimpleChannelInboundHandler.java index 23c2ad8eb4..5b8df6e852 100644 --- a/transport/src/main/java/io/netty/channel/SimpleChannelInboundHandler.java +++ b/transport/src/main/java/io/netty/channel/SimpleChannelInboundHandler.java @@ -35,30 +35,55 @@ import io.netty.util.internal.TypeParameterMatcher; * } * * + * Be aware that depending of the constructor parameters it will release all handled messages. + * */ public abstract class SimpleChannelInboundHandler extends ChannelInboundHandlerAdapter { private final TypeParameterMatcher matcher; private final boolean autoRelease; + /** + * @see {@link #SimpleChannelInboundHandler(boolean)} with {@code true} as boolean parameter. + */ protected SimpleChannelInboundHandler() { this(true); } + /** + * Create a new instance which will try to detect the types to match out of the type parameter of the class. + * + * @param autoRelease {@code true} if handled messages should be released automatically by pass them to + * {@link ReferenceCountUtil#release(Object)}. + */ protected SimpleChannelInboundHandler(boolean autoRelease) { matcher = TypeParameterMatcher.find(this, SimpleChannelInboundHandler.class, "I"); this.autoRelease = autoRelease; } + /** + * @see {@link #SimpleChannelInboundHandler(Class, boolean)} with {@code true} as boolean value. + */ protected SimpleChannelInboundHandler(Class inboundMessageType) { this(inboundMessageType, true); } + /** + * Create a new instance + * + * @param inboundMessageType The type of messages to match + * @param autoRelease {@code true} if handled messages should be released automatically by pass them to + * {@link ReferenceCountUtil#release(Object)}. + */ protected SimpleChannelInboundHandler(Class inboundMessageType, boolean autoRelease) { matcher = TypeParameterMatcher.get(inboundMessageType); this.autoRelease = autoRelease; } + /** + * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next + * {@link ChannelInboundHandler} in the {@link ChannelPipeline}. + */ public boolean acceptInboundMessage(Object msg) throws Exception { return matcher.match(msg); } diff --git a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java index 58a6ac1936..662e1789b3 100644 --- a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java +++ b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java @@ -15,6 +15,7 @@ */ package io.netty.channel; +import io.netty.util.concurrent.EventExecutorGroup; import io.netty.util.concurrent.SingleThreadEventExecutor; import java.util.concurrent.ThreadFactory; @@ -25,6 +26,9 @@ import java.util.concurrent.ThreadFactory; */ public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop { + /** + * @see {@link SingleThreadEventExecutor#SingleThreadEventExecutor(EventExecutorGroup, ThreadFactory, boolean)} + */ protected SingleThreadEventLoop(EventLoopGroup parent, ThreadFactory threadFactory, boolean addTaskWakesUp) { super(parent, threadFactory, addTaskWakesUp); }