Exemple #1
0
        public void Write(StreamAction action)
        {
            string tmpFile = null;

            try {
                tmpFile = CreateTempFile("config-", ".tmp", Path.GetDirectoryName(_configFile));
                MD5 outputDigest        = MD5.Create();
                OutputFileStream output = new OutputFileStream(tmpFile, outputDigest);
                try {
                    action(output);
                } finally {
                    output.Close();
                }
                if (inputDigest == null || !File.Exists(_configFile) ||
                    !ArraysAreEqual(inputDigest, output.ComputedHash))
                {
                    if (File.Exists(_configFile))
                    {
                        File.Delete(_configFile);
                    }
                    File.Move(tmpFile, _configFile);
                }
            } catch (Exception e) {
                System.Console.Error.WriteLine("do not write config. Error occured: " + e);
            } finally {
                if (tmpFile != null)
                {
                    File.Delete(tmpFile);
                }
            }
        }
Exemple #2
0
        private async void GeneratorLoop(int count, string identifier)
        {
            string Output = "";

            for (int i = 0; i < count; i++)
            {
                await Task.Run(() =>
                {
                    string Line = identifier.ToString() + i.ToString() + ",10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000";
                    Output     += Line + "\n";
                });
            }

            try
            {
                int retry = 0;

                while (retry < 5)
                {
                    try
                    {
                        await OutputFileStream.WriteLineAsync(Output).ConfigureAwait(false);

                        //await OutputFileStream.FlushAsync().ConfigureAwait(false);

                        break;
                    }
                    catch { await Task.Delay(500); }
                }

                if (retry >= 5)
                {
                    StatusUpdate("Failed", "Unable to add generated data set to the file " + FilePath);
                }
            }
            catch (Exception ex)
            {
                StatusUpdate("Error", ex.Message);
            }

            CompletedTaskCount++;
            CurrentProgressValue = (int)((double)CompletedTaskCount / (double)TotalTasksCount * 100);
        }
Exemple #3
0
 public void Write(StreamAction action)
 {
     string tmpFile = null;
     try {
         tmpFile = CreateTempFile("config-", ".tmp", Path.GetDirectoryName(_configFile));
         MD5 outputDigest = MD5.Create();
         OutputFileStream output = new OutputFileStream(tmpFile, outputDigest);
         try {
             action(output);
         } finally {
             output.Close();
         }
         if (inputDigest == null || !File.Exists(_configFile) ||
             !ArraysAreEqual(inputDigest, output.ComputedHash)) {
             if (File.Exists(_configFile))
                 File.Delete(_configFile);
             File.Move(tmpFile, _configFile);
         }
     } catch (Exception e) {
         System.Console.Error.WriteLine("do not write config. Error occured: " + e);
     } finally {
         if (tmpFile != null)
             File.Delete(tmpFile);
     }
 }
