code: public byte[] toArray(int offset, int length, int capacity) {
          // validate the offset, length are ok
          ByteBuffer.checkOffsetLength(size(), offset, length);
          
          // will we have a large enough byte[] allocated?
          if (capacity < length) {
              throw new IllegalArgumentException("Capacity must be large enough to hold a byte[] of at least a size=" + length);
          }
  
          byte[] arrayCopy = new byte[capacity];
          this.toArray(offset, length, arrayCopy, 0);
          return arrayCopy;
      }
docs: Gets a copy of the current buffer as byte array, but the new byte[]
  has the specified capacity.  Useful if you need to store additional bytes
  in the returned byte[] and dont' want to do an additional System.arraycopy()
  afterwards. Method will allocate memory to hold a copy of the current array
  and return it.
  @param offset The offset to start from
  @param length The length from the offset
  @param capacity The size of the new byte[]. Must be >= this buffer's size()
  @return A byte array. Could be empty.
  @throws IllegalArgumentException If capacity isn't large enough enough
  to hold the new byte[]