title: Netty: Weird IndexOutOfBoundsException: readerIndex + length exceeds writerIndex
body: <p>I am currently sending different packets through netty and I'm very often getting exceptions like these <strong>when receiving them</strong>:</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: readerIndex(39) + length(32) exceeds writerIndex(64): UnpooledUnsafeDirectByteBuf(ridx: 39, widx: 64, cap: 2048)
  at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1166)
  at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:655) 
  at &lt;snip&gt;.PacketDataHolder.readString(PacketDataHolder.java:79)
  </code></pre>
  
  <p>......
  or</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: readerIndex(0) + length(1) exceeds writerIndex(0): UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 2048)
  at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1166)
  at io.netty.buffer.AbstractByteBuf.readByte(AbstractByteBuf.java:570) ......
  </code></pre>
  
  <p>Here's how I send packets:</p>
  
  <pre><code>public void sendPacket(Packet packet) {
      PacketDataHolder packetDataHolder = new PacketDataHolder(); //I'm creating a packetDataHolder, code is below
      packetDataHolder.writeHeader(packet); //Then i'm adding the packet ID as a header
      packet.writeData(packetDataHolder); //Then I'm writing the data of the packet (String for example)
      socketChannel.writeAndFlush(packetDataHolder.getByteBuf()); //Then I'm sending the packet
  }
  </code></pre>
  
  <p>And how I receive and read them:</p>
  
  <pre><code>protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
      PacketDataHolder packetDataHolder = new PacketDataHolder(byteBuf); //First I'm creating a PacketDataHolder containing the bytebuf
      Packet packet = PacketParser.parsePacket(packetDataHolder); //Then I'm parsing the header I wrote (The packet id)
  </code></pre>
  
  <p>Here's related code:</p>
  
  <p>(PacketDataHolder class)
      public synchronized void writeHeader(Packet packet) {
          this.writeByte(packet.getId());
      }</p>
  
  <pre><code>public synchronized byte readHeader() {
      return this.readByte();
  }
  public synchronized void writeByte(int value) {
      this.byteBuf.writeByte(value);
  }
  
  public synchronized byte readByte() {
      return this.byteBuf.readByte();
  }
  public synchronized void writeString(String value) {
      if(value == null)
          value = "An error occurred while displaying this message";
      byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
      if (bytes.length &gt; 32767)
          throw new EncoderException("String too big (was " + value.length()
                  + " bytes encoded, max 32767)");
      else {
          writeVarInt(bytes.length);
          byteBuf.writeBytes(bytes);
      }
  }
  
  public synchronized String readString() {
      int length = readVarInt();
      if (length &lt; 0)
          throw new DecoderException(
                  "The received encoded string buffer length is less than zero! Weird string!");
      return new String(byteBuf.readBytes(length).array(),
              StandardCharsets.UTF_8);
  }
  </code></pre>
  
  <p>The first error I described always happens at the writeString method, so I think in any way it's related to that. 
  I think it's mostly when very long strings are sent, but I'm not sure.</p>
  
  <p>If you need more example code feel free to ask!
  I hope you can help me,
  Kombustor</p>