title: Java Buffer - Cannot send object with length bigger than 20kb
body: <p>I don't know why, but cannot send objects with length bigger than 20,000 bytes. I like length of buffer is lower than remaining. How can I fix that? Using Grizzly and Ambassador libraries. While sending small values everything is ok.</p>
  
  <p>System outputs</p>
  
  <ol>
  <li>[05:44:04 INFO]: 14480</li>
  <li>[05:44:04 INFO]: STOPED3   14480 &lt; 20636</li>
  <li>[05:44:04 INFO]: 20557</li>
  <li>[05:44:04 INFO]: STOPED2   20557 &lt; 1229410144</li>
  </ol>
  
  <p>My Code:</p>
  
  <pre><code>private static final int HEADER_SIZE = 12;
  
  @Override
  public NextAction handleRead(final FilterChainContext ctx) throws IOException {
      final Buffer buffer = ctx.getMessage();
      System.out.println(buffer.limit());
      final int total = buffer.remaining();
      if (total &lt; HEADER_SIZE) {
          System.out.println("STOPED1 "+"  "+total+" &lt; "+HEADER_SIZE);
          return ctx.getStopAction(buffer);
      }
      buffer.get();
      buffer.get();
      buffer.get();
      buffer.get();
      int len = buffer.getInt();
      if (total &lt; HEADER_SIZE + len) {
          System.out.println("STOPED2 "+"  "+total+" &lt; "+(HEADER_SIZE+len));
          return ctx.getStopAction(buffer);
      }
      byte[] pack = new byte[len];
      buffer.get(pack);
      String name = new String(pack);
      int size = buffer.getInt();
      final int completeMessageLength = HEADER_SIZE + size + len;
      if (total &lt; completeMessageLength) {
          System.out.println("STOPED3 "+"  "+total+" &lt; "+completeMessageLength);
          return ctx.getStopAction(buffer);
      }
      final Buffer remainder = total &gt; completeMessageLength ? buffer.split(completeMessageLength) : null;
      System.out.println("PACKET[IN] Name: " + name + " Size: " + total + " BodySize: " + buffer.remaining());
      byte[] body = new byte[size];
      buffer.get(body);
      Packet packet = null;
      try {
          packet = getPacket(name);
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      }
      if (packet == null) {
          return ctx.getInvokeAction(remainder);
      }
      packet.read(body);
      handlePacket(ctx.getConnection(), packet);
      return ctx.getInvokeAction(remainder);
  }
  
  @Override
  public NextAction handleWrite(final FilterChainContext ctx) throws IOException {
      Packet packet = ctx.getMessage();
      String name = packet.getClass().getName();
      byte[] packetBody = name.getBytes();
      byte[] body = packet.write();
      int bodyLen = body.length;
      int packetLen = packetBody.length;
      final MemoryManager memoryManager = ctx.getConnection().getTransport().getMemoryManager();
      int size = HEADER_SIZE + bodyLen + packetLen;
      final Buffer output = memoryManager.allocate(size);
      output.allowBufferDispose(true);
      output.put((byte) 0);
      output.put((byte) 0);
      output.put((byte) 0);
      output.put((byte) 0);
      output.putInt(packetLen);
      output.put(packetBody);
      output.putInt(bodyLen);
      output.put(body);
      ctx.setMessage(output.flip());
      System.out.println("PACKET[OUT] ID: " + name + " Size: " + size);
      return ctx.getInvokeAction();
  }
  
  protected static Packet getPacket(String name) throws IllegalAccessException {
      try {
          Class cl = Class.forName(name);
          return (Packet) cl.newInstance();
      } catch (ClassNotFoundException | InstantiationException e) {
          e.printStackTrace();
      }
      return null;
  }
  
  
  protected abstract void handlePacket(Connection connection, Packet packet);
  </code></pre>