public VirtualFileStream(IVirtualFileDataAccessor VirtualFileDataAccessor, string path, StreamType streamType) { if (VirtualFileDataAccessor == null) { throw new ArgumentNullException("VirtualFileDataAccessor"); } this.VirtualFileDataAccessor = VirtualFileDataAccessor; this.path = path; if (VirtualFileDataAccessor.FileExists(path)) { /* only way to make an expandable MemoryStream that starts with a particular content */ var data = VirtualFileDataAccessor.GetFile(path).Contents; if (data != null && data.Length > 0) { Write(data, 0, data.Length); Seek(0, StreamType.APPEND.Equals(streamType) ? SeekOrigin.End : SeekOrigin.Begin); } } else { if (StreamType.READ.Equals(streamType)) { throw new FileNotFoundException("File not found.", path); } VirtualFileDataAccessor.AddFile(path, new VirtualFileData(new byte[] { })); } }
public override string GetTempFileName() { string fileName = VirtualFileDataAccessor.Path.GetRandomFileName(); string tempDir = VirtualFileDataAccessor.Path.GetTempPath(); string fullPath = VirtualFileDataAccessor.Path.Combine(tempDir, fileName); VirtualFileDataAccessor.AddFile(fullPath, new VirtualFileData(string.Empty)); return(fullPath); }
public override void AppendAllText(string path, string contents, Encoding encoding) { if (encoding == null) { throw new ArgumentNullException("encoding"); } VirtualFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); if (!VirtualFileDataAccessor.FileExists(path)) { var dir = VirtualFileDataAccessor.Path.GetDirectoryName(path); if (!VirtualFileDataAccessor.Directory.Exists(dir)) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path)); } VirtualFileDataAccessor.AddFile(path, new VirtualFileData(contents, encoding)); } else { var file = VirtualFileDataAccessor.GetFile(path); var bytesToAppend = encoding.GetBytes(contents); file.Contents = file.Contents.Concat(bytesToAppend).ToArray(); } }