/// ------------------------------------------------------------------------------------ /// <summary> /// Opens a stream to write the given file in the given mode. /// </summary> /// <param name="filename">The fully-qualified file name</param> /// <param name="mode">The <see>FileMode</see> to use</param> /// <returns>A stream with write access</returns> /// ------------------------------------------------------------------------------------ Stream IFileOS.OpenStreamForWrite(string filename, FileMode mode) { if (mode != FileMode.Open && mode != FileMode.OpenOrCreate) { throw new NotImplementedException("Haven't implemented the mocked version of this method for file mode " + mode); } if (!((IFileOS)this).FileExists(filename)) { if (mode == FileMode.OpenOrCreate) { AddFile(filename, string.Empty, Encoding.ASCII); } else { throw new FileNotFoundException("Could not find file " + filename); } } MockFile finfo = GetFileInfo(filename, FileLockType.Write); MemoryStream stream = new MemoryStream(10); StreamWriter sw = new StreamWriter(stream, finfo.Encoding); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); finfo.AddStream(stream); return(stream); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Opens a memory stream to read m_FileContents using the encoding (m_FileEncoding) /// that was set at the time the file was added. /// </summary> /// <param name="filename">Not used</param> /// <returns>A stream with read access</returns> /// ------------------------------------------------------------------------------------ Stream IFileOS.OpenStreamForRead(string filename) { MockFile finfo = GetFileInfo(filename, FileLockType.Read); //string fileContents = finfo.Contents; //byte[] contents = Encoding.Unicode.GetBytes(fileContents); MemoryStream stream = new MemoryStream(finfo.ContentsAsBytes, true); stream.Seek(0, SeekOrigin.Begin); finfo.AddStream(stream); return(stream); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Gets the writer. /// </summary> /// <param name="filename">The filename.</param> /// <param name="encoding">The encoding.</param> /// <returns></returns> /// ------------------------------------------------------------------------------------ TextWriter IFileOS.GetWriter(string filename, Encoding encoding) { if (!((IFileOS)this).FileExists(filename)) { AddFile(filename, string.Empty, encoding); } MockFile finfo = GetFileInfo(filename, FileLockType.Write); Assert.AreEqual(finfo.Encoding, encoding); StringWriter writer = new StringWriter(new StringBuilder()); finfo.AddStream(writer); return(writer); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Opens a TextReader on the given file /// </summary> /// <param name="filename">The fully-qualified filename</param> /// <param name="encoding">The encoding to use for interpreting the contents</param> /// ------------------------------------------------------------------------------------ TextReader IFileOS.GetReader(string filename, Encoding encoding) { MockFile finfo = GetFileInfo(filename, FileLockType.Read); // Make sure the encoding of the file is the same as the requested type. // However, it is possible that we want an ASCII encoded file opened in the magic // code page to turn it into unicode. Assert.IsTrue(finfo.Encoding == encoding || ((finfo.Encoding == Encoding.ASCII || finfo.Encoding == Encoding.UTF8) && encoding == Encoding.GetEncoding(FileUtils.kMagicCodePage))); StringReader reader = new StringReader(finfo.Contents.TrimStart('\ufeff')); finfo.AddStream(reader); return(reader); }