/**
   * Parse a message from the server stream.
   *
   * @return the parsed Message
   * @throws IOException
   */
  private EmailContent.Message addParser(final int endingTag)
      throws IOException, CommandStatusException {
    EmailContent.Message msg = new EmailContent.Message();
    msg.mAccountKey = mAccount.mId;
    msg.mMailboxKey = mMailbox.mId;
    msg.mFlagLoaded = EmailContent.Message.FLAG_LOADED_COMPLETE;
    // Default to 1 (success) in case we don't get this tag
    int status = 1;

    while (nextTag(endingTag) != END) {
      switch (tag) {
        case Tags.SYNC_SERVER_ID:
          msg.mServerId = getValue();
          LogUtils.d(TAG, "ServerId: %s", msg.mServerId);
          break;
        case Tags.SYNC_STATUS:
          status = getValueInt();
          break;
        case Tags.SYNC_APPLICATION_DATA:
          addData(msg, tag);
          break;
        default:
          skipTag();
      }
    }
    // For sync, status 1 = success
    if (status != 1) {
      throw new CommandStatusException(status, msg.mServerId);
    }
    return msg;
  }
  /**
   * Copy field-by-field from a "store" message to a "provider" message
   *
   * @param message The message we've just downloaded (must be a MimeMessage)
   * @param localMessage The message we'd like to write into the DB
   * @return true if dirty (changes were made)
   */
  public static boolean updateMessageFields(
      final EmailContent.Message localMessage,
      final Message message,
      final long accountId,
      final long mailboxId)
      throws MessagingException {

    final Address[] from = message.getFrom();
    final Address[] to = message.getRecipients(Message.RecipientType.TO);
    final Address[] cc = message.getRecipients(Message.RecipientType.CC);
    final Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
    final Address[] replyTo = message.getReplyTo();
    final String subject = message.getSubject();
    final Date sentDate = message.getSentDate();
    final Date internalDate = message.getInternalDate();

    if (from != null && from.length > 0) {
      localMessage.mDisplayName = from[0].toFriendly();
    }
    if (sentDate != null) {
      localMessage.mTimeStamp = sentDate.getTime();
    } else if (internalDate != null) {
      LogUtils.w(Logging.LOG_TAG, "No sentDate, falling back to internalDate");
      localMessage.mTimeStamp = internalDate.getTime();
    }
    if (subject != null) {
      localMessage.mSubject = subject;
    }
    localMessage.mFlagRead = message.isSet(Flag.SEEN);
    if (message.isSet(Flag.ANSWERED)) {
      localMessage.mFlags |= EmailContent.Message.FLAG_REPLIED_TO;
    }

    // Keep the message in the "unloaded" state until it has (at least) a display name.
    // This prevents early flickering of empty messages in POP download.
    if (localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE) {
      if (localMessage.mDisplayName == null || "".equals(localMessage.mDisplayName)) {
        localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_UNLOADED;
      } else {
        localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_PARTIAL;
      }
    }
    localMessage.mFlagFavorite = message.isSet(Flag.FLAGGED);
    //        public boolean mFlagAttachment = false;
    //        public int mFlags = 0;

    localMessage.mServerId = message.getUid();
    if (internalDate != null) {
      localMessage.mServerTimeStamp = internalDate.getTime();
    }
    //        public String mClientId;

    // Only replace the local message-id if a new one was found.  This is seen in some ISP's
    // which may deliver messages w/o a message-id header.
    final String messageId = message.getMessageId();
    if (messageId != null) {
      localMessage.mMessageId = messageId;
    }

    //        public long mBodyKey;
    localMessage.mMailboxKey = mailboxId;
    localMessage.mAccountKey = accountId;

    if (from != null && from.length > 0) {
      localMessage.mFrom = Address.toString(from);
    }

    localMessage.mTo = Address.toString(to);
    localMessage.mCc = Address.toString(cc);
    localMessage.mBcc = Address.toString(bcc);
    localMessage.mReplyTo = Address.toString(replyTo);

    //        public String mText;
    //        public String mHtml;
    //        public String mTextReply;
    //        public String mHtmlReply;

    //        // Can be used while building messages, but is NOT saved by the Provider
    //        transient public ArrayList<Attachment> mAttachments = null;

    return true;
  }