Ejemplo n.º 1
0
        /// <summary>
        /// Flushes a file's contents.
        /// </summary>
        /// <param name="sender">The file to flush.</param>
        /// <param name="e">This is not used.</param>
        /// <exception cref="InvalidOperationException">
        /// The file has been closed.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// Overriding contents not supported.
        /// </exception>
        private void Flush(object sender, EventArgs e)
        {
            Fat32FileStream file = sender as Fat32FileStream;

            if (!openedFiles.Contains(file))
            {
                throw new InvalidOperationException("The file has been closed.");
            }

            if (file.Entry.FileSize != 0)
            {
                throw new NotSupportedException("Overriding contents not supported.");
            }

            file.Entry.FileSize = (uint)file.Length;
            file.Position       = 0;

            byte[] buffer  = new byte[boot.BytesPerCluster];
            long   cluster = -1;

            while (file.Position != file.Length)
            {
                cluster = GetFreeCluster(cluster);
                if (file.Position == 0)
                {
                    file.Entry.FirstCluster = (uint)cluster;
                }

                Source.Position = FirstSectorOfCluster((uint)cluster) * boot.BytesPerSector;

                int bytesRead = file.Read(buffer, 0, buffer.Length);
                Source.Write(buffer, 0, buffer.Length);
            }
        }