Beispiel #1
0
        async Task Save(string fileName,
                        string fileContents)
        {
            if (!IsFileSystemReady)
            {
                ShowErrorMessage("File system is not open", PPError.Failed);
                return;
            }

            using (var fileref = new FileRef(fileSystem, fileName))
                using (var file = new PepperSharp.FileIO(this))
                {
                    var openResult = await file.OpenAsync(fileref,
                                                          FileOpenFlags.Write | FileOpenFlags.Create | FileOpenFlags.Truncate);

                    if (openResult != PPError.Ok)
                    {
                        ShowErrorMessage("File open for write failed", openResult);
                        return;
                    }

                    // We have truncated the file to 0 bytes. So we need only write if
                    // file_contents is non-empty.
                    if (!string.IsNullOrEmpty(fileContents.ToString()))
                    {
                        if (fileContents.Length > Int32.MaxValue)
                        {
                            ShowErrorMessage("File too big", PPError.Filetoobig);
                            return;
                        }
                        int offset       = 0;
                        int bytesWritten = 0;
                        var byteContents = Encoding.UTF8.GetBytes(fileContents);
                        do
                        {
                            var writeResult = await file.WriteAsync(byteContents,
                                                                    offset, byteContents.Length);

                            if (writeResult.EndOfFile)
                            {
                                return;
                            }

                            bytesWritten = writeResult.Count;

                            if (bytesWritten > 0)
                            {
                                offset += bytesWritten;
                            }
                            else
                            {
                                ShowErrorMessage("File write failed", bytesWritten);
                                return;
                            }
                        } while (bytesWritten < fileContents.Length);
                    }
                    // All bytes have been written, flush the write buffer to complete
                    var flush_result = await file.FlushAsync();

                    if (flush_result != PPError.Ok)
                    {
                        ShowErrorMessage("File fail to flush", flush_result);
                        return;
                    }


                    ShowStatusMessage("Save success");
                }
        }
Beispiel #2
0
        async Task Load(string fileName)
        {
            if (!IsFileSystemReady)
            {
                ShowErrorMessage("File system is not open", PPError.Failed);
                return;
            }

            using (var fileref = new FileRef(fileSystem, fileName))
                using (var file = new PepperSharp.FileIO(this))
                {
                    var openResult = await file.OpenAsync(fileref, FileOpenFlags.Read);

                    if (openResult == PPError.Filenotfound)
                    {
                        ShowErrorMessage("File not found", openResult);
                        return;
                    }
                    else if (openResult != PPError.Ok)
                    {
                        ShowErrorMessage("File open for read failed", openResult);
                        return;
                    }

                    var queryResult = await file.QueryAsync();

                    if (queryResult.QueryResult != PPError.Ok)
                    {
                        ShowErrorMessage("File query failed", queryResult.QueryResult);
                        return;
                    }

                    // FileIO.Read() can only handle int32 sizes
                    if (queryResult.Size > Int32.MaxValue)
                    {
                        ShowErrorMessage("File too big", PPError.Filetoobig);
                        return;
                    }

                    int numBytesRead     = 0;
                    int numBytesToRead   = (int)queryResult.Size;
                    var bytes            = new byte[numBytesToRead];
                    var dataBufferString = new StringBuilder();

                    var readBuffer = new ArraySegment <byte>(bytes);

                    while (numBytesToRead > 0)
                    {
                        var readResult = await file.ReadAsync(readBuffer, numBytesRead,
                                                              readBuffer.Count, messageLoop);

                        if (readResult.EndOfFile)
                        {
                            break;
                        }

                        if (readResult.Count > 0)
                        {
                            byte[] readBytes = readBuffer.Skip(readBuffer.Offset).Take((int)readResult.Count).ToArray();
                            dataBufferString.Append(Encoding.UTF8.GetString(readBytes));
                        }
                        else
                        {
                            ShowErrorMessage("File read failed", numBytesRead);
                            return;
                        }

                        numBytesRead   += readResult.Count;
                        numBytesToRead -= readResult.Count;
                    }
                    PostArrayMessage("DISP", dataBufferString.ToString());
                    ShowStatusMessage("Load success");
                }
        }