using System; using System.IO; using System.Collections.Generic; using System.Text; using LumiSoft.Misc.IO; namespace LumiSoft.Misc.MIME { /// /// This class represents MIME text/xxx bodies. Defined in RFC 2045. /// /// /// The "text" media type is intended for sending material which is principally textual in form. /// public class MIME_b_Text : MIME_b_SinglepartBase { /// /// Default constructor. /// /// MIME media type. /// Is raised when mediaSubType is null reference. /// Is raised when any of the arguments has invalid value. public MIME_b_Text(string mediaType) : base(new MIME_h_ContentType(mediaType)) { } #region static method Parse /// /// Parses body from the specified stream /// /// Owner MIME entity. /// Default content-type for this body. /// Stream from where to read body. /// Returns parsed body. /// Is raised when stream, mediaType or stream is null reference. /// Is raised when any parsing errors. protected static new MIME_b Parse(MIME_Entity owner,MIME_h_ContentType defaultContentType,SmartStream stream) { if(owner == null){ throw new ArgumentNullException("owner"); } if(defaultContentType == null){ throw new ArgumentNullException("defaultContentType"); } if(stream == null){ throw new ArgumentNullException("stream"); } MIME_b_Text retVal = null; if(owner.ContentType != null){ retVal = new MIME_b_Text(owner.ContentType.TypeWithSubype); } else{ retVal = new MIME_b_Text(defaultContentType.TypeWithSubype); } Net_Utils.StreamCopy(stream,retVal.EncodedStream,32000); retVal.SetModified(false); return retVal; } #endregion #region method SetText /// /// Sets text. /// /// Content transfer encoding. /// Charset to use to encode text. If not sure, utf-8 is recommended. /// Text. /// Is raised when transferEncoding, charset or text is null reference. /// Is raised when this method is accessed and this body is not bounded to any entity. /// Is raised when body contains not supported Content-Transfer-Encoding. public void SetText(string transferEncoding,Encoding charset,string text) { if(transferEncoding == null){ throw new ArgumentNullException("transferEncoding"); } if(charset == null){ throw new ArgumentNullException("charset"); } if(text == null){ throw new ArgumentNullException("text"); } if(this.Entity == null){ throw new InvalidOperationException("Body must be bounded to some entity first."); } SetEncodedData(transferEncoding,new MemoryStream(charset.GetBytes(text))); this.Entity.ContentType.Param_Charset = charset.WebName; } #endregion #region method GetCharset /// /// Gets charset from Content-Type. If char set isn't specified, "ascii" is defined as default and it will be returned. /// /// Returns content charset. /// Is raised when Content-Type has not supported charset parameter value. private Encoding GetCharset() { // RFC 2046 4.1.2. The default character set, US-ASCII. if(this.Entity.ContentType == null || string.IsNullOrEmpty(this.Entity.ContentType.Param_Charset)){ return Encoding.ASCII; } else{ return Encoding.GetEncoding(this.Entity.ContentType.Param_Charset); } } #endregion #region Properties implementation /// /// Gets body decoded text. /// /// Is raised when not supported content-type charset or not supported content-transfer-encoding value. /// Is raised when body contains not supported Content-Transfer-Encoding. public string Text { get{ return GetCharset().GetString(this.Data); } } #endregion } }