Exemple #1
0
        /// <summary>
        /// Read from the stream
        /// </summary>
        /// <param name="buffer">the buffer to read</param>
        /// <param name="offset">the offset at which to start</param>
        /// <param name="count">the number of bytes to read</param>
        /// <returns>the number of bytes actually read</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesToRead = count;

            // Need to limit the # of bytes returned, if the stream is intended to have
            // a definite length.  This is especially useful when returning a stream for
            // the uncompressed data directly to the application.  The app won't
            // necessarily read only the UncompressedSize number of bytes.  For example
            // wrapping the stream returned from OpenReader() into a StreadReader() and
            // calling ReadToEnd() on it, We can "over-read" the zip data and get a
            // corrupt string.  The length limits that, prevents that problem.

            if (_lengthLimit != UnsetLengthLimit)
            {
                if (_Crc32.TotalBytesRead >= _lengthLimit)
                {
                    return(0);                                       // EOF
                }
                Int64 bytesRemaining = _lengthLimit - _Crc32.TotalBytesRead;
                if (bytesRemaining < count)
                {
                    bytesToRead = (int)bytesRemaining;
                }
            }
            int n = _innerStream.Read(buffer, offset, bytesToRead);

            if (n > 0)
            {
                _Crc32.SlurpBlock(buffer, offset, n);
            }
            return(n);
        }
        public override void Write(System.Byte[] buffer, int offset, int count)
        {
            // workitem 7159
            // calculate the CRC on the unccompressed data  (before writing)
            if (crc != null)
            {
                crc.SlurpBlock(buffer, offset, count);
            }

            if (_streamMode == StreamMode.Undefined)
            {
                _streamMode = StreamMode.Writer;
            }
            else if (_streamMode != StreamMode.Writer)
            {
                throw new ZlibException("Cannot Write after Reading.");
            }

            if (count == 0)
            {
                return;
            }

            // first reference of z property will initialize the private var _z
            z.InputBuffer       = buffer;
            _z.NextIn           = offset;
            _z.AvailableBytesIn = count;
            bool done = false;

            do
            {
                _z.OutputBuffer      = workingBuffer;
                _z.NextOut           = 0;
                _z.AvailableBytesOut = _workingBuffer.Length;
                int rc = (_wantCompress)
                             ? _z.Deflate(_flushMode)
                             : _z.Inflate(_flushMode);
                if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                {
                    throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
                }

                //if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
                _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);

                done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;

                // If GZIP and de-compress, we're done when 8 bytes remain.
                if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
                {
                    done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
                }
            } while (!done);
        }
Exemple #3
0
        private void _DeflateOne(object wi)
        {
            WorkItem workitem = (WorkItem)wi;

            try
            {
                lock (workitem)
                {
                    if (workitem.status != 2)
                    {
                        throw new InvalidOperationException();
                    }
                    CRC32 crc = new CRC32();
                    crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable);
                    this.DeflateOneSegment(workitem);
                    workitem.status = 4;
                    workitem.crc    = crc.Crc32Result;
                    Monitor.Pulse(workitem);
                }
            }
            catch (Exception exception)
            {
                lock (this._eLock)
                {
                    if (this._pendingException != null)
                    {
                        this._pendingException = exception;
                    }
                }
            }
        }
        private void _DeflateOne(Object wi)
        {
            var workitem = (WorkItem)wi;

            try
            {
                // compress one buffer
                int myItem = workitem.index;

                lock (workitem)
                {
                    if (workitem.status != (int)WorkItem.Status.Filled)
                    {
                        throw new InvalidOperationException();
                    }

                    var crc = new CRC32();

                    // use the workitem:
                    // calc CRC on the buffer
                    crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable);

                    // deflate it
                    DeflateOneSegment(workitem);

                    // update status
                    workitem.status = (int)WorkItem.Status.Compressed;
                    workitem.crc    = crc.Crc32Result;

                    TraceOutput(TraceBits.Compress,
                                "Compress          wi({0}) stat({1}) len({2})",
                                workitem.index,
                                workitem.status,
                                workitem.compressedBytesAvailable
                                );

                    // release the item
                    Monitor.Pulse(workitem);
                }
            }
            catch (Exception exc1)
            {
                lock (_eLock)
                {
                    // expose the exception to the main thread
                    if (_pendingException != null)
                    {
                        _pendingException = exc1;
                    }
                }
            }
        }
        private void _DeflateOne(Object wi)
        {
            var workitem = (WorkItem)wi;
            try
            {
                // compress one buffer
                //int myItem = workitem.index;

                lock (workitem)
                {
                    if (workitem.status != (int)WorkItem.Status.Filled)
                        throw new InvalidOperationException();

                    var crc = new CRC32();

                    // use the workitem:
                    // calc CRC on the buffer
                    crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable);

                    // deflate it
                    DeflateOneSegment(workitem);

                    // update status
                    workitem.status = (int)WorkItem.Status.Compressed;
                    workitem.crc = crc.Crc32Result;

                    TraceOutput(TraceBits.Compress,
                                "Compress          wi({0}) stat({1}) len({2})",
                                workitem.index,
                                workitem.status,
                                workitem.compressedBytesAvailable
                        );

                    // release the item
                    Monitor.Pulse(workitem);
                }
            }
            catch (Exception exc1)
            {
                lock (_eLock)
                {
                    // expose the exception to the main thread
                    if (_pendingException != null)
                        _pendingException = exc1;
                }
            }
        }