code: public static ByteBuffer expand(ByteBuffer buffer, int newCapacity) {
          if(newCapacity < buffer.capacity())
              throw new IllegalArgumentException("newCapacity (" + newCapacity
                                                 + ") must be larger than existing capacity ("
                                                 + buffer.capacity() + ")");
  
          ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
          int position = buffer.position();
          buffer.rewind();
          newBuffer.put(buffer);
          newBuffer.position(position);
          return newBuffer;
      }
docs: If we have no more room in the current buffer, then double our capacity
  and copy the current buffer to the new one.
  <p/>
  Note: the new buffer is allocated using ByteBuffer.allocate.
  
  @param buffer The buffer from which to copy the contents
  @param newCapacity The new capacity size