public PackedFile(string documentsPath, string metadataPath, JournalWriter journalWriter, IFileSystem fileSystem) { memStream = new MemoryStream(); memWriter = new BinaryWriter(memStream); this.documentsPath = documentsPath; pagesPath = metadataPath; this.journalWriter = journalWriter; this.fileSystem = fileSystem; documentsStream = fileSystem.OpenFileStream(documentsPath); pagesStream = fileSystem.OpenFileStream(pagesPath); if (pagesStream.Length >= 1) { if (documentsStream.Length < 1) { throw new CorruptionException("Documents is empty but metadata isn't"); } ReadFileHeader(pagesStream); ReadFileHeader(documentsStream); } else { documentsStream.WriteByte(0); pagesStream.WriteByte(0); } documentsReader = new BinaryReader(documentsStream); pagesReader = new BinaryReader(pagesStream); }
public JournalPlayer(string path, IFileSystem fileSystem) { this.fileSystem = fileSystem; journalStream = fileSystem.OpenFileStream(path); writer = new BinaryWriter(journalStream, Encoding.UTF8); reader = new BinaryReader(journalStream, Encoding.UTF8); }
public JournalWriter(string path, IFileSystem fileSystem) { this.path = path; this.fileSystem = fileSystem; stream = fileSystem.OpenFileStream(path); writer = new BinaryWriter(stream, Encoding.UTF8); }
public CodeBase Analyze(MetricsCommandArguments details) { var command = collectionStepFactory.GetStep(details.RepositorySourceType); details.MetricsOutputFolder = fileSystem.MetricsOutputFolder; details.BuildOutputFolder = fileSystem.GetProjectBuildFolder(details.ProjectName); fileSystem.CreateFolder(details.BuildOutputFolder); var metricsResults = command.Run(details); var codeBase = CodeBase.Empty(); foreach (var x in metricsResults) { var filename = x.MetricsFile; var cb = codebaseService.Get(fileSystem.OpenFileStream(filename), x.ParseType); codeBase.Enrich(new CodeGraph(cb.AllInstances)); } var codebase = analyzerFactory.For(details.RepositorySourceType).Analyze(codeBase.AllInstances); codebase.SourceType = details.RepositorySourceType; codebase.Name = details.ProjectName; return(codebase); }
/// <summary> /// Copies the specified file to the target directory. /// </summary> /// <param name="sourceFileSystem">The source file system.</param> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception> public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory) { sourceFileSystem.ThrowIfNull(() => sourceFileSystem); sourceFile.ThrowIfNull(() => sourceFile); targetDirectory.ThrowIfNull(() => targetDirectory); if (!(targetDirectory is LocalDirectoryInfo)) { throw new ArgumentException("The target directory must be of type LocalDirectoryInfo.", "targetDirectory"); } try { using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile)) { string targetFilePath = this.CombinePath(targetDirectory.FullName, sourceFile.Name); try { using (FileStream targetStream = File.Create(targetFilePath)) { if (sourceFile.Length > 0) { var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 256 * 1024, true); copyOperation.CopyProgressChanged += (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e); copyOperation.Execute(); } } } catch (IOException) { File.Delete(targetFilePath); throw; } } } catch (UnauthorizedAccessException ex) { throw new AccessException("The file could not be accessed.", ex); } catch (SecurityException ex) { throw new AccessException("The file could not be accessed.", ex); } catch (IOException ex) { throw new AccessException("The file could not be accessed.", ex); } }
/// <summary> /// Copies the specified file to the target directory. /// </summary> /// <param name="sourceFileSystem">The source file system.</param> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception> public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory) { sourceFileSystem.ThrowIfNull(() => sourceFileSystem); sourceFile.ThrowIfNull(() => sourceFile); targetDirectory.ThrowIfNull(() => targetDirectory); if (!(targetDirectory is LocalDirectoryInfo)) throw new ArgumentException("The target directory must be of type LocalDirectoryInfo.", "targetDirectory"); try { using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile)) { string targetFilePath = this.CombinePath(targetDirectory.FullName, sourceFile.Name); try { using (FileStream targetStream = File.Create(targetFilePath)) { if (sourceFile.Length > 0) { var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 256 * 1024, true); copyOperation.CopyProgressChanged += (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e); copyOperation.Execute(); } } } catch (IOException) { File.Delete(targetFilePath); throw; } } } catch (UnauthorizedAccessException ex) { throw new AccessException("The file could not be accessed.", ex); } catch (SecurityException ex) { throw new AccessException("The file could not be accessed.", ex); } catch (IOException ex) { throw new AccessException("The file could not be accessed.", ex); } }
private void ApplyEntries(int curEntryIndex, int numEntries) { for (int i = curEntryIndex; i < numEntries; i++) { JournalEntryType journalEntryType = (JournalEntryType)reader.ReadByte(); switch (journalEntryType) { case JournalEntryType.Resize: { string path3 = reader.ReadString(); Stream targetFileStream3 = fileSystem.OpenFileStream(path3); ResizeEntry entry3 = ReadResizeEntry(); ApplyResizeEntry(targetFileStream3, entry3); break; } case JournalEntryType.Write: { string path2 = reader.ReadString(); Stream targetFileStream2 = fileSystem.OpenFileStream(path2); WriteEntry entry2 = ReadWriteEntry(); ApplyWriteEntry(targetFileStream2, entry2); break; } case JournalEntryType.Copy: { string path = reader.ReadString(); Stream targetFileStream = fileSystem.OpenFileStream(path); CopyEntry entry = ReadCopyEntry(); ApplyCopyEntry(targetFileStream, entry); break; } default: throw new CorruptionException("Unhandled entry type: " + journalEntryType); } curEntryIndex++; SetCurEntryIndex(curEntryIndex); } }
public void ShouldReturnFileAsStream() { MemoryStream stream = new MemoryStream(); string path = @"c:\temp\data\script.sql"; Expect.Call(fileSystem.OpenFileStream(path, FileMode.Open)).Return(stream); mocks.ReplayAll(); ScriptFile script = new ScriptFile(path); script.FileSystem = fileSystem; Assert.AreEqual(stream, script.GetContentStream()); mocks.VerifyAll(); }
/// <summary> /// Copies the specified file to the target directory. /// </summary> /// <param name="sourceFileSystem">The source file system.</param> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception> /// /// <exception cref="ArgumentException">The target directory is not of type <see cref="FlagFtp.FtpDirectoryInfo"/>.</exception> public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory) { sourceFileSystem.ThrowIfNull(() => sourceFileSystem); sourceFile.ThrowIfNull(() => sourceFile); targetDirectory.ThrowIfNull(() => targetDirectory); if (!(targetDirectory is FtpDirectoryInfo)) { throw new ArgumentException("The target directory must be of type FtpDirectoryInfo.", "targetDirectory"); } Uri targetFilePath = new Uri(this.CombinePath(targetDirectory.FullName, sourceFile.Name)); try { using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile)) { using (Stream targetStream = this.client.OpenWrite(targetFilePath)) { if (sourceFile.Length > 0) { var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 8 * 1024, true); copyOperation.CopyProgressChanged += (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e); copyOperation.Execute(); } } } } catch (WebException ex) { switch (ex.Status) { case WebExceptionStatus.ConnectFailure: case WebExceptionStatus.ProxyNameResolutionFailure: throw new FileSystemUnavailableException("The FTP server is currently unavailable.", ex); } throw new AccessException("The file could not be accessed", ex); } }
/// <summary> /// Adds specified file name and its contents to a stream in multipart request format /// </summary> private static void WriteFileToMemoryStream(Stream stream, string fieldName, string fileName, IFileSystem fileSystem) { Stream fileStream = null; try { // prepare header for every uploaded file string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n"; // add file name to postdata for current form data block string header = string.Format(headerTemplate, fieldName, fileName); byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(header); stream.Write(headerBytes, 0, headerBytes.Length); // Read file and write it to memory stream // Note: we don't verify if file exist or not. It's for testing purpose and we expect // that files are specified, if not it is a test bug. fileStream = fileSystem.OpenFileStream(fileName, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { stream.Write(buffer, 0, bytesRead); } // write boundary stream.Write(_boundaryBytes, 0, _boundaryBytes.Length); } finally { if (fileStream != null) { fileStream.Close(); } } }
public Index(string path, IFixedSizeIndexValueType <TEntryValue> valueType, Aes256Encryptor encryptor, IFileSystem fileSystem, JournalWriter journalWriter) { this.path = path; this.valueType = valueType; this.encryptor = encryptor; this.fileSystem = fileSystem; this.journalWriter = journalWriter; uint size = valueType.Size; entrySize = 4 + size; fileStream = fileSystem.OpenFileStream(path); serializedBytes = new byte[entrySize]; serializerStream = new MemoryStream(serializedBytes); serializerWriter = new BinaryWriter(serializerStream); if (fileStream.Length >= 1) { int num = fileStream.ReadByte(); if (num != 0) { throw new CorruptionException("Unsupported format version: " + num); } uint num2 = (uint)(fileStream.Length - 1); Count = num2 / entrySize; if (num2 % entrySize != 0) { uint entryPosition = GetEntryPosition(Count); fileStream.SetLength(entryPosition); } } else { fileStream.SetLength(1L); fileStream.WriteByte(0); } fileStreamReader = new BinaryReader(fileStream); }
private void Parse(IInstanceReader parser, string fileName) { var result = parser.Parse(fileSystem.OpenFileStream(fileName)); EnrichWorkspace(result); }
async private Task <bool> SaveRegistrationsAndSignatureDataToServer(Planning planning, int statusID) { try { SQLiteConnection db = App.dbHandler.db; StatusResult statusResult; //lbStatus.Text = "Sending registrations" //activityIndicator.IsRunning = activityIndicator.IsVisible = true; UpdateUI(true); var regs = from r in db.Table <Registration> () where r.PlanningID == planning.ID select r; List <rsRegistration> registrationList = new List <rsRegistration> (regs.Count() + 1); foreach (Registration r in regs) { rsRegistration reg = new rsRegistration() { ID = r.IDguid, PlanningID = r.PlanningIDguid, OrderID = r.OrderIDguid, UserID = r.UserIDguid, Date = r.Date, RegTypeID = r.RegTypeID, Priority = r.Priority, IsDisplayed = r.IsDisplayed, IsOnReport = r.IsOnReport, IsRequired = r.IsRequired, IsReadingOnly = r.IsReadingOnly, Caption = r.Caption, Result = r.Result, PathName = r.PathName, Input = r.Input, IsClientReg = r.IsClientReg, IsChanged = r.IsChanged, IsDeleted = r.IsDeleted }; registrationList.Add(reg); } //lbSendStatus.Text = text.SendingArticles; var artRegs = (from a in db.Table <ArticleReg> () where a.PlanningID == planning.ID select new rsArticleReg() { ArticleID = a.ArticleID, Article = "", OrderID = a.OrderIDguid, PlanningID = a.PlanningIDguid, Qty = a.Qty, IsChanged = a.IsChanged, IsDeleted = a.IsDeleted, PriceIn = a.PriceIn, PriceOut = a.PriceOut }).ToList <rsArticleReg> (); // Save registrations and articleregistrations to server // SaveRegistrationInput input = new SaveRegistrationInput() { userIDin = App.appSettings.loginVars.userID.ToString(), installationID = App.appSettings.installationID, registrations = registrationList, articles = artRegs }; MobRegService service = new MobRegService(ProgramVars.URL); statusResult = await service.SaveRegistrationsAsync(input); if (statusResult.statusCode != 0) { throw new Exception(statusResult.status); } // // SaveRegistrations completed OK, next SavePlanning // // input.dataIn, input.overWriteExecEndDateTime, input.includeSignatureData, input.sendReport, input.resources rsPlanning rsplanning = planning.TOrsPlanning(); var planningResources = db.Table <Resource> ().Where(r => r.PlanningID == planning.ID); if (planningResources != null && planningResources.Count() > 0) { rsplanning.Resources = new List <rsResource> (planningResources.Count()); foreach (Resource res in planningResources) { rsplanning.Resources.Add(res.TOrsResource()); } } SavePlanningInput saveInput = new SavePlanningInput() { userIDin = App.appSettings.loginVars.userID.ToString(), installationID = App.appSettings.installationID, dataIn = rsplanning, overWriteExecEndDateTime = false, includeSignatureData = true, sendReport = false, resources = Common.GetResourcesForPlanningID(planning.ID) }; statusResult = await service.SavePlanningAsync(saveInput); if (statusResult.statusCode != 0) { throw new Exception(statusResult.status); } // Save all pictures with changes and not deleted to server // var regPics = from r in db.Table <Registration> () where r.PlanningID == planning.ID && r.RegTypeID == RegistrationTypes.Picture && r.IsChanged && !r.IsDeleted select r; IFileSystem fileSystem = DependencyService.Get <IFileSystem>(); string handlerUrl, path; foreach (Registration r in regPics) { path = fileSystem.GetDataPath($"{r.ID}.jpg"); if (fileSystem.ExistsFile(path).statusCode == 0) { handlerUrl = string.Format("{0}?regid={1}&userid={2}&instid={3:d}&mode=up", ProgramVars.ImageHandlerUrl, r.ID, r.UserID, App.appSettings.installationID); statusResult = await service.SaveBinaryAsync(handlerUrl, fileSystem.OpenFileStream(path)); if (statusResult.statusCode != 0) { throw new Exception(statusResult.status); } } } //statusPanel.Visible = false; // Save signature // handlerUrl = string.Format("{0}?regid={1}&userid={2}&instid={3:d}&mode=SIGNATURE", ProgramVars.ImageHandlerUrl, planning.ID, App.appSettings.loginVars.userID.ToString(), App.appSettings.installationID); path = fileSystem.GetDataPath($"sig{planning.ID}.png"); statusResult = await service.SaveBinaryAsync(handlerUrl, fileSystem.OpenFileStream(path)); if (statusResult.statusCode != 0) { throw new Exception(statusResult.status); } //activityIndicator.IsRunning = activityIndicator.IsVisible = false; UpdateUI(true); return(true); } catch (Exception ex) { //statusPanel.Visible = false; //activityIndicator.IsRunning = activityIndicator.IsVisible = false; UpdateUI(false); await DisplayAlert(AppResources.Error, ex.Message, AppResources.OK); return(false); } }
/// <summary> /// Copies the specified file to the target directory. /// </summary> /// <param name="sourceFileSystem">The source file system.</param> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception> /// /// <exception cref="ArgumentException">The target directory is not of type <see cref="FlagFtp.FtpDirectoryInfo"/>.</exception> public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory) { sourceFileSystem.ThrowIfNull(() => sourceFileSystem); sourceFile.ThrowIfNull(() => sourceFile); targetDirectory.ThrowIfNull(() => targetDirectory); if (!(targetDirectory is FtpDirectoryInfo)) throw new ArgumentException("The target directory must be of type FtpDirectoryInfo.", "targetDirectory"); Uri targetFilePath = new Uri(this.CombinePath(targetDirectory.FullName, sourceFile.Name)); try { using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile)) { using (Stream targetStream = this.client.OpenWrite(targetFilePath)) { if (sourceFile.Length > 0) { var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 8 * 1024, true); copyOperation.CopyProgressChanged += (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e); copyOperation.Execute(); } } } } catch (WebException ex) { switch (ex.Status) { case WebExceptionStatus.ConnectFailure: case WebExceptionStatus.ProxyNameResolutionFailure: throw new FileSystemUnavailableException("The FTP server is currently unavailable.", ex); } throw new AccessException("The file could not be accessed", ex); } }
public Stream GetContentStream() { return(fileSystem.OpenFileStream(path, FileMode.Open)); }