Example #1
0
 public static void Write(this IEncodingOutputStream @this, byte value)
 {
     for (int n = 0; n < 8; n++)
     {
         @this.WriteBit((value & 1) == 1 ? Bit.One : Bit.Zero);
         value >>= 1;
     }
 }
Example #2
0
        private void LayoutStreamInMemory(long length)
        {
            int trailingBitCount = MathHelper.ModAdv(length, 8);

            for (int n = 0; n < trailingBitCount; n++)
            {
                stream.WriteBit(Bit.Zero);
            }
        }
        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);
        }