/// <summary> /// Convenient method to set the content type. /// Set first content type 1, if this is set (not empty), then set the second, etc. /// </summary> /// <param name="contentType">The content type to set.</param> /// <returns>This class.</returns> public Message SetContentType(Content contentType) { for (int i = 0, reference = 0; i < ContentTypeLength; i++) { if (ContentTypes[i] == Content.Empty) // TODO check if this works as expected (cf. java tests for null) { if (contentType == Content.PublicKeySignature && i != 0) { throw new InvalidOperationException("The public key needs to be the first to be set."); } ContentTypes[i] = contentType; ContentReferences.Enqueue(new MessageContentIndex(reference, contentType)); return(this); } if (ContentTypes[i] == contentType) { reference++; } else if (ContentTypes[i] == Content.PublicKeySignature || ContentTypes[i] == Content.PublicKey) { // special handling for public key, as we store both in the same list if (contentType == Content.PublicKeySignature || contentType == Content.PublicKey) { reference++; } } } throw new InvalidOperationException("Already set 8 content types."); }
// TODO check comment, if only vs. only if /// <summary> /// Restore the content references if only the content types array is /// present. The content references are removed when decoding a message. That /// means if a message was received it cannot be used a second time as the /// content references are not there anymore. This method restores the /// content references based on the content types of the message. /// </summary> public void RestoreContentReferences() { IDictionary <Content, int> references = new Dictionary <Content, int>(ContentTypes.Count() * 2); foreach (var contentType in ContentTypes) { // TODO what about Content.Undefined if (contentType == Content.Empty) { return; } int index = 0; if (contentType == Content.PublicKeySignature || contentType == Content.PublicKey) { int j = references[Content.PublicKeySignature]; if (j != default(int)) { index = j; } else { j = references[Content.PublicKey]; if (j != default(int)) { index = j; } } } if (!references.ContainsKey(contentType)) { references.Add(contentType, index); } else { index = references[contentType]; } ContentReferences.Enqueue(new MessageContentIndex(index, contentType)); references.Add(contentType, index + 1); } }