/// <summary> /// Convert StringBuilder to Stream. /// Depending on the size, appropriate stream will be used. /// </summary> /// <param name="sb">string builder</param> /// <returns>utf8 stream</returns> public static Stream ToStream(this StringBuilder sb) { if (sb.Length < 8192) { return(new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))); } var cms = ChunkedMemoryStream.Create(); var sw = new StreamWriter(cms); sw.WriteBuilder(sb); sw.Flush(); cms.Position = 0; return(cms); }
/// <summary> /// Convert provided stream content to pdf. /// Specify extension of the file /// </summary> /// <param name="content">file content</param> /// <param name="ext">file extension</param> /// <param name="disposeStream">dispose provided stream after conversion</param> /// <returns>pdf converted stream</returns> public static Stream Convert(Stream content, string ext, bool disposeStream) { var from = TemporaryResources.CreateFile(ext); var to = from + ".pdf"; var fs = new FileStream(from, FileMode.Create, FileAccess.Write); content.CopyTo(fs); fs.Close(); if (disposeStream) { content.Dispose(); } RunConverter(from); var cms = ChunkedMemoryStream.Create(); using (var f = new FileStream(to, FileMode.Open, FileAccess.Read)) { f.CopyTo(cms); } File.Delete(from); File.Delete(to); cms.Position = 0; return(cms); }
/// <summary> /// Convert string builder to base64 stream. /// Depending on the size, appropriate method will be used to avoid LOH issues. /// </summary> /// <param name="sb">string builder</param> /// <returns>base64 encoded stream</returns> public static Stream ToBase64Stream(this StringBuilder sb) { if (sb.Length < 8192) { return(new MemoryStream(Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(sb.ToString()))))); } using (var cms = ChunkedMemoryStream.Create()) using (var sw = new StreamWriter(cms)) { var len = sb.Length / 4096; var pos = 0; for (int i = 0; i < len; i++, pos += 4096) { sw.Write(sb.ToString(pos, 4096)); } if (sb.Length > pos) { sw.Write(sb.ToString(pos, sb.Length - pos)); } sw.Flush(); cms.Position = 0; return(cms.ToBase64Stream()); } }
public CustomWriter(ChunkedMemoryStream cms) : base(cms) { }