Пример #1
0
        private ZReturnConsts do_deflate(ZFlushConsts flush, out int avail_out)
        {
            avail_out = BUFFER_SIZE;
            ZReturnConsts ret_val = z_stream_deflate(z_stream, flush, sized_buffer, ref avail_out);

            switch (ret_val)
            {
            case ZReturnConsts.Z_STREAM_ERROR:
                throw new InvalidOperationException("Internal error.");

            case ZReturnConsts.Z_MEM_ERROR:
                throw new InvalidOperationException("Memory error.");
            }
            return(ret_val);
        }
Пример #2
0
        private int ReadInternal(byte[] array, int offset, int count)
        {
            int buffer_size;

            if (finished)
            {
                return(0);
            }

            if (compressedStream.CanSeek)
            {
                buffer_size = BUFFER_SIZE;
            }
            else
            {
                buffer_size = 1;
            }

            IntPtr buffer = Marshal.AllocHGlobal(count);

            try {
                int avail_out;

                avail_out = count;
                z_stream_set_next_out(z_stream, buffer);

                while (avail_out != 0 && !finished)
                {
                    if (z_stream_get_avail_in(z_stream) == 0)
                    {
                        byte[] read_buf    = new byte[buffer_size];
                        int    length_read = compressedStream.Read(read_buf, 0, buffer_size);
                        bytes_read += length_read;
                        if (length_read == 0)
                        {
                            break;
                        }
                        Marshal.Copy(read_buf, 0, sized_buffer, length_read);
                        z_stream_set_next_in(z_stream, sized_buffer);
                        z_stream_set_avail_in(z_stream, length_read);
                    }
                    ZReturnConsts ret_val = z_stream_inflate(z_stream, ref avail_out);
                    switch (ret_val)
                    {
                    case ZReturnConsts.Z_OK:
                        break;

                    case ZReturnConsts.Z_STREAM_END:
                        finished = true;
                        break;

                    case ZReturnConsts.Z_NEED_DICT:
                        throw new InvalidDataException("ZLib stream requires a dictionary.");

                    case ZReturnConsts.Z_DATA_ERROR:
                        throw new InvalidDataException("Invalid ZLib data.");

                    case ZReturnConsts.Z_STREAM_ERROR:
                        throw new InvalidOperationException("Internal DeflateStream error.");

                    case ZReturnConsts.Z_MEM_ERROR:
                        throw new OutOfMemoryException();

                    case ZReturnConsts.Z_BUF_ERROR:
                        throw new InvalidOperationException("Internal DeflateStream error: Buf error.");
                    }
                }
                if (count != avail_out)
                {
                    Marshal.Copy(buffer, array, offset, count - avail_out);
                }
                return(count - avail_out);
            } finally {
                Marshal.FreeHGlobal(buffer);
            }
        }