int getMessageInfoAndMetadataListSize() {
    int size = 0;
    if (version >= VERSION_5) {
      // version
      size += Short.BYTES;
    }
    // num elements in list
    size += Integer.BYTES;
    if (messageInfoList == null) {
      return size;
    }
    ListIterator<MessageInfo> infoListIterator = messageInfoList.listIterator();
    ListIterator<MessageMetadata> metadataListIterator = messageMetadataList.listIterator();
    while (infoListIterator.hasNext()) {
      MessageInfo messageInfo = infoListIterator.next();
      MessageMetadata messageMetadata = metadataListIterator.next();
      size += messageInfo.getStoreKey().sizeInBytes();
      // message size
      size += Long.BYTES;
      // expiration time
      size += Long.BYTES;
      // whether deleted
      size += Byte.BYTES;
      if (version < VERSION_1 || version > VERSION_MAX) {
        throw new IllegalArgumentException("Unknown version in MessageInfoList " + version);
      }
      if (version >= VERSION_5) {
        // whether ttl updated
        size += Byte.BYTES;
      }
      if (version >= VERSION_6) {
        // whether undelete
        size += Byte.BYTES;
      }
      if (version > VERSION_1) {
        // whether crc is present
        size += Byte.BYTES;
        if (messageInfo.getCrc() != null) {
          // crc
          size += Long.BYTES;
        }
      }
      if (version > VERSION_2) {
        // accountId
        size += Short.BYTES;
        // containerId
        size += Short.BYTES;
        // operationTime
        size += Long.BYTES;
      }
      if (version >= VERSION_6) {
        // lifeVersion
        size += Short.BYTES;
      }
      if (version > VERSION_3) {
        // whether message metadata is present.
        size += Byte.BYTES;
        if (messageMetadata != null) {
          size += messageMetadata.sizeInBytes();
        }
      }
    }
    return size;
  }
@return the size in bytes that serialization of this MessageInfoAndMetadataList will result in.
ambry-protocol/src/main/java/com/github/ambry/protocol/MessageInfoAndMetadataListSerde.java