Revenj.Utility.ChunkedMemoryStream.Create C# (CSharp) Method

Create() public static method

Create or get a new instance of memory stream Stream is bound to thread and must be released from the same thread
public static Create ( ) : ChunkedMemoryStream
return ChunkedMemoryStream
        public static ChunkedMemoryStream Create()
        {
            ChunkedMemoryStream stream;
            if (MemoryPool.TryPop(out stream))
            {
                CurrentEstimate--;
                stream.Reset();
                stream.BoundToThread = Thread.CurrentThread.ManagedThreadId;
                stream.disposed = false;
                return stream;
            }
            CurrentEstimate = MemoryPool.Count;
            return new ChunkedMemoryStream();
        }

Usage Example

Example #1
0
        /// <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);
        }