public override unsafe void Close() { if (_isClosed) { return; } _isClosed = true; if (_mode == CompressionMode.Compress) { // Mark end of stream var endSize = LZ4F_compressEnd(_ctx, _dstBuffer.Ptr, (IntPtr)_dstBuffer.Size); if (LZ4F_isError(endSize)) { throw new LZ4Exception(endSize, $"End of file generation failed {GetErrorName(endSize)}"); } _baseStream.Write(_dstBuffer.Buffer, 0, endSize.ToInt32()); var rc = LZ4F_freeCompressionContext(_ctx); if (LZ4F_isError(rc)) { throw new LZ4Exception(rc, $"Error : can't free LZ4F context resource: {GetErrorName(rc)}"); } } else { // Decompress var rc = LZ4Native.LZ4F_freeDecompressionContext(_ctx); if (LZ4Native.LZ4F_isError(rc)) { throw new LZ4Exception(rc, $"Error : can't free LZ4F context resource : {LZ4Native.GetErrorName(rc)}"); } } _baseStream.Close(); if (_srcBuffer != null) { _srcBuffer.Dispose(); _srcBuffer = null; } if (_dstBuffer != null) { _dstBuffer.Dispose(); _dstBuffer = null; } }
/// <summary> /// Initializes a new instance of the <see cref="LZ4Exception"/> class with a specified /// error code /// </summary> /// <param name="errorCode">The lz4 error code that caused the exception</param> public LZ4Exception(IntPtr errorCode) : this(errorCode, LZ4Native.GetErrorName(errorCode)) { }