public int completeByteStreamGeneration() {
    if (!alreadyGenerated) {
      alreadyGenerated = true;
      if (!fieldsWritten()) {
        initialize();
      }
      writeUnreadData();
      appendOffsets();
      int typeId;
      if (definingNewPdxType()) {
        newType.initialize(this);
        if (unreadData != null && !unreadData.isEmpty()) {
          // We created a new type that had unreadData.
          // In this case we don't define a local type
          // but we do set the serialized type.
          newType = tr.defineType(newType);
          typeId = newType.getTypeId();
          unreadData.setSerializedType(newType);
        } else {
          newType = tr.defineLocalType(pdx, newType);
          typeId = newType.getTypeId();
        }
      } else {
        if (doExtraValidation()) {
          int fieldCount = fieldId + 1;
          if (existingType.getFieldCount() != fieldCount) {
            throw new PdxSerializationException("Expected the number of fields for class "
                + existingType.getClassName() + " to be " + existingType.getFieldCount()
                + " but instead it was " + fieldCount);
          }
        }
        typeId = existingType.getTypeId();
      }

      // Now write length of the byte stream (does not include bytes for DSCODE and the length
      // itself.)
      long bits = ((long) getCurrentOffset()) << 32 | (0x00000000FFFFFFFFL & typeId); // fixes 45005
      lu.update(bits);
    } // !alreadyGenerated

    return getCurrentOffset() + 1; // +1 for DSCODE.PDX.toByte()
  }
Must be invoked only after {@link PdxSerializable#toData(PdxWriter)}

@return total number of bytes serialized for this pdx
geode-core/src/main/java/org/apache/geode/pdx/internal/PdxWriterImpl.java