public void Execute() { ParseFileNames(); List <ArgumentOption> argumentOptions = GetArgumentOptions(); bool isArgumentTypesNotSimular = argumentOptions.Select(ao => ao.Type).Distinct().Count() != 1; if (isArgumentTypesNotSimular) { throw new ApplicationException("Arguments must be only input or output options"); } ArgumentType type = argumentOptions.First().Type; if (type == ArgumentType.Input) { argumentOptions.Reverse(); } IInputStream inputStream = new FileInputStream(_inputFileName); IOutputStream outputStream = new FileOutputStream(_outputFileName); DecorateByArgumentOptions(argumentOptions, ref inputStream, ref outputStream); WriteFromTo(inputStream, outputStream); inputStream.Dispose(); outputStream.Dispose(); }
private async Task <OperationResult <MediaModel> > UploadPhoto(string path) { System.IO.Stream stream = null; FileInputStream fileInputStream = null; try { var photo = new Java.IO.File(path); fileInputStream = new FileInputStream(photo); stream = new StreamConverter(fileInputStream, null); var request = new UploadMediaModel(AppSettings.User.UserInfo, stream, System.IO.Path.GetExtension(path)); var serverResult = await Presenter.TryUploadMedia(request); return(serverResult); } catch (Exception ex) { AppSettings.Reporter.SendCrash(ex); return(new OperationResult <MediaModel>(new AppError(LocalizationKeys.PhotoProcessingError))); } finally { fileInputStream?.Close(); // ??? change order? stream?.Flush(); fileInputStream?.Dispose(); stream?.Dispose(); } }
private async Task <OperationResult <UUIDModel> > UploadMedia(GalleryMediaModel model) { model.UploadState = UploadState.UploadStart; System.IO.Stream stream = null; FileInputStream fileInputStream = null; try { var photo = new Java.IO.File(model.TempPath); fileInputStream = new FileInputStream(photo); stream = new StreamConverter(fileInputStream, null); var request = new UploadMediaModel(AppSettings.User.UserInfo, stream, System.IO.Path.GetExtension(model.TempPath)); var serverResult = await Presenter.TryUploadMedia(request); model.UploadState = UploadState.UploadEnd; return(serverResult); } catch (Exception ex) { model.UploadState = UploadState.UploadError; await AppSettings.Logger.Error(ex); return(new OperationResult <UUIDModel>(new InternalException(LocalizationKeys.PhotoUploadError, ex))); } finally { fileInputStream?.Close(); // ??? change order? stream?.Flush(); fileInputStream?.Dispose(); stream?.Dispose(); } }
public void CloseConnection() { Logger.LogInfo(nameof(Connection), nameof(CloseConnection), "called."); try { // Cancel the receive data task and dispose the rest. _TaskCancelTokenSource.Cancel(); _IsConnected = false; _OnConnectionClosed(); _InputStream.Dispose(); _OutputStream.Dispose(); _Accessory.Dispose(); _Manager.Dispose(); _OnDataReceived = null; _OnConnectionClosed = null; } catch (Exception ex) { Logger.LogError(ex); } }
public void ReadBlock_FileIsEmpty_ReadDataSizeEqualsZero() { // Arrange const int expectedResult = 0; IInputStream fileInputStream = new FileInputStream($"{TestFilesPath}/emptyFile.txt"); // Act int result = fileInputStream.ReadBlock(buffer: new byte[1], count: 1); fileInputStream.Dispose(); // Assert Assert.Equal(expectedResult, result); }
public void ReadByte_FileWithData_ReturnFirstByte() { // Arrange const int expectedResult = 0x31; IInputStream fileInputStream = new FileInputStream($"{TestFilesPath}/FileWithData.txt"); // Act int result = fileInputStream.ReadByte(); fileInputStream.Dispose(); // Assert Assert.Equal(expectedResult, result); }
public void ReadByte_FileIsEmpty_ReturnEndOfStreamIntCode() { // Arrange const int endOfFileCode = -1; IInputStream fileInputStream = new FileInputStream($"{TestFilesPath}/EmptyFile.txt"); // Act int result = fileInputStream.ReadByte(); fileInputStream.Dispose(); // Assert Assert.Equal(endOfFileCode, result); }
public void ReadBlock_FileWithData_ReturnData() { // Arrange const int expectedReadSize = 3; var expectedBuffer = new byte[] { 0x31, 0x32, 0x33 }; var buffer = new byte[3]; IInputStream fileInputStream = new FileInputStream($"{TestFilesPath}/FileWithData.txt"); // Act int readSize = fileInputStream.ReadBlock(buffer, count: 3); fileInputStream.Dispose(); // Assert Assert.Equal(expectedReadSize, readSize); Assert.Equal(expectedBuffer, buffer); }
public static bool IsFileLocked(File file) { if (!file.Exists()) { return(false); } FileInputStream stream = null; try { stream = new FileInputStream(file); } catch (FileNotFoundException) { return(false); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return(true); } catch (UnauthorizedAccessException) { return(true); } finally { if (stream != null) { stream.Dispose(); } } //file is not locked return(false); }
private void BtnExportClick(object sender, RoutedEventArgs e) { if (_FixedDoc == null) { this.BtnExport.IsEnabled = false; return; } System.Windows.Forms.SaveFileDialog fileDialog = new System.Windows.Forms.SaveFileDialog(); fileDialog.Filter = "Docx|*.Docx"; fileDialog.FileName = _FixedDocVM.CurMember.Name; if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } try { this.BtnExport.IsEnabled = false; string MemberTemplet = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OWPMemberInfo.dat"); if (!File.Exists(MemberTemplet)) { this.BtnExport.IsEnabled = true; AppFuns.ShowMessage("未找到欲导出的模板文件!", Caption: "失败"); return; } FileStream FStream = File.OpenRead(MemberTemplet); if (FStream == null) { this.BtnExport.IsEnabled = true; AppFuns.ShowMessage("读取模板文件时出现错误,是否文件正在使用?", Caption: "失败"); return; } InputStream WordStream = new FileInputStream(FStream); if (WordStream == null) { this.BtnExport.IsEnabled = true; AppFuns.ShowMessage("转换模板文件成Word对象流时出现错误?", Caption: "失败"); return; } XWPFDocument WDoc = new XWPFDocument(WordStream); if (WDoc == null) { this.BtnExport.IsEnabled = true; AppFuns.ShowMessage("转换模板文件成Word对象时出现错误?", Caption: "失败"); return; } //释放不必须的资源 FStream.Dispose(); WordStream.Dispose(); //开始导出 ExportWord(WDoc, _FixedDocVM); //写入输出文件 if (WDoc != null) { FileStream NewWordDoc = File.Create(fileDialog.FileName); WDoc.Write(NewWordDoc); NewWordDoc.Close(); WDoc.Close(); AppFuns.ShowMessage("数据导出成功!", Caption: "完成"); FileOperation.UseDefaultAppOpenFile(fileDialog.FileName); } else { AppFuns.ShowMessage("数据导出失败!", Caption: "失败"); } this.BtnExport.IsEnabled = true; } catch (Exception Ex) { this.BtnExport.IsEnabled = true; AppFuns.ShowMessage(Ex.Message, Caption: "失败"); } }