Exemple #1
0
        /// <summary>
        /// Реализует поблочную запись в файл
        /// </summary>
        /// <exception cref="System.UnauthorizedAccessException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void WriteFile()
        {
            bool fileHadHiddenAttribute = (File.Exists(fileToWritePath) && TryToRemoveHiddenAtribute(fileToWritePath));
            int  blockToWriteNumber     = 0;

            try
            {
                using Writer fileWriter = new Writer(fileToWritePath);
                Block blockToWrite = null;
                while (!allBlocksWriten)
                {
                    if (writeBuffer.ContainsKey(blockToWriteNumber))
                    {
                        if (writeBuffer[blockToWriteNumber] != null)
                        {
                            writeBuffer.TryRemove(blockToWriteNumber, out blockToWrite);
                            blockToWriteNumber++;
                            fileWriter.Write(blockToWrite);
                        }
                    }

                    if (writeBuffer.Count < 100)
                    {
                        canAddBlockToBuffer = true;
                    }

                    if (blocksReaded == fileWriter.TotalBlockWrite && !readQueue.IsWaitingForEnqueue)
                    {
                        allBlocksWriten = true;
                    }
                }
            }

            catch (IOException e)
            {
                if (fileHadHiddenAttribute)
                {
                    SetHiddenAttribute();
                }

                IOExceptionHandler.Run(e.Message, args, "Writer");
            }

            catch (UnauthorizedAccessException e)
            {
                if (fileHadHiddenAttribute)
                {
                    SetHiddenAttribute();
                }

                UnauthorizedAccessExceptionHandler.Run(e.Message, args, "Writer");
            }

            if (fileHadHiddenAttribute)
            {
                SetHiddenAttribute();
            }
        }
Exemple #2
0
        /// <summary>
        /// Реализует поблочное чтение из файла
        /// </summary>
        /// <exception cref="System.UnauthorizedAccessException"></exception>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadFile()
        {
            Thread readerThread = new Thread(() =>
            {
                Reader fileReader = null;
                try
                {
                    fileReader = new Reader(fileToReadPath);
                    while (fileReader.CanRead)
                    {
                        Block block = fileReader.ReadNextBlock();
                        readQueue.Enqueue(block);
                        blocksReaded++;
                    }
                }

                catch (IOException e)
                {
                    IOExceptionHandler.Run(e.Message, args, "Reader");
                }

                catch (UnauthorizedAccessException e)
                {
                    UnauthorizedAccessExceptionHandler.Run(e.Message, args, "Reader");
                }

                catch (Exception e)
                {
                    OnException?.Invoke(e);
                }

                finally
                {
                    if (fileReader != null)
                    {
                        fileReader.Dispose();
                    }
                }

                readQueue.IsWaitingForEnqueue = false;
                blocksReaded--;
                Console.WriteLine(Thread.CurrentThread.Name + " ended");
                countdown.Signal();
            });

            readerThread.Name = "ReaderThread";
            readerThread.Start();
        }