コード例 #1
0
        public void ReadOpensFile()
        {
            using (var fs = new FileStream(tempFile, FileMode.CreateNew)) {
                fs.WriteByte(0x42);
                fs.WriteByte(0xAA);
            }

            using (var stream = new LazyFileStream(tempFile, FileOpenMode.Read)) {
                Assert.That(stream.BaseStream, Is.Null);
                Assert.That(stream.ReadByte(), Is.EqualTo(0x42));
                Assert.That(stream.BaseStream, Is.Not.Null);
            }

            using (var stream = new LazyFileStream(tempFile, FileOpenMode.Read)) {
                Assert.That(stream.BaseStream, Is.Null);
                byte[] buffer = new byte[2];
                stream.Read(buffer, 0, buffer.Length);
                Assert.That(buffer[1], Is.EqualTo(0xAA));
                Assert.That(stream.BaseStream, Is.Not.Null);
            }
        }
コード例 #2
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>());
        }