/// <summary> /// Releases underlying resources. /// </summary> /// <param name="disposing">Whether this method is called from Dispose.</param> protected override void Dispose(bool disposing) { try { if (disposing) { if (_compressedStream != null && _ownsCompressed == Ownership.Dispose) { _compressedStream.Dispose(); } _compressedStream = null; if (_rleStream != null) { _rleStream.Dispose(); _rleStream = null; } } } finally { base.Dispose(disposing); } }
/// <summary> /// Initializes a new instance of the BZip2DecoderStream class. /// </summary> /// <param name="stream">The compressed input stream.</param> /// <param name="ownsStream">Whether ownership of stream passes to the new instance.</param> public BZip2DecoderStream(Stream stream, Ownership ownsStream) { _compressedStream = stream; _ownsCompressed = ownsStream; _bitstream = new BigEndianBitStream(new BufferedStream(stream)); // The Magic BZh byte[] magic = new byte[3]; magic[0] = (byte)_bitstream.Read(8); magic[1] = (byte)_bitstream.Read(8); magic[2] = (byte)_bitstream.Read(8); if (magic[0] != 0x42 || magic[1] != 0x5A || magic[2] != 0x68) { throw new InvalidDataException("Bad magic at start of stream"); } // The size of the decompression blocks in multiples of 100,000 int blockSize = (int)_bitstream.Read(8) - 0x30; if (blockSize < 1 || blockSize > 9) { throw new InvalidDataException("Unexpected block size in header: " + blockSize); } blockSize *= 100000; _rleStream = new BZip2RleStream(); _blockDecoder = new BZip2BlockDecoder(blockSize); _blockBuffer = new byte[blockSize]; if (ReadBlock() == 0) { _eof = true; } }