From 30bcc72b4409fefd5075bf39420a6690e5a2d419 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 11 Feb 2013 14:16:45 +0100 Subject: [PATCH] [#1038] Remove ChannelHandlerContext.replace*Buffer() methods --- .../netty/channel/ChannelHandlerContext.java | 68 --------- .../channel/DefaultChannelHandlerContext.java | 131 +----------------- 2 files changed, 4 insertions(+), 195 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/ChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/ChannelHandlerContext.java index 7a3935d2c4..32e70e5578 100755 --- a/transport/src/main/java/io/netty/channel/ChannelHandlerContext.java +++ b/transport/src/main/java/io/netty/channel/ChannelHandlerContext.java @@ -224,74 +224,6 @@ public interface ChannelHandlerContext */ MessageBuf outboundMessageBuffer(); - /** - * Replaces the inbound byte buffer with the given buffer. This returns the - * old buffer, so any readable bytes can be handled appropriately by the caller. - *

- * Be cautious with caching {@link #inboundByteBuffer()} as it may change as a result of this - * method. For example, instead of extending {@link io.netty.handler.codec.ByteToMessageDecoder}, - * extend what that class does (currently, {@link ChannelInboundHandlerAdapter} and - * {@link ChannelInboundByteHandler}. In other words, implementing your own - * {@link ChannelInboundHandlerAdapter#inboundBufferUpdated}/{@link ChannelStateHandler#inboundBufferUpdated} - * will help guarantee a replaced buffer won't be missed.

- * - * @param newInboundByteBuf the new inbound byte buffer - * @return the old buffer. - * @throws NullPointerException if the argument is {@code null}. - */ - ByteBuf replaceInboundByteBuffer(ByteBuf newInboundByteBuf); - - /** - * Replaces the inbound message buffer with the given buffer. This returns the - * old buffer, so any pending messages can be handled appropriately by the caller. - *

- * Be cautious with caching {@link #inboundMessageBuffer()} as it may change as a result of this - * method. For example, instead of extending {@link io.netty.handler.codec.MessageToMessageDecoder}, - * extend what that class does (currently, {@link ChannelInboundHandlerAdapter} and - * {@link ChannelInboundMessageHandler}. In other words, implementing your own - * {@link ChannelInboundHandlerAdapter#inboundBufferUpdated}/{@link ChannelStateHandler#inboundBufferUpdated} - * will help guarantee a replaced buffer won't be missed.

- * - * @param newInboundMsgBuf the new inbound message buffer - * @return the old buffer. - * @throws NullPointerException if the argument is {@code null}. - */ - MessageBuf replaceInboundMessageBuffer(MessageBuf newInboundMsgBuf); - - /** - * Replaces the outbound byte buffer with the given buffer. This returns the - * old buffer, so any readable bytes can be handled appropriately by the caller. - *

- * Be cautious with caching {@link #outboundByteBuffer()} as it may change as a result of this - * method. For example, instead of extending {@link io.netty.handler.codec.ByteToByteEncoder}, - * extend what that class does (currently, {@link ChannelOutboundByteHandlerAdapter}). - * In other words, implementing your own - * {@link ChannelOutboundHandlerAdapter#flush}/{@link ChannelOperationHandler#flush} - * will help guarantee a replaced buffer won't be missed.

- * - * @param newOutboundByteBuf the new inbound byte buffer - * @return the old buffer. - * @throws NullPointerException if the argument is {@code null}. - */ - ByteBuf replaceOutboundByteBuffer(ByteBuf newOutboundByteBuf); - - /** - * Replaces the outbound message buffer with the given buffer. This returns the - * old buffer, so any pending messages can be handled appropriately by the caller. - *

- * Be cautious with caching {@link #outboundMessageBuffer()} as it may change as a result of this - * method. For example, instead of extending {@link io.netty.handler.codec.MessageToByteEncoder} - * or {@link io.netty.handler.codec.MessageToMessageEncoder}, extend what these classes do (currently, - * {@link ChannelOutboundMessageHandlerAdapter}. In other words, implementing your own - * {@link ChannelOutboundHandlerAdapter#flush}/{@link ChannelOperationHandler#flush} - * will help guarantee a replaced buffer won't be missed.

- * - * @param newOutboundMsgBuf the new inbound message buffer - * @return the old buffer. - * @throws NullPointerException if the argument is {@code null}. - */ - MessageBuf replaceOutboundMessageBuffer(MessageBuf newOutboundMsgBuf); - /** * Return the {@link ByteBuf} of the next {@link ChannelHandlerContext}. */ diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java index 6b76f845c5..652297e564 100755 --- a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java @@ -28,7 +28,6 @@ import java.util.Collections; import java.util.EnumSet; import java.util.Queue; import java.util.Set; -import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -160,8 +159,10 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements if (buf instanceof ByteBuf) { inByteBuf = (ByteBuf) buf; + inMsgBuf = null; } else if (buf instanceof MessageBuf) { inMsgBuf = (MessageBuf) buf; + inByteBuf = null; } else { throw new ChannelPipelineException( handler.getClass().getSimpleName() + ".newInboundBuffer() returned neither " + @@ -211,6 +212,8 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements executor = null; inByteBuf = handler.byteSink; inMsgBuf = handler.msgSink; + outByteBuf = null; + outMsgBuf = null; } void forwardBufferContent() { @@ -478,30 +481,6 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements return (MessageBuf) outMsgBuf; } - /** - * Executes a task on the event loop and waits for it to finish. If the task is interrupted, then the - * current thread will be interrupted and this will return {@code null}. It is expected that the task - * performs any appropriate locking. - *

- * If the {@link Callable#call()} call throws a {@link Throwable}, but it is not an instance of - * {@link Error}, {@link RuntimeException}, or {@link Exception}, then it is wrapped inside an - * {@link AssertionError} and that is thrown instead.

- * - * @param c execute this callable and return its value - * @param the return value type - * @return the task's return value, or {@code null} if the task was interrupted. - * @see Callable#call() - * @see Future#get() - * @throws Error if the task threw this. - * @throws RuntimeException if the task threw this. - * @throws Exception if the task threw this. - * @throws ChannelPipelineException with a {@link Throwable} as a cause, if the task threw another type of - * {@link Throwable}. - */ - private T executeOnEventLoop(Callable c) throws Exception { - return getFromFuture(executor().submit(c)); - } - /** * Executes a task on the event loop and waits for it to finish. If the task is interrupted, then the * current thread will be interrupted. It is expected that the task performs any appropriate locking. @@ -592,108 +571,6 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements } } - @Override - public ByteBuf replaceInboundByteBuffer(final ByteBuf newInboundByteBuf) { - if (newInboundByteBuf == null) { - throw new NullPointerException("newInboundByteBuf"); - } - - if (!executor().inEventLoop()) { - try { - return executeOnEventLoop(new Callable() { - @Override - public ByteBuf call() { - return replaceInboundByteBuffer(newInboundByteBuf); - } - }); - } catch (Exception ex) { - throw new ChannelPipelineException("failed to replace an inbound byte buffer", ex); - } - } - - ByteBuf currentInboundByteBuf = inboundByteBuffer(); - - inByteBuf = newInboundByteBuf; - return currentInboundByteBuf; - } - - @Override - @SuppressWarnings("unchecked") - public MessageBuf replaceInboundMessageBuffer(final MessageBuf newInboundMsgBuf) { - if (newInboundMsgBuf == null) { - throw new NullPointerException("newInboundMsgBuf"); - } - - if (!executor().inEventLoop()) { - try { - return executeOnEventLoop(new Callable>() { - @Override - public MessageBuf call() { - return replaceInboundMessageBuffer(newInboundMsgBuf); - } - }); - } catch (Exception ex) { - throw new ChannelPipelineException("failed to replace an inbound message buffer", ex); - } - } - - MessageBuf currentInboundMsgBuf = inboundMessageBuffer(); - - inMsgBuf = (MessageBuf) newInboundMsgBuf; - return currentInboundMsgBuf; - } - - @Override - public ByteBuf replaceOutboundByteBuffer(final ByteBuf newOutboundByteBuf) { - if (newOutboundByteBuf == null) { - throw new NullPointerException("newOutboundByteBuf"); - } - - if (!executor().inEventLoop()) { - try { - return executeOnEventLoop(new Callable() { - @Override - public ByteBuf call() { - return replaceOutboundByteBuffer(newOutboundByteBuf); - } - }); - } catch (Exception ex) { - throw new ChannelPipelineException("failed to replace an outbound byte buffer", ex); - } - } - - ByteBuf currentOutboundByteBuf = outboundByteBuffer(); - - outByteBuf = newOutboundByteBuf; - return currentOutboundByteBuf; - } - - @Override - @SuppressWarnings("unchecked") - public MessageBuf replaceOutboundMessageBuffer(final MessageBuf newOutboundMsgBuf) { - if (newOutboundMsgBuf == null) { - throw new NullPointerException("newOutboundMsgBuf"); - } - - if (!executor().inEventLoop()) { - try { - return executeOnEventLoop(new Callable>() { - @Override - public MessageBuf call() { - return replaceOutboundMessageBuffer(newOutboundMsgBuf); - } - }); - } catch (Exception ex) { - throw new ChannelPipelineException("failed to replace an outbound message buffer", ex); - } - } - - MessageBuf currentOutboundMsgBuf = outboundMessageBuffer(); - - outMsgBuf = (MessageBuf) newOutboundMsgBuf; - return currentOutboundMsgBuf; - } - @Override public ByteBuf nextInboundByteBuffer() { DefaultChannelHandlerContext ctx = next;