code: public static int flipToFill(ByteBuffer buffer) {
          int position = buffer.position();
          int limit = buffer.limit();
          if (position == limit) {
              buffer.position(0);
              buffer.limit(buffer.capacity());
              return 0;
          }
  
          int capacity = buffer.capacity();
          if (limit == capacity) {
              buffer.compact();
              return 0;
          }
  
          buffer.position(limit);
          buffer.limit(capacity);
          return position;
      }
docs: Flip the buffer to fill mode.
  The position is set to the first unused position in the buffer
  (the old limit) and the limit is set to the capacity.
  If the buffer is empty, then this call is effectively {@link #clearToFill(ByteBuffer)}.
  If there is no unused space to fill, a {@link ByteBuffer#compact()} is done to attempt
  to create space.
  <p>
  This method is used as a replacement to {@link ByteBuffer#compact()}.
  
  @param buffer The buffer to flip
  @return The position of the valid data before the flipped position. This value should be
  passed to a subsequent call to {@link #flipToFlush(ByteBuffer, int)}