public static char ReadChar(this IDecodingInputStream @this)
        {
            byte b1 = @this.ReadByte();
            byte b2 = @this.ReadByte();

            return((char)(b1 | (b2 << 8)));
        }
 public TreeWalker(HuffmanTreeBase tree, IDecodingInputStream input, IDecodingOutputStream output, long seqLength)
 {
     this.tree      = tree;
     this.input     = input;
     this.output    = output;
     this.seqLength = seqLength;
 }
 public DefaultFileDecoder(WeightsTable weightsTable, IDecodingInputStream inputStream, long length, HuffmanDecoder decoder)
 {
     this.weightsTable = weightsTable;
     this.inputStream  = inputStream;
     this.length       = length;
     this.decoder      = decoder;
 }
        public static int ReadInt(this IDecodingInputStream @this)
        {
            byte b1 = @this.ReadByte();
            byte b2 = @this.ReadByte();
            byte b3 = @this.ReadByte();
            byte b4 = @this.ReadByte();

            return(b1 | (b2 << 8) | (b3 << 16) | (b4 << 24));
        }
        public DirectorySegment ParseDirectory(IDecodingInputStream stream)
        {
            Guard.IsNotNull(stream, nameof(stream));
            int    length      = stream.ReadInt() / 2;
            string name        = ReadString(stream, length);
            int    cardinality = stream.ReadInt();

            return(new DirectorySegment(name, cardinality));
        }
        static string ReadString(IDecodingInputStream stream, int length)
        {
            char[] chars = new char[length];

            for (int n = 0; n < length; n++)
            {
                chars[n] = stream.ReadChar();
            }
            return(new string(chars));
        }
        public FileSegment ParseFile(IDecodingInputStream stream, WeightsTable weightsTable)
        {
            Guard.IsNotNull(stream, nameof(stream));
            Guard.IsNotNull(weightsTable, nameof(weightsTable));

            int    nameLength = stream.ReadInt() / 2;
            string name       = ReadString(stream, nameLength);
            long   dataLength = stream.ReadLong();

            return(new FileSegment(name, new DefaultFileDecoder(weightsTable, stream, dataLength, decoder)));
        }
        public void Decode(IDecodingInputStream inputStream, WeightsTable weightsTable, IDecodingOutputStream outputStream, long sequenceLength, CancellationToken cancellationToken, IProgressHandler progress)
        {
            Guard.IsNotNull(inputStream, nameof(inputStream));
            Guard.IsNotNull(weightsTable, nameof(weightsTable));
            Guard.IsNotNull(outputStream, nameof(outputStream));
            Guard.IsNotNegative(sequenceLength, nameof(sequenceLength));

            HuffmanTreeBase tree = new HuffmanEncoder().BuildHuffmanTree(weightsTable);

            Decode(inputStream, tree, outputStream, sequenceLength, cancellationToken, progress);
        }
        public static long ReadLong(this IDecodingInputStream @this)
        {
            byte b1 = @this.ReadByte();
            byte b2 = @this.ReadByte();
            byte b3 = @this.ReadByte();
            byte b4 = @this.ReadByte();
            byte b5 = @this.ReadByte();
            byte b6 = @this.ReadByte();
            byte b7 = @this.ReadByte();
            byte b8 = @this.ReadByte();

            return(((long)(uint)(b5 | (b6 << 8) | (b7 << 16) | (b8 << 24)) << 32) | (uint)(b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)));
        }
        public BootstrapSegment ParseWeightsTable(IDecodingInputStream stream)
        {
            Guard.IsNotNull(stream, nameof(stream));

            WeightsTable weightsTable = new WeightsTable();
            int          tableSize    = stream.ReadInt() / 9;

            for (int n = 0; n < tableSize; n++)
            {
                byte code      = stream.ReadByte();
                long frequency = stream.ReadLong();
                weightsTable.TrackSymbol(code, frequency);
            }
            return(new BootstrapSegment(weightsTable));
        }
        public static byte ReadByte(this IDecodingInputStream @this)
        {
            byte value = 0;

            for (int n = 0; n < 8; n++)
            {
                if ([email protected](out Bit bit))
                {
                    throw new InvalidOperationException();
                }

                if (bit == Bit.One)
                {
                    value |= (byte)(1 << n);
                }
            }
            return(value);
        }
Exemplo n.º 12
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);
            }
        }
 public static StreamKind ReadStreamFormat(this IDecodingInputStream @this)
 {
     try { return((StreamKind)@this.ReadByte()); }
     catch (Exception e) { throw new InvalidOperationException(string.Empty, e); }
 }
 FileSegment IStreamParser.ParseFile(IDecodingInputStream stream, WeightsTable weightsTable)
 {
     Trace += "->ParseFile;";
     return(new FileSegment(FileName, Decoder));
 }
 DirectorySegment IStreamParser.ParseDirectory(IDecodingInputStream stream)
 {
     Trace += "->ParseDirectory;";
     return(new DirectorySegment(DirectoryName, Cardinality));
 }
 BootstrapSegment IStreamParser.ParseWeightsTable(IDecodingInputStream stream)
 {
     Trace += "->ParseWeightsTable;";
     return(new BootstrapSegment(new WeightsTable()));
 }