Wrapped Streams are always disposed - 0.21 #300

Open
opened 2026-01-29 22:09:44 +00:00 by claunia · 3 comments
Owner

Originally created by @t246246 on GitHub (May 16, 2018).

This program

using SharpCompress.Compressors.BZip2;
using System;
using System.IO;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var inFile = new FileStream("../../Program.cs", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var bzipStream = new BZip2Stream(memoryStream, SharpCompress.Compressors.CompressionMode.Compress, true))
                    {
                        inFile.CopyTo(bzipStream);
                        bzipStream.Close();
                    }                    
                    var length = memoryStream.Length;
                    Console.Out.WriteLine(length);
                }
            }
        }
    }
}

worked in version 0.20.0 (as long as the 3rd argument of BZip2Stream is true.)
It was the only way I managed to find getting bzip'ed bytes in memory.

It no longer works current 0.21.1 --- memoryStream is closed when trying to get Length. So how to get bzip'ed imege to memory stream (without using temporary file?)

Originally created by @t246246 on GitHub (May 16, 2018). This program ``` using SharpCompress.Compressors.BZip2; using System; using System.IO; namespace ConsoleApp { class Program { static void Main(string[] args) { using (var inFile = new FileStream("../../Program.cs", FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var memoryStream = new MemoryStream()) { using (var bzipStream = new BZip2Stream(memoryStream, SharpCompress.Compressors.CompressionMode.Compress, true)) { inFile.CopyTo(bzipStream); bzipStream.Close(); } var length = memoryStream.Length; Console.Out.WriteLine(length); } } } } } ``` worked in version 0.20.0 (as long as the 3rd argument of BZip2Stream is true.) It was the only way I managed to find getting bzip'ed bytes in memory. It no longer works current 0.21.1 --- memoryStream is closed when trying to get Length. So how to get bzip'ed imege to memory stream (without using temporary file?)
Author
Owner

@adamhathcock commented on GitHub (May 16, 2018):

Don't allow the stream to be disposed. Use NonDisposingStream like:

        [Fact]
        public void TestBZip2InMemory()
        {
            using (var inFile = new FileStream("../../../ADCTest.cs", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var bzipStream = new BZip2Stream(new NonDisposingStream(memoryStream), SharpCompress.Compressors.CompressionMode.Compress, true))
                    {
                        inFile.CopyTo(bzipStream);
                        bzipStream.Close();
                    }                    
                    var length = memoryStream.Length;
                    Console.Out.WriteLine(length);
                }
            }
        }
@adamhathcock commented on GitHub (May 16, 2018): Don't allow the stream to be disposed. Use `NonDisposingStream` like: ``` [Fact] public void TestBZip2InMemory() { using (var inFile = new FileStream("../../../ADCTest.cs", FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var memoryStream = new MemoryStream()) { using (var bzipStream = new BZip2Stream(new NonDisposingStream(memoryStream), SharpCompress.Compressors.CompressionMode.Compress, true)) { inFile.CopyTo(bzipStream); bzipStream.Close(); } var length = memoryStream.Length; Console.Out.WriteLine(length); } } } ```
Author
Owner

@adamhathcock commented on GitHub (May 16, 2018):

FYI, there was a breaking change in behavior in regards to how stream disposal is handled. I used to fight the good fight and try to make it so that Streams wouldn't dispose/close wrapped Streams they didn't own. However, this is rarely the case for many reasons. Now, you should assume disposing a Stream/Reader will also dispose the wrapped Stream.

@adamhathcock commented on GitHub (May 16, 2018): FYI, there was a breaking change in behavior in regards to how stream disposal is handled. I used to fight the good fight and try to make it so that Streams wouldn't dispose/close wrapped Streams they didn't own. However, this is rarely the case for many reasons. Now, you should assume disposing a Stream/Reader will also dispose the wrapped Stream.
Author
Owner

@adamhathcock commented on GitHub (May 16, 2018):

Also updated USAGE: https://github.com/adamhathcock/sharpcompress/blob/master/USAGE.md

@adamhathcock commented on GitHub (May 16, 2018): Also updated USAGE: https://github.com/adamhathcock/sharpcompress/blob/master/USAGE.md
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#300