Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 3, 4, 6, 10, 100 };

            BinFileWriter.CreateFile(arr, "test.bin");
            BinFileReader.ReadFile("test.bin");

            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method for compressing data from BinFileReader and write into BinFileWriter
        /// </summary>
        private void Compress()
        {
            this.BinFileReader.BaseStream.Position = 0;
            this.BinFileWriter.BaseStream.Position = 0;

            BinFileWriter.Write((UInt16)0);
            this.PreviousStructInStream = 0;

            this.SlidingWindow = new System.Collections.Generic.List <byte>();

            for (; this.BinFileReader.BaseStream.Position < this.BinFileReader.BaseStream.Length;)
            {
                /// Запомнили стартовую позицию указателя
                this.StreamPositionBuffer = this.BinFileReader.BaseStream.Position;
                this.IndexOfEquals        = null;
                this.IsLast = false;
                this.Offset = 0;

                /// Читаем символ, и проверяем соответствие в скользящем окне
                for (; this.BinFileReader.BaseStream.Position < this.BinFileReader.BaseStream.Length;)
                {
                    this.ReadedByte = this.BinFileReader.ReadByte();
                    /// Подготовка списка кондидитов на удаление для текущей итерации
                    this.IndexForRemove = new System.Collections.Generic.List <int>();

                    /// Создание буфера соответствий
                    if (this.IndexOfEquals == null)
                    {
                        this.IndexOfEquals = new System.Collections.Generic.List <Int32>();

                        this.IndexOfEquals = this.SlidingWindow.Select((x, i) => (x == this.ReadedByte) ? i : -1).Where(x => x != -1).ToList();

                        /// Не найдено не одного соответствия
                        /// = Заход в первый раз.
                        if (this.IndexOfEquals.Count < 1)
                        {
                            ++this.Offset;
                            break;
                        }
                    }
                    else
                    {
                        foreach (Int32 x in IndexOfEquals)
                        {
                            /// Если не достигнут конец скользящего окна
                            if ((x + this.Offset) < SlidingWindow.Count)
                            {
                                /// Если элемент в окне не соответствует считанному с потока
                                if (this.SlidingWindow[x + this.Offset] != this.ReadedByte)
                                {
                                    /// Помечаем как кондидат на удаление
                                    this.IndexForRemove.Add(x);
                                }
                            }
                            else
                            {
                                /// Помечаем как кондидат на удаление
                                this.IndexForRemove.Add(x);
                            }
                        }

                        this.HelpDeleateOffset = 0;
                        foreach (Int32 x in IndexForRemove)
                        {
                            /// Если индекс последний
                            if (IndexOfEquals.Count() <= 1)
                            {
                                this.IsLast = true;
                                break;
                            }
                            else
                            {
                                this.IndexOfEquals.Remove(x);
                                ++this.HelpDeleateOffset;
                                continue;
                            }
                        }
                    }
                    if (IsLast)
                    {
                        break;
                    }
                    ++this.Offset;
                }

                /// Не пишим промежуточные(дополнительные отметки)
                if (this.Offset > (Method.MinArchivingCount - 1))
                {
                    this.BinFileReader.BaseStream.Position = this.StreamPositionBuffer;
                    this.InputByteBuffer = this.BinFileReader.ReadBytes(this.Offset);
                    //BinFileWriter.Write((UInt16)0);


                    this.BinFileWriter.BaseStream.Position = this.PreviousStructInStream;
                    this.BinFileWriter.Write((UInt16)(this.BinFileWriter.BaseStream.Length - 4 - this.BinFileWriter.BaseStream.Position));
                    this.BinFileWriter.BaseStream.Position = this.BinFileWriter.BaseStream.Length;
                    this.PreviousStructInStream            = this.BinFileWriter.BaseStream.Length;
                    BinFileWriter.Write((UInt16)0);
                    BinFileWriter.Write((UInt16)this.IndexOfEquals[0]);
                    BinFileWriter.Write((UInt16)this.Offset);

                    /*
                     * this.BinFileWriter.BaseStream.Position = this.PreviousStructInStream;
                     * /// 4 = 2 * Sizeof(BufferLengthByte)
                     * this.BinFileWriter.Write((UInt16)(this.BinFileWriter.BaseStream.Length - 4 - this.BinFileWriter.BaseStream.Position));
                     * /// 2 = Sizeof(BufferLengthByte)
                     * this.BinFileWriter.BaseStream.Position = this.BinFileWriter.BaseStream.Length;
                     * this.PreviousStructInStream = this.BinFileWriter.BaseStream.Position - 2;
                     *
                     * BinFileWriter.Write((UInt16)this.IndexOfEquals[0]);
                     * BinFileWriter.Write((UInt16)this.Offset);*////?qwe_??qwsa

                    /*
                     * this.IndexOfEquals.Add((UInt16)(this.BinFileWriter.BaseStream.Position - this.PreviousStructInStream));
                     * this.StreamPositionBuffer = this.BinFileWriter.BaseStream.Position;
                     * this.BinFileWriter.BaseStream.Position = this.PreviousStructInStream;
                     * this.BinFileWriter.Write(this.IndexOfEquals.Last().ToString());
                     * this.BinFileWriter.BaseStream.Position = this.StreamPositionBuffer + 1;
                     * this.PreviousStructInStream = this.StreamPositionBuffer - 1;
                     */

                    /// Запись в скользящее окно
                    this.SlidingWindow.AddRange(InputByteBuffer);
                    if (this.SlidingWindow.Count - Method.BufferLengthByte > 0)
                    {
                        this.SlidingWindow.RemoveRange(0, (this.SlidingWindow.Count) - Method.BufferLengthByte);
                    }
                    this.IndexOfEquals.Clear();
                }
                else
                {
                    this.BinFileReader.BaseStream.Position = this.StreamPositionBuffer;
                    /// Чтение знаков из входного потока
                    this.InputByteBuffer = this.BinFileReader.ReadBytes(this.Offset);
                    /// Запись в выходной
                    this.BinFileWriter.Write(InputByteBuffer);
                    /// Запись в скользящее окно
                    this.SlidingWindow.AddRange(this.InputByteBuffer);

                    /// Чтобы не вылетал, когда не заполнен под завязку
                    if (this.SlidingWindow.Count - Method.BufferLengthByte > 0)
                    {
                        this.SlidingWindow.RemoveRange(0, this.SlidingWindow.Count - Method.BufferLengthByte);
                    }
                    this.IndexOfEquals.Clear();
                }
            }
        }