Ejemplo n.º 1
0
        public BrotliFileStructure Apply(byte[] bytes, BrotliDictionary dictionary)
        {
            var fileParameters = new BrotliFileParameters.Builder {
                WindowSize = DetermineWindowSize(bytes),
                Dictionary = dictionary
            }.Build();

            var compressionParameters = SetupCompressionParameters(bytes);

            var bfs        = new BrotliFileStructure(fileParameters);
            var encoder    = CreateEncoder(bytes, fileParameters);
            var encodeInfo = new BrotliEncodeInfo(fileParameters, compressionParameters, bytes);

            do
            {
                var(metaBlock, newEncodeInfo) = encoder.Encode(encodeInfo);

                if (Transformers.Count == 0)
                {
                    bfs.MetaBlocks.Add(metaBlock);
                    encodeInfo = newEncodeInfo;
                }
                else
                {
                    var(transformedMetaBlocks, transformedState) = ApplyTransformerChain(encodeInfo.State, metaBlock, compressionParameters);

                    bfs.MetaBlocks.AddRange(transformedMetaBlocks);
                    encodeInfo = newEncodeInfo.WithState(transformedState);
                }
            }while(!encodeInfo.IsFinished);

            FinalizeStructure(bfs);
            return(bfs);
        }
Ejemplo n.º 2
0
        private void backgroundWorkerLoading_DoWork(object?sender, DoWorkEventArgs e)
        {
            BrotliDictionary dict = (BrotliDictionary)e.Argument;

            IDictionaryFormat             format     = dict.Format;
            IReadOnlyList <WordTransform> transforms = dict.Transforms;

            DataTable         table = new DataTable();
            DataRowCollection rows  = table.Rows;

            int totalProgressPoints   = format.WordLengths.Sum(format.WordCount);
            int currentProgressPoints = 0;
            int lastPercentReported   = -1;

            table.Columns.AddRange(new DataColumn[] { colLength, colIndex, colTransform, colText });
            table.MinimumCapacity = totalProgressPoints * transforms.Count;
            table.BeginLoadData();

            foreach (int length in format.WordLengths)
            {
                for (int word = 0, count = format.WordCount(length); word < count; word++)
                {
                    if (backgroundWorkerLoading.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    byte[] bytes = dict.ReadRaw(length, word);

                    for (int transform = 0; transform < transforms.Count; transform++)
                    {
                        DataRow row = table.NewRow();

                        row[colLength]    = length;
                        row[colIndex]     = word;
                        row[colTransform] = transform;
                        row[colText]      = Encoding.UTF8.GetString(transforms[transform].Process(bytes)).Replace(' ', FormatSpace).Replace("\r\n", FormatNewLine).Replace("\n", FormatNewLine).Replace("\r", "");

                        rows.Add(row);
                    }

                    ++currentProgressPoints;

                    int percentProgress = (int)Math.Floor(100.0 * currentProgressPoints / totalProgressPoints);

                    if (lastPercentReported != percentProgress)
                    {
                        lastPercentReported = percentProgress;
                        backgroundWorkerLoading.ReportProgress(percentProgress, currentProgressPoints * transforms.Count);
                    }
                }
            }

            table.EndLoadData();
            e.Result = table.DefaultView;
        }
Ejemplo n.º 3
0
 public FormStaticDictionary(BrotliDictionary dict)
 {
     InitializeComponent();
     dataGridViewWords.EnableDoubleBuffering();
     backgroundWorkerLoading.RunWorkerAsync(dict);
 }
Ejemplo n.º 4
0
 private void OpenFileWithPipeline(BrotliDictionary dictionary, BrotliEncodePipeline pipeline)
 {
     OpenFileWith("Open File to Encode", fileName => fileGenerated.EncodeFile(fileName, pipeline, dictionary));
 }
Ejemplo n.º 5
0
 public Builder(BrotliFileParameters original){
     WindowSize = original.WindowSize;
     Dictionary = original.Dictionary;
 }