public void writeTo(MixedStream st) { // important to access attachments before header is being BUILT: // attachment positions will be initialized IOrderedEnumerable <Attachment> attachmentList = getAttachments(); string headerString = JsonConvert.SerializeObject(getHeader()); byte[] headerBytes = Encoding.UTF8.GetBytes(headerString); byte[] lenHeader = BitConverter.GetBytes((long)headerBytes.Length); byte[] numAttachments = BitConverter.GetBytes((long)this.numAttachments()); long attachmentLen = this.lenAttachments() + (this.numAttachments() + 1) * 8; byte[] lenAttachments = BitConverter.GetBytes((long)attachmentLen); if (BitConverter.IsLittleEndian) { Array.Reverse(lenHeader); Array.Reverse(lenAttachments); Array.Reverse(numAttachments); } st.Write(lenHeader); st.Flush(); st.Write(lenAttachments); st.Flush(); st.Write(headerBytes); st.Flush(); st.Write(numAttachments); st.Flush(); if (this.numAttachments() > 0) { foreach (Attachment attachment in attachmentList) { byte[] lenAttachment = BitConverter.GetBytes((long)attachment.getLength()); if (BitConverter.IsLittleEndian) { Array.Reverse(lenAttachment); } st.Write(lenAttachment); st.Flush(); attachment.write(st); st.Flush(); } } }
public override void write(MixedStream st) { st.Write(data); }