Exemplo n.º 1
0
        public static void LoadFile(string path, Action<string, Stream> onSuccessfulLoad, TextWriter errorOut)
        {
            const string ioExceptionPreamble = "Could not read program file due to an IO Exception.";
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, fileBufferSize, true);
            }
            catch (FileNotFoundException fileNotFoundException)
            {
                errorOut.WriteLine("Could not read program file." + fileNotFoundException.Message);
                return;
            }
            catch (IOException ioException)
            {
                errorOut.Write(ioExceptionPreamble + ioException.Message);
                return;
            }

            LoadState loadState = new LoadState(fileBufferSize);
            int bytesRead = -1;
            Func<bool> iterate = null;
            AsyncCallback onEndRead = null;
            Action onLoadComplete = null;

            iterate = () =>
            {
                while (bytesRead != 0)
                {
                    IAsyncResult result = fileStream.BeginRead(loadState.ReadBuffer, 0, fileBufferSize, onEndRead, null);
                    if (!result.CompletedSynchronously)
                    {
                        return false;
                    }
                    bytesRead = fileStream.EndRead(result);
                    loadState.FlushReadBuffer(bytesRead);
                }
                return true;
            };

            onEndRead = (ar) =>
            {
                if (!ar.CompletedSynchronously)
                {
                    try
                    {
                        bytesRead = fileStream.EndRead(ar);
                        loadState.FlushReadBuffer(bytesRead);
                        if (iterate())
                        {
                            onLoadComplete();
                        }
                    }
                    catch (IOException ioException)
                    {
                        errorOut.Write(ioExceptionPreamble + ioException.Message);
                        fileStream.Close();
                    }
                    catch
                    {
                        fileStream.Close();
                        throw;
                    }
                }
            };

            onLoadComplete = () =>
            {
                fileStream.Close();
                loadState.BufferedFile.Seek(0, SeekOrigin.Begin);
                onSuccessfulLoad(path, loadState.BufferedFile);
            };

            try
            {
                if (iterate())
                {
                    onLoadComplete();
                }
            }
            catch (IOException ioException)
            {
                errorOut.Write(ioExceptionPreamble + ioException.Message);
                fileStream.Close();
            }
            catch
            {
                fileStream.Close();
                throw;
            }
        }
Exemplo n.º 2
0
        public static void LoadFile(string path, Action <string, Stream> onSuccessfulLoad, TextWriter errorOut)
        {
            const string ioExceptionPreamble = "Could not read program file due to an IO Exception.";
            FileStream   fileStream          = null;

            try
            {
                fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, fileBufferSize, true);
            }
            catch (FileNotFoundException fileNotFoundException)
            {
                errorOut.WriteLine("Could not read program file." + fileNotFoundException.Message);
                return;
            }
            catch (IOException ioException)
            {
                errorOut.Write(ioExceptionPreamble + ioException.Message);
                return;
            }

            LoadState     loadState      = new LoadState(fileBufferSize);
            int           bytesRead      = -1;
            Func <bool>   iterate        = null;
            AsyncCallback onEndRead      = null;
            Action        onLoadComplete = null;

            iterate = () =>
            {
                while (bytesRead != 0)
                {
                    IAsyncResult result = fileStream.BeginRead(loadState.ReadBuffer, 0, fileBufferSize, onEndRead, null);
                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }
                    bytesRead = fileStream.EndRead(result);
                    loadState.FlushReadBuffer(bytesRead);
                }
                return(true);
            };

            onEndRead = (ar) =>
            {
                if (!ar.CompletedSynchronously)
                {
                    try
                    {
                        bytesRead = fileStream.EndRead(ar);
                        loadState.FlushReadBuffer(bytesRead);
                        if (iterate())
                        {
                            onLoadComplete();
                        }
                    }
                    catch (IOException ioException)
                    {
                        errorOut.Write(ioExceptionPreamble + ioException.Message);
                        fileStream.Close();
                    }
                    catch
                    {
                        fileStream.Close();
                        throw;
                    }
                }
            };

            onLoadComplete = () =>
            {
                fileStream.Close();
                loadState.BufferedFile.Seek(0, SeekOrigin.Begin);
                onSuccessfulLoad(path, loadState.BufferedFile);
            };

            try
            {
                if (iterate())
                {
                    onLoadComplete();
                }
            }
            catch (IOException ioException)
            {
                errorOut.Write(ioExceptionPreamble + ioException.Message);
                fileStream.Close();
            }
            catch
            {
                fileStream.Close();
                throw;
            }
        }