title: How do I correctly work with ByteToMessageCodec.encode and direct buffers
body: <p>I use a <a href="https://netty.io/4.1/api/io/netty/handler/codec/ByteToMessageCodec.html" rel="nofollow noreferrer">ByteToMessageCodec</a> in my netty project. The encode has the following structure:</p>
  
  <pre class="lang-java prettyprint-override"><code>protected abstract void encode(
      ChannelHandlerContext ctx,
      I msg,
      ByteBuf out
  ) throws Exception
  </code></pre>
  
  <p>In my case the <code>msg</code> is a message that holds a directBuffer that came from another channel and is to be forwarded.</p>
  
  <p>My Questions:</p>
  
  <ul>
  <li>What is the best practice if <code>out.isWritable()</code> is false?</li>
  <li>Does <code>out.writeBytes(myMessage.directBuffer)</code> copy the memory if <code>out</code> is also a direct buffer?</li>
  <li>If it does copy the memory, can I just call <code>ctx.writeAndFlush(myMessage.directBuffer)</code> to prevent the memory copy?</li>
  </ul>
  
  <p>There is a <a href="http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#32.0" rel="nofollow noreferrer">source by norman maurer</a> telling something about this exact topic, but it seems to memory copy the buffers and doesn't answer all my questions.</p>