private Stream GetFileContent(VirtualFile file, bool write = false)
        {
            if (throwOnAccessFiles.Contains(file.FullName))
            {
                throw new FormattableException("You shall not pass.");
            }
            MemoryStream fileContent = fileContents[file];

            fileContent.Seek(0, SeekOrigin.Begin);
            ClosedStream closedStream = new ClosedStream();

            fileContent.CopyTo(closedStream);
            closedStream.SetLength(fileContent.Length);
            closedStream.Seek(0, SeekOrigin.Begin);
            closedStream.Closing += OnClosing;
            if (write)
            {
                changedFiles.Add(file);
            }
            return(closedStream);

            void OnClosing(object sender, EventArgs args)
            {
                closedStream.Closing -= OnClosing;
                if (write)
                {
                    closedStream.Seek(0, SeekOrigin.Begin);
                    fileContent.Seek(0, SeekOrigin.Begin);
                    closedStream.CopyTo(fileContent);
                    fileContent.SetLength(closedStream.Length);
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task DecompressParallel_ArgumentValidation()
        {
            await Assert.ThrowsAsync <ArgumentNullException>(() => StreamExtensions.DecompressParallelToAsync(null, Stream.Null, new Progress <double>(), CancellationToken.None));

            await Assert.ThrowsAsync <ArgumentNullException>(() => Stream.Null.DecompressParallelToAsync(null, new Progress <double>(), CancellationToken.None));

            await Assert.ThrowsAsync <ArgumentNullException>(() => Stream.Null.DecompressParallelToAsync(Stream.Null, null, CancellationToken.None));

            await Assert.ThrowsAsync <NotSupportedException>(() => ClosedStream.DecompressParallelToAsync(Stream.Null, new Progress <double>(), CancellationToken.None));

            await Assert.ThrowsAsync <NotSupportedException>(() => Stream.Null.DecompressParallelToAsync(ClosedStream, new Progress <double>(), CancellationToken.None));
        }