code: @SuppressWarnings("null") // gadgetBytes will be null only if gadget_ == null AND empty == true
    public byte[] toByteArray(final ArrayOfItemsSerDe<T> serDe, final Class<?> clazz) {
      final int preLongs, outBytes;
      final boolean empty = gadget_ == null;
      final byte[] gadgetBytes = (gadget_ != null ? gadget_.toByteArray(serDe, clazz) : null);
  
      if (empty) {
        preLongs = Family.RESERVOIR_UNION.getMinPreLongs();
        outBytes = 8;
      } else {
        preLongs = Family.RESERVOIR_UNION.getMaxPreLongs();
        outBytes = (preLongs << 3) + gadgetBytes.length; // for longs, we know the size
      }
      final byte[] outArr = new byte[outBytes];
      final WritableMemory mem = WritableMemory.wrap(outArr);
  
      // build preLong
      PreambleUtil.insertPreLongs(mem, preLongs);                       // Byte 0
      PreambleUtil.insertSerVer(mem, SER_VER);                          // Byte 1
      PreambleUtil.insertFamilyID(mem, Family.RESERVOIR_UNION.getID()); // Byte 2
      if (empty) {
        PreambleUtil.insertFlags(mem, EMPTY_FLAG_MASK);
      } else {
        PreambleUtil.insertFlags(mem, 0);                               // Byte 3
      }
      PreambleUtil.insertMaxK(mem, maxK_);                              // Bytes 4-5
  
      if (!empty) {
        final int preBytes = preLongs << 3;
        mem.putByteArray(preBytes, gadgetBytes, 0, gadgetBytes.length);
      }
  
      return outArr;
    }
docs: Returns a byte array representation of this union. This method should be used when the array
  elements are subclasses of a common base class.
  
  @param serDe An instance of ArrayOfItemsSerDe
  @param clazz A class to which the items are cast before serialization
  @return a byte array representation of this union