示例#1
0
        public void PublicMethodThrowAfterDispose()
        {
            var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite);

            stream.Dispose();

            byte[] buffer = new byte[1];
            Assert.That(
                () => stream.SetLength(10),
                Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(
                () => stream.WriteByte(0x00),
                Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(
                () => stream.Write(buffer, 0, 1),
                Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(
                () => stream.ReadByte(),
                Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(
                () => stream.Read(buffer, 0, 1),
                Throws.InstanceOf <ObjectDisposedException>());
        }
示例#2
0
        public void WriteOpensFile()
        {
            using (var stream = new LazyFileStream(tempFile, FileOpenMode.Write)) {
                Assert.That(stream.BaseStream, Is.Null);
                stream.WriteByte(0x42);
                Assert.That(stream.BaseStream, Is.Not.Null);
            }

            using (var fs = new FileStream(tempFile, FileMode.Open)) {
                Assert.That(fs.ReadByte(), Is.EqualTo(0x42));
            }

            using (var stream = new LazyFileStream(tempFile, FileOpenMode.Append)) {
                stream.Position = 1;
                Assert.That(stream.BaseStream, Is.Null);
                stream.Write(new byte[] { 0xAA, 0xBB }, 1, 1);
                Assert.That(stream.BaseStream, Is.Not.Null);
            }

            using (var fs = new FileStream(tempFile, FileMode.Open)) {
                Assert.That(fs.ReadByte(), Is.EqualTo(0x42));
                Assert.That(fs.ReadByte(), Is.EqualTo(0xBB));
            }
        }