Exemplo n.º 1
0
        /// <summary>
        /// Reads a file from disc and stores its content into a list of byte buffer.
        /// </summary>
        /// <param name="completePath">The complete path to the file</param>
        public void ReadFileFromDisc(string completePath)
        {
            try
            {
                byte[] buffer;
                int    concreteBufferSize;

                FileInfo info     = new FileInfo(completePath);
                int      fileSize = (int)info.Length;

                int streamPosition = 0;

                using (FileStream stream = info.OpenRead())
                {
                    while (true)
                    {
                        if (streamPosition + BufferSize < fileSize)
                        {
                            concreteBufferSize = BufferSize;
                        }
                        else
                        {
                            concreteBufferSize = fileSize - streamPosition;
                        }

                        buffer = new byte[concreteBufferSize];
                        stream.Read(buffer, 0, concreteBufferSize);
                        streamPosition += concreteBufferSize;

                        ByteArray newByteArray = new ByteArray(buffer);
                        byteBuffers.Add(newByteArray);

                        IUpdateAction addNewByteArrayAction  = new UpdateStoreAction(newByteArray);
                        IUpdateAction deactivateNewByteArray = new DeactivateAction(newByteArray, 1);
                        DatabaseManager.Instance.PerformDataUpdate(addNewByteArrayAction,
                                                                   deactivateNewByteArray);

                        if (streamPosition == fileSize)
                        {
                            break;
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                throw new DiscAccessException(String.Format("{0}{1}{2}",
                                                            "Could not read the file \"",
                                                            completePath,
                                                            "\" from disc."), ex);
            }

            finally
            {
                IUpdateAction addByteArrayListAction = new UpdateStoreAction(byteBuffers);
                DatabaseManager.Instance.PerformDataUpdate(addByteArrayListAction);
            }
        }
Exemplo n.º 2
0
        public void UpdateDatabase()
        {
            try
            {
                IUpdateAction updateBooksDataAction = new UpdateStoreAction(this);
                DatabaseManager.Instance.PerformDataUpdate(updateBooksDataAction);
            }

            catch (DatabaseException ex)
            {
                throw new BookEntitiesException(ex.Message, ex);
            }
        }