code: @Override
    public long transferTo(final WritableByteChannel target, final long position) throws IOException {
      Preconditions.checkArgument(position == totalBytesTransferred, "Invalid position.");
      // Bytes written for header in this call.
      long writtenHeader = 0;
      if (header.readableBytes() > 0) {
        writtenHeader = copyByteBuf(header, target);
        totalBytesTransferred += writtenHeader;
        if (header.readableBytes() > 0) {
          return writtenHeader;
        }
      }
  
      // Bytes written for body in this call.
      long writtenBody = 0;
      if (body instanceof FileRegion) {
        writtenBody = ((FileRegion) body).transferTo(target, totalBytesTransferred - headerLength);
      } else if (body instanceof ByteBuf) {
        writtenBody = copyByteBuf((ByteBuf) body, target);
      }
      totalBytesTransferred += writtenBody;
  
      return writtenHeader + writtenBody;
    }
docs: This code is more complicated than you would think because we might require multiple
  transferTo invocations in order to transfer a single MessageWithHeader to avoid busy waiting.
  
  The contract is that the caller will ensure position is properly set to the total number
  of bytes transferred so far (i.e. value returned by transferred()).