code: @SuppressWarnings("null") // bytes will be null only if empty == true
    public byte[] toByteArray(final ArrayOfItemsSerDe<? super T> serDe, final Class<?> clazz) {
      final int preLongs, outBytes;
      final boolean empty = itemsSeen_ == 0;
      byte[] bytes = null; // for serialized items from serDe
  
      if (empty) {
        preLongs = 1;
        outBytes = 8;
      } else {
        preLongs = Family.RESERVOIR.getMaxPreLongs();
        bytes = serDe.serializeToByteArray(getSamples(clazz));
        outBytes = (preLongs << 3) + bytes.length;
      }
      final byte[] outArr = new byte[outBytes];
      final WritableMemory mem = WritableMemory.wrap(outArr);
  
      // Common header elements
      PreambleUtil.insertPreLongs(mem, preLongs);                  // Byte 0
      PreambleUtil.insertLgResizeFactor(mem, rf_.lg());
      PreambleUtil.insertSerVer(mem, SER_VER);                     // Byte 1
      PreambleUtil.insertFamilyID(mem, Family.RESERVOIR.getID());  // Byte 2
      if (empty) {
        PreambleUtil.insertFlags(mem, EMPTY_FLAG_MASK);            // Byte 3
      } else {
        PreambleUtil.insertFlags(mem, 0);
      }
      PreambleUtil.insertK(mem, reservoirSize_);                   // Bytes 4-7
  
      // conditional elements
      if (!empty) {
        PreambleUtil.insertN(mem, itemsSeen_);
  
        // insert the bytearray of serialized samples, offset by the preamble size
        final int preBytes = preLongs << 3;
        mem.putByteArray(preBytes, bytes, 0, bytes.length);
      }
  
      return outArr;
    }
docs: Returns a byte array representation of this sketch. Copies contents into an array of the
  specified class for serialization to allow for polymorphic types.
  
  @param serDe An instance of ArrayOfItemsSerDe
  @param clazz The class represented by &lt;T&gt;
  @return a byte array representation of this sketch