Ejemplo n.º 1
0
        public long Encode(IEncodingInputStream inputStream, IEncodingOutputStream outputStream, EncodingToken encodingToken, CancellationToken cancellationToken, IProgressHandler progress)
        {
            Guard.IsNotNull(inputStream, nameof(inputStream));
            Guard.IsNotNull(outputStream, nameof(outputStream));
            Guard.IsNotNull(encodingToken, nameof(encodingToken));

            CodingTable codingTable = encodingToken.CodingTable;

            inputStream.Reset();
            long sequenceLength = 0;
            long progressValue  = progress?.State.CastTo <CodingProgressState>()?.Value ?? 0;

            while (inputStream.ReadSymbol(out byte symbol))
            {
                BitSequence codingSequence = codingTable[symbol];
                for (int n = 0; n < codingSequence.Size; n++)
                {
                    Bit bit = codingSequence[n];
                    sequenceLength++;
                    outputStream.WriteBit(bit);
                }
                // Throttling
                if (++progressValue == ChunkSize)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    progress?.Report(ChunkSize, inputStream.Path);
                    progressValue = 0;
                }
            }
            if (progress != null)
            {
                CodingProgressState progressState = (CodingProgressState)progress.State ?? new CodingProgressState();
                progress.State = progressState.WithValue(progressValue);
            }
            return(sequenceLength);
        }
Ejemplo n.º 2
0
        public void Decode(IDecodingInputStream inputStream, HuffmanTreeBase tree, IDecodingOutputStream outputStream, long sequenceLength, CancellationToken cancellationToken, IProgressHandler progress)
        {
            Guard.IsNotNull(inputStream, nameof(inputStream));
            Guard.IsNotNull(tree, nameof(tree));
            Guard.IsNotNull(outputStream, nameof(outputStream));
            Guard.IsNotNegative(sequenceLength, nameof(sequenceLength));

            const int chunkSize      = 0x20000 * 8; // 128Kb
            long      progressValue  = progress?.State.CastTo <CodingProgressState>()?.Value ?? 0;
            long      streamPosition = inputStream.Position;

            TreeWalker walker = new TreeWalker(tree, inputStream, outputStream, sequenceLength);

            while (!walker.Exhausted && !inputStream.IsEmpty)
            {
                if (!tree.Walk(walker))
                {
                    throw new ArgumentException();
                }
                // Throttling
                progressValue += inputStream.Position - streamPosition;
                streamPosition = inputStream.Position;

                if (progressValue >= chunkSize)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    progress?.Report(progressValue / 8, outputStream.Path);
                    progressValue %= 8;
                }
            }
            if (progress != null)
            {
                CodingProgressState progressState = (CodingProgressState)progress.State ?? new CodingProgressState();
                progress.State = progressState.WithValue(progressValue);
            }
        }