Example #1
0
        /// <summary>
        ///   Resets the stream for use with another stream.
        /// </summary>
        /// <remarks>
        ///   Because the ParallelDeflateOutputStream is expensive to create, it
        ///   has been designed so that it can be recycled and re-used.  You have
        ///   to call Close() on the stream first, then you can call Reset() on
        ///   it, to use it again on another stream.
        /// </remarks>
        ///
        /// <param name="stream">
        ///   The new output stream for this era.
        /// </param>
        ///
        /// <example>
        /// <code>
        /// ParallelDeflateOutputStream deflater = null;
        /// foreach (var inputFile in listOfFiles)
        /// {
        ///     string outputFile = inputFile + ".compressed";
        ///     using (System.IO.Stream input = System.IO.File.OpenRead(inputFile))
        ///     {
        ///         using (var outStream = System.IO.File.Create(outputFile))
        ///         {
        ///             if (deflater == null)
        ///                 deflater = new ParallelDeflateOutputStream(outStream,
        ///                                                            CompressionLevel.Best,
        ///                                                            CompressionStrategy.Default,
        ///                                                            true);
        ///             deflater.Reset(outStream);
        ///
        ///             while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
        ///             {
        ///                 deflater.Write(buffer, 0, n);
        ///             }
        ///         }
        ///     }
        /// }
        /// </code>
        /// </example>
        public void Reset(Stream stream)
        {
            TraceOutput(TraceBits.Session, "-------------------------------------------------------");
            TraceOutput(TraceBits.Session, "Reset {0:X8} firstDone({1})", GetHashCode(), _firstWriteDone);

            if (!_firstWriteDone)
            {
                return;
            }

            // reset all status
            _toWrite.Clear();
            _toFill.Clear();
            foreach (var workitem in _pool)
            {
                _toFill.Enqueue(workitem.index);
                workitem.ordinal = -1;
            }

            _firstWriteDone   = false;
            BytesProcessed    = 0L;
            _runningCrc       = new Crc.Crc32();
            _isClosed         = false;
            _currentlyFilling = -1;
            _lastFilled       = -1;
            _lastWritten      = -1;
            _latestCompressed = -1;
            _outStream        = stream;
        }
Example #2
0
        private void DeflateOne(object?wi)
        {
            if (wi == null)
            {
                throw new ArgumentNullException(nameof(wi));
            }

            // compress one buffer
            var workitem = (WorkItem)wi;

            try
            {
                int myItem = workitem.index;
                var crc    = new Crc.Crc32();

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

                // deflate it
                int rc = DeflateOneSegment(workitem);

                // update status
                workitem.crc = crc.Crc32Result;
                TraceOutput(TraceBits.Compress,
                            "Compress          wi({0}) ord({1}) len({2})",
                            workitem.index,
                            workitem.ordinal,
                            workitem.compressedBytesAvailable
                            );

                lock (_latestLock)
                {
                    if (workitem.ordinal > _latestCompressed)
                    {
                        _latestCompressed = workitem.ordinal;
                    }
                }
                lock (_toWrite)
                {
                    _toWrite.Enqueue(workitem.index);
                }
                _newlyCompressedBlob.Set();
            }
            catch (Exception exc1)
            {
                lock (_eLock)
                {
                    // expose the exception to the main thread
                    if (_pendingException != null)
                    {
                        _pendingException = exc1;
                    }
                }
            }
        }
Example #3
0
 public ZlibBaseStream(
     Stream stream,
     CompressionMode compressionMode,
     CompressionLevel level,
     ZlibStreamFlavor flavor,
     bool leaveOpen)
     : base()
 {
     _flushMode = FlushType.None;
     //this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT];
     _stream          = stream;
     _leaveOpen       = leaveOpen;
     _compressionMode = compressionMode;
     _flavor          = flavor;
     _level           = level;
     // workitem 7159
     if (flavor == ZlibStreamFlavor.GZIP)
     {
         _crc = new Crc.Crc32();
     }
 }
Example #4
0
        private void InitializePoolOfWorkItems()
        {
            _toWrite = new Queue <int>();
            _toFill  = new Queue <int>();
            _pool    = new List <WorkItem>();
            int nTasks = BufferPairsPerCore * Environment.ProcessorCount;

            nTasks = Math.Min(nTasks, _maxBufferPairs);
            for (int i = 0; i < nTasks; i++)
            {
                _pool.Add(new WorkItem(_bufferSize, _compressLevel, Strategy, i));
                _toFill.Enqueue(i);
            }

            _newlyCompressedBlob = new AutoResetEvent(false);
            _runningCrc          = new Crc.Crc32();
            _currentlyFilling    = -1;
            _lastFilled          = -1;
            _lastWritten         = -1;
            _latestCompressed    = -1;
        }