コード例 #1
0
        unsafe void Write(byte *buffer, int count, LZMAAction action)
        {
            if (_mode == CompressionMode.Decompress)
            {
                throw new NotSupportedException("Can't write on a decompress stream!");
            }

            _wasWrittenTo = true;

            _zstream->avail_in = (IntPtr)count;
            _zstream->next_in  = buffer;

            do
            {
                var result = LZMANative.lzma_code(_zstream, action);

                var avail_out = _zstream->avail_out.ToInt32();
                if (avail_out < BufferSize)
                {
                    var outSize = BufferSize - avail_out;
                    _compressedStream.Write(_tmpBuffer, 0, outSize);
                    _zstream->next_out  = _tmpBufferPtr;
                    _zstream->avail_out = (IntPtr)BufferSize;
                }


                // Translate erros into specific exceptions
                switch (result)
                {
                case LZMAStatus.LZMA_OK:
                    continue;

                case LZMAStatus.LZMA_STREAM_END:
                    return;

                case LZMAStatus.LZMA_MEM_ERROR:
                case LZMAStatus.LZMA_MEMLIMIT_ERROR:
                    throw new OutOfMemoryException("liblzma return code: " + result);

                default:
                    throw new Exception("liblzma return code: " + result);
                }
            } while ((_zstream->avail_in.ToInt32() > 0) || action == LZMAAction.LZMA_FINISH);
        }
コード例 #2
0
        public unsafe int Read(byte *buffer, int length, int offset, int count)
        {
            if (_mode == CompressionMode.Compress)
            {
                throw new NotSupportedException("Can't read on a compress stream!");
            }

            var exitLoop = false;

            _zstream->next_out  = buffer + offset;
            _zstream->avail_out = (IntPtr)count;

            while (_zstream->avail_out.ToInt32() > 0 && exitLoop == false)
            {
                if (_zstream->avail_in.ToInt32() == 0)
                {
                    var readLength = _compressedStream.Read(_tmpBuffer, 0, _tmpBuffer.Length);
                    _zstream->avail_in = (IntPtr)readLength;
                    _zstream->next_in  = _tmpBufferPtr;
                }
                var result = LZMANative.lzma_code(_zstream, LZMAAction.LZMA_RUN);

                if (result == LZMAStatus.LZMA_OK)
                {
                    continue;
                }

                switch (result)
                {
                case LZMAStatus.LZMA_STREAM_END:
                    exitLoop = true;
                    break;

                case LZMAStatus.LZMA_MEM_ERROR:
                case LZMAStatus.LZMA_MEMLIMIT_ERROR:
                    throw new OutOfMemoryException("liblzma return code: " + result);

                default:
                    throw new Exception("liblzma return code: " + result);
                }
            }

            return(count - (int)_zstream->avail_out);
        }