code: public static <T, U extends T> int safeWrite(int num, ArrayWritable<U> aw, T[] array) {
  
          final int elementsToCopy = Math.min(num, Math.min(aw.size(), array.length));
  
          if (elementsToCopy <= 0) {
              return 0;
          }
  
          aw.writeToArray(0, array, 0, elementsToCopy);
          return elementsToCopy;
      }
docs: Writes a given maximum amount of data items from a container to an array. This method ensures that the array's
  capacity is not exceeded.
  
  @param num
  the number of elements to copy
  @param aw
  the container.
  @param array
  the array
  
  @return the number of elements copied