title: Why am I losing exactly 5 bytes during decoding in my netty pipeline?
body: <p>I'm writing a netty pipeline for a protocol which is able to transfer files and structured messages. File transfers are initiated with a structured message (handshake) followed by a stream of bytes representing the file.</p>
  
  <p>The exact message flow for an incoming file looks like this (server being my netty implementation, client another software):</p>
  
  <pre><code>            +---------+                                    +---------+
              | Client  |                                    | Server  |
              +---------+                                    +---------+
                   |                                              |
                   | Connect (1)                                  |
                   |---------------------------------------------&gt;|
                   |                                              |
                   | Handshake to announce incoming file (2)      |
                   |---------------------------------------------&gt;|
                   |                                              |
                   |                Acknowledge file transfer (3) |
                   |&lt;---------------------------------------------|
                   |                                              |
                   | Send file (4)                                |
                   |---------------------------------------------&gt;|
  </code></pre>
  
  <p>The protocol message looks like this:</p>
  
  <pre><code>            +---------+----------------+----------------+
              | Length  |      Type      | Actual Message |
              | 4 bytes |     1 byte     |   N bytes      |
              +---------+----------------+----------------+
  </code></pre>
  
  <p>In the case of the handshake message, the <code>Actual Message</code> only consists of a single <code>Long</code> value, the <code>token</code>.</p>
  
  <p>Here's the ReplayingDecoder:</p>
  
  <pre><code>    public class ProtocolDecoder extends ReplayingDecoder&lt;State&gt; {
  
        private Integer msgType;
        private Long token;
  
  
        public enum State {
          LENGTH,
          MSG_TYPE,
          TOKEN
        }
  
        public ProtocolDecoder() {
          super(State.LENGTH);
        }
  
        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List&lt;Object&gt; out) throws Exception {
  
          switch (state()) {
  
            case LENGTH:
              // (2) read the message length from the handshake
              long messageLength = in.readUnsignedIntLE();
              checkpoint(State.MSG_TYPE);
  
            case MSG_TYPE:
              // (2) read the message type from the handshake
              msgType = in.readBoolean();
              checkpoint(State.TOKEN);
  
            case TOKEN:
              try {
                // (2) read the token from the handshake
                token = in.readUnsignedIntLE();
                // (3) write back the acknowledgement
                ctx.channel().writeAndFlush(new Acknowledgement(token));
                // (4) done reading the protocol message
                // now switch to the file protocol
                ctx.pipeline().addLast(new FileInboundHandler());
                // write everything that is still in the buffer to the
                // modified pipeline
                ByteBuf rest = in.readBytes(super.actualReadableBytes());
                out.add(rest);
                // remove the protocol handshake decoder and pass
                // the rest of this channel to the `FileInboundHandler`
                ctx.pipeline().remove(this);
              } finally {
                reset();
              }
              break;
            default:
              throw new Error("Shouldn't reach here.");
          }
        }
  
        private void reset() {
          token = null;
          msgType = null;
        }
  </code></pre>
  
  <p>The <code>FileInboundHandler</code> simply creates a file and writes all <code>ByteBuf</code>s to it. </p>
  
  <p>This is working in principle, with the only problem being that each file misses exactly 5 bytes at the beginning.</p>
  
  <p>I have 2 questions:</p>
  
  <p>1) If I put a <code>LoggingHandler</code> as the first handler in the pipeline, can I be sure that all traffic on the socket is logged, no matter if my decoders are buggy?</p>
  
  <p>2) When calling <code>ctx.channel().writeAndFlush()</code>, does it flush only the "outbound" buffer, meaning I can be sure I'm not flushing any bytes I have not consumed from the inbound buffer yet?</p>