Exemple #4
0
        public void CodingFiles(string PathSourseFile, string PathEndFile, string endFileNameAndFolder)
        {
            byte[] WorkingString = File.ReadAllBytes(PathSourseFile);

            //FileStream inputFile = File.Open(PathSourseFile, FileMode.Open);

            for (int i = 0; i < 256; i++)//обнуление частотного словаря
            {
                Dict[i] = 0;
            }/*
              * for (int i = 0; i < inputFile.Length; i++)
              * {
              * int byt = inputFile.ReadByte();
              * Dict[byt] = Dict[byt] + 1;
              * }*/
             //inputFile.Seek(0, SeekOrigin.Begin);

            foreach (byte item in WorkingString)//создание частотного словаря
            {
                Dict[item] = Dict[(int)item] + 1;
            }

            List <Node> t = new List <Node>();

            for (int i = 0; i < 256; i++)//создание листа нодов из словаря
            {
                t.Add(new Node {
                    C = (byte)i, N = Dict[i]
                });
            }

            while (t.Count != 1)//отбор с самых низких значений, и создание дерева нодов с одним оставшимся рутом
            {
                Node a1 = t[0];

                foreach (Node item in t)
                {
                    if (a1.N > item.N)
                    {
                        a1 = item;
                    }
                }
                t.Remove(a1);

                Node a2 = t[0];

                foreach (Node item in t)
                {
                    if (a2.N > item.N)
                    {
                        a2 = item;
                    }
                }
                t.Remove(a2);

                Node p = new Node()
                {
                    left = a1, right = a2, N = a1.N + a2.N
                };
                t.Add(p);
            }
            Node root = t[0];

            table.Clear();    //очистка словаря
            BuildTable(root); //рекурсивная функция записи кодов в словарь

            int countOfBits = 0;

            for (int i = 0; i < 256; i++)
            {
                countOfBits += table[(byte)i].Count * Dict[(byte)i];//подсчёт размера массива байтоБит
            }


            ////////////////////////////////////////////////////////////////////////////////
            //сохранение файла
            string filename = endFileNameAndFolder;

            byte[] filNamBytStr = new byte[filename.Length];
            for (int i = 0; i < filename.Length; i++)
            {
                filNamBytStr[i] = (byte)filename[i];
            }

            FileStream OutputFileStream;

            OutputFileStream = File.Open(PathEndFile, FileMode.Append);
            OutputFileStream.WriteByte((byte)filename.Length);             //запись количества букв в названии макс 256
            OutputFileStream.Write(filNamBytStr, 0, filNamBytStr.Count()); //запись названия файла

            for (int i = 0; i < 256; i++)                                  //запись частоты повторов
            {
                int    tem = Dict[i];
                byte[] x   = new byte[4];
                x[0] = (byte)(tem & 0xff);
                x[1] = (byte)((tem >> 8) & 0xff);
                x[2] = (byte)((tem >> 16) & 0xff);
                x[3] = (byte)(tem >> 24);

                OutputFileStream.Write(x, 0, 4);
            }

            int MassSize = (countOfBits / 8) + 1;//запись размера конечного файла

            //int MassSize = OutFileMass.Count();
            byte[] xx = new byte[4];
            xx[0] = (byte)(MassSize & 0xff);
            xx[1] = (byte)((MassSize >> 8) & 0xff);
            xx[2] = (byte)((MassSize >> 16) & 0xff);
            xx[3] = (byte)(MassSize >> 24);

            OutputFileStream.Write(xx, 0, 4);
            /////////////////////////////

            //string tempStr = "";
            //byte[] OutFileMass = new byte[(countOfBits / 8) + 1];//создание массива байт для сохранения
            int counterForOutFileMass = 0;

            /*
             *          for (int i = 0; i < inputFile.Length; i++)
             *          {
             *              byte tempByte = (byte)inputFile.ReadByte();
             *
             *              List<bool> tem = table[tempByte];
             *              foreach (bool item in tem)
             *              {
             *                  if (tempStr.Length == 8)
             *                  {
             *                      OutputFileStream.WriteByte(Convert.ToByte(tempStr, 2));
             *                      counterForOutFileMass++;
             *                      //OutFileMass[counterForOutFileMass++] = Convert.ToByte(tempStr, 2);
             *                      tempStr = "";
             *                  }
             *
             *                  if (item)
             *                  {
             *                      tempStr = tempStr + "1";
             *                  }
             *                  else
             *                  {
             *                      tempStr = tempStr + "0";
             *                  }
             *              }
             *
             *          }*/
            int counterforBit = 0;

            int[] MassBit = new int[8];

            int probe           = 0;
            int counterForProbe = 0;

            foreach (byte b in WorkingString)
            {
                List <bool> tem = table[b];
                foreach (bool item in tem)
                {
                    if (counterforBit /*counterForProbe*/ == 8)
                    {
                        int resbyte = 0;
                        for (int i = 0; i < 8; i++)
                        {
                            resbyte |= MassBit[i] << 7 - i;
                        }
                        OutputFileStream.WriteByte((byte)resbyte);
                        counterForOutFileMass++;
                        counterforBit = 0;

                        /*
                         * OutputFileStream.WriteByte((byte)probe);
                         * probe = 0;
                         * counterForProbe = 0;*/
                    }
                    if (item)
                    {
                        //probe |= 1 << 7 - counterForProbe++;
                        MassBit[counterforBit++] = 1;
                    }
                    else
                    {
                        //probe |= 0 << 7 - counterForProbe++;
                        MassBit[counterforBit++] = 0;
                    }
                }
            }

            if (counterforBit /*counterForProbe*/ > 0)
            {
                for (; counterforBit /*counterForProbe*/ < 8;)
                {
                    MassBit[counterforBit++] = 0;
                    //probe |= 0 << 7 - counterForProbe++;
                }

                int resbyte = 0;
                for (int i = 0; i < 8; i++)
                {
                    resbyte |= MassBit[i] << 7 - i;
                }
                OutputFileStream.WriteByte((byte)resbyte);
                counterForOutFileMass++;
                counterforBit = 0;

                //OutputFileStream.WriteByte((byte)probe);
            }

            if (counterForOutFileMass /*counterForProbe */ < MassSize)
            {
                OutputFileStream.WriteByte(0);
                counterForOutFileMass++;
            }

            OutputFileStream.Close();
        }