/// <summary>
        /// Creates a new decompression dictionary.
        /// </summary>
        /// <returns>
        /// The IntPtr to the decompression dictionary.
        /// </returns>
        private IntPtr CreateDecompressionDictionary()
        {
            var alloc = GCHandle.Alloc(_dictionary, GCHandleType.Pinned);

            try
            {
                var dictBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(_dictionary, 0);
                var dictSize   = new UIntPtr((uint)_dictionary.Length);
                return(Interop.ZSTD_createDDict(dictBuffer, dictSize));
            }
            finally
            {
                alloc.Free();
            }
        }
        /// <summary>
        /// Releases unmanaged resources.
        /// </summary>
        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        // ReSharper disable once UnusedParameter.Local
        private void Dispose(bool dispose)
        {
            if (_isDisposed == false)
            {
                _isDisposed = true;

                if (_ddict != IntPtr.Zero)
                {
                    Interop.ZSTD_freeDDict(_ddict);
                    _ddict = IntPtr.Zero;
                }

                foreach (KeyValuePair <int, IntPtr> kv in _cdicts.ToList())
                {
                    Interop.ZSTD_freeCDict(kv.Value);
                    _cdicts.Remove(kv.Key);
                }
            }
        }
        /// <summary>
        /// Releases unmanaged resources.
        /// </summary>
        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool dispose)
        {
            if (this.isDisposed == false)
            {
                this.isDisposed = true;

                if (this.ddict != IntPtr.Zero)
                {
                    Interop.ZSTD_freeDDict(this.ddict);
                    this.ddict = IntPtr.Zero;
                }

                foreach (var kv in this.cdicts.ToList())
                {
                    Interop.ZSTD_freeCDict(kv.Value);
                    this.cdicts.Remove(kv.Key);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZstandardStream"/> class by using the specified stream and compression mode, and optionally leaves the stream open.
        /// </summary>
        /// <param name="stream">The stream to compress.</param>
        /// <param name="mode">One of the enumeration values that indicates whether to compress or decompress the stream.</param>
        /// <param name="leaveOpen">true to leave the stream open after disposing the <see cref="ZstandardStream"/> object; otherwise, false.</param>
        public ZstandardStream(Stream stream, CompressionMode mode, bool leaveOpen = false)
        {
            this.stream    = stream ?? throw new ArgumentNullException(nameof(stream));
            this.mode      = mode;
            this.leaveOpen = leaveOpen;

            if (mode == CompressionMode.Compress)
            {
                this.zstreamInputSize  = Interop.ZSTD_CStreamInSize().ToUInt32();
                this.zstreamOutputSize = Interop.ZSTD_CStreamOutSize().ToUInt32();
                this.zstream           = Interop.ZSTD_createCStream();
                this.data = arrayPool.Rent((int)this.zstreamOutputSize);
            }

            if (mode == CompressionMode.Decompress)
            {
                this.zstreamInputSize  = Interop.ZSTD_DStreamInSize().ToUInt32();
                this.zstreamOutputSize = Interop.ZSTD_DStreamOutSize().ToUInt32();
                this.zstream           = Interop.ZSTD_createDStream();
                this.data = arrayPool.Rent((int)this.zstreamInputSize);
            }
        }