Beispiel #1
0
        /// <summary>This property is not supported and always throws a NotSupportedException.</summary>
        /// <param name="array">The array used to store compressed bytes.</param>
        /// <param name="offset">The location in the array to begin reading.</param>
        /// <param name="count">The number of bytes compressed.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (pCompMode == CompressionMode.Decompress)
            {
                throw new NotSupportedException("Can't write on a decompression stream!");
            }

            pBytesIn += count;

            using (FixedArray writePtr = new FixedArray(pWorkData))
                using (FixedArray bufferPtr = new FixedArray(buffer))
                {
                    pZstream.next_in   = bufferPtr[offset];
                    pZstream.avail_in  = (uint)count;
                    pZstream.next_out  = writePtr[pWorkDataPos];
                    pZstream.avail_out = (uint)(WORK_DATA_SIZE - pWorkDataPos);

                    //				pCrcValue = crc32(pCrcValue, &bufferPtr[offset], (uint)count);

                    while (pZstream.avail_in != 0)
                    {
                        if (pZstream.avail_out == 0)
                        {
                            //rar logikk, men det betyr vel bare at den kun skriver hvis buffer ble fyllt helt,
                            //dvs halvfyllt buffer vil kun skrives ved flush
                            pStream.Write(pWorkData, 0, (int)WORK_DATA_SIZE);
                            pBytesOut         += WORK_DATA_SIZE;
                            pWorkDataPos       = 0;
                            pZstream.next_out  = writePtr;
                            pZstream.avail_out = WORK_DATA_SIZE;
                        }

                        uint outCount = pZstream.avail_out;

                        int zlibError = ZLib.deflate(ref pZstream, ZLibFlush.NoFlush);

                        pWorkDataPos += (int)(outCount - pZstream.avail_out);

                        if (zlibError != ZLibReturnCode.Ok)
                        {
                            pSuccess = false;
                            throw new ZLibException(zlibError, pZstream.lasterrormsg);
                        }
                    }
                }
        }
Beispiel #2
0
        /// <summary>Flushes the contents of the internal buffer of the current GZipStream object to the underlying stream.</summary>
        public override void Flush()
        {
            if (pCompMode == CompressionMode.Decompress)
            {
                throw new NotSupportedException("Can't flush a decompression stream.");
            }

            using (FixedArray workDataPtr = new FixedArray(pWorkData))
            {
                pZstream.next_in   = IntPtr.Zero;
                pZstream.avail_in  = 0;
                pZstream.next_out  = workDataPtr[pWorkDataPos];
                pZstream.avail_out = (uint)(WORK_DATA_SIZE - pWorkDataPos);

                int zlibError = ZLibReturnCode.Ok;
                while (zlibError != ZLibReturnCode.StreamEnd)
                {
                    if (pZstream.avail_out != 0)
                    {
                        uint outCount = pZstream.avail_out;
                        zlibError = ZLib.deflate(ref pZstream, ZLibFlush.Finish);

                        pWorkDataPos += (int)(outCount - pZstream.avail_out);
                        if (zlibError == ZLibReturnCode.StreamEnd)
                        {
                            //ok. will break loop
                        }
                        else if (zlibError != ZLibReturnCode.Ok)
                        {
                            pSuccess = false;
                            throw new ZLibException(zlibError, pZstream.lasterrormsg);
                        }
                    }

                    pStream.Write(pWorkData, 0, pWorkDataPos);
                    pBytesOut         += pWorkDataPos;
                    pWorkDataPos       = 0;
                    pZstream.next_out  = workDataPtr;
                    pZstream.avail_out = WORK_DATA_SIZE;
                }
            }

            this.pStream.Flush();
        }