/// <summary>Prepares a new block for decompression if any remain in the stream</summary> /// <remarks>If a previous block has completed, its CRC is checked and merged into the stream CRC. /// If the previous block was the final block in the stream, the stream CRC is validated</remarks> /// <return>true if a block was successfully initialised, or false if the end of file marker was encountered</return> /// <exception>If either the block or stream CRC check failed, if the following data is /// not a valid block-header or end-of-file marker, or if the following block could not be decoded</exception> private bool InitialiseNextBlock() { /* If we're already at the end of the stream, do nothing */ if (this.streamComplete) return false; /* If a block is complete, check the block CRC and integrate it into the stream CRC */ if (this.blockDecompressor != null) { uint blockCRC = this.blockDecompressor.CheckCrc(); this.streamCRC = ((this.streamCRC << 1) | (this.streamCRC >> 31)) ^ blockCRC; } /* Read block-header or end-of-stream marker */ uint marker1 = this.bitInputStream.ReadBits(24); uint marker2 = this.bitInputStream.ReadBits(24); if (marker1 == BZip2BlockCompressor.BLOCK_HEADER_MARKER_1 && marker2 == BZip2BlockCompressor.BLOCK_HEADER_MARKER_2) { // Initialise a new block try { this.blockDecompressor = new BZip2BlockDecompressor(this.bitInputStream, this.streamBlockSize); } catch (IOException) { // If the block could not be decoded, stop trying to read more data this.streamComplete = true; throw; } return true; } if (marker1 == BZip2OutputStream.STREAM_END_MARKER_1 && marker2 == BZip2OutputStream.STREAM_END_MARKER_2) { // Read and verify the end-of-stream CRC this.streamComplete = true; uint storedCombinedCRC = this.bitInputStream.ReadInteger(); ///.ReadBits(32); if (storedCombinedCRC != this.streamCRC) throw new Exception ("BZip2 stream CRC error"); return false; } /* If what was read is not a valid block-header or end-of-stream marker, the stream is broken */ this.streamComplete = true; throw new Exception ("BZip2 stream format error"); }
public override void Close() { if (this.bitInputStream == null) return; this.streamComplete = true; this.blockDecompressor = null; this.bitInputStream = null; try { this.inputStream.Close(); } finally { this.inputStream = null; } }