Esempio n. 1
0
        public override byte[] ReadAllBytes(string path)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            using (Stream s = file.GetContentStream())
            {
                int count = (int)s.Length;

                int    pos    = 0;
                byte[] result = new byte[count];
                while (count > 0)
                {
                    int n = s.Read(result, pos, count);
                    if (n == 0)
                    {
                        throw new IOException("Unexpected end of stream");
                    }

                    pos   += n;
                    count -= n;
                }

                return(result);
            }
        }
Esempio n. 2
0
        public override Stream OpenFileStream(string path, FileMode fileMode, FileAccess fileAccess, FileShare shareMode, FileOptions options, bool flushesToDisk)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            if (fileMode == FileMode.Create)
            {
                if (file != null)
                {
                    this.RootDirectory.RemoveFile(path);
                }

                return(this.CreateAndOpenFileStream(path));
            }

            if (fileMode == FileMode.OpenOrCreate)
            {
                if (file == null)
                {
                    return(this.CreateAndOpenFileStream(path));
                }
            }
            else
            {
                file.ShouldNotBeNull();
            }

            return(file.GetContentStream());
        }
Esempio n. 3
0
        public override string ReadAllText(string path)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            using (StreamReader reader = new StreamReader(file.GetContentStream()))
            {
                return(reader.ReadToEnd());
            }
        }
Esempio n. 4
0
        public override IEnumerable <string> ReadLines(string path)
        {
            MockFile file = this.RootDirectory.FindFile(path);

            using (StreamReader reader = new StreamReader(file.GetContentStream()))
            {
                while (!reader.EndOfStream)
                {
                    yield return(reader.ReadLine());
                }
            }
        }