docs: @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_.getNumSamples() == 0;
      final byte[] gadgetBytes = (empty ? null : gadget_.toByteArray(serDe, clazz));
  
      if (empty) {
        preLongs = Family.VAROPT_UNION.getMinPreLongs();
        outBytes = 8;
      } else {
        preLongs = Family.VAROPT_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.VAROPT_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-7
  
      if (!empty) {
        PreambleUtil.insertN(mem, n_);                               // Bytes 8-15
        PreambleUtil.insertOuterTauNumerator(mem, outerTauNumer);    // Bytes 16-23
        PreambleUtil.insertOuterTauDenominator(mem, outerTauDenom);  // Bytes 24-31
  
        final int preBytes = preLongs << 3;
        mem.putByteArray(preBytes, gadgetBytes, 0, gadgetBytes.length);
      }
  
      return outArr;
    }