ManagedXZ.XZCompressStream.Dispose C# (CSharp) 메소드

Dispose() 보호된 메소드

protected Dispose ( bool disposing ) : void
disposing bool
리턴 void
        protected override void Dispose(bool disposing)
        {
            if (_stream == null) return;
            try
            {
                // compress all remaining data
                while (true)
                {
                    // do compress, LZMA_FINISH action should return LZMA_OK or LZMA_STREAM_END on success
                    var ret = Native.lzma_code(_lzma_stream, lzma_action.LZMA_FINISH);
                    if (ret != lzma_ret.LZMA_STREAM_END && ret != lzma_ret.LZMA_OK)
                        throw new Exception($"lzma_code returns {ret}");

                    // write output buffer to underlying stream
                    if (_lzma_stream.avail_out == UIntPtr.Zero || ret == lzma_ret.LZMA_STREAM_END)
                    {
                        byte[] data = new byte[BUFSIZE - (uint)_lzma_stream.avail_out];
                        Marshal.Copy(_outbuf, data, 0, data.Length);
                        _stream.Write(data, 0, data.Length);

                        // Reset next_out and avail_out.
                        _lzma_stream.next_out = _outbuf;
                        _lzma_stream.avail_out = (UIntPtr)BUFSIZE;
                    }

                    if (ret == lzma_ret.LZMA_STREAM_END)
                        break;
                }
            }
            finally
            {
                Native.lzma_end(_lzma_stream);
                Marshal.FreeHGlobal(_inbuf);
                Marshal.FreeHGlobal(_outbuf);
                _stream.Close();
                _stream = null;
            }
        }

Usage Example

예제 #1
0
        private void TestDispose()
        {
            var c = new XZCompressStream("temp1.xz");
            c.Close();
            c.Close();

            c = new XZCompressStream("temp2.xz");
            c.Dispose();
            c.Dispose();

            var d = new XZDecompressStream("temp1.xz");
            d.Close();
            d.Close();

            d = new XZDecompressStream("temp2.xz");
            d.Dispose();
            d.Dispose();
        }