Esempio n. 1
0
 public void LengthSyncWithStream()
 {
     using (var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite)) {
         stream.SetLength(1);
         Assert.That(stream.Length, Is.EqualTo(1));
         stream.BaseStream.Position = 1;
         stream.BaseStream.WriteByte(0xAA);
         Assert.That(stream.Length, Is.EqualTo(2));
     }
 }
Esempio n. 2
0
 public void SetLengthOpensFile()
 {
     using (var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite)) {
         stream.SetLength(1);
         Assert.That(stream.BaseStream, Is.Not.Null);
         Assert.That(File.Exists(tempFile), Is.True);
         Assert.That(stream.Length, Is.EqualTo(1));
         Assert.That(stream.BaseStream.Length, Is.EqualTo(1));
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Creates a new <see cref="DataStream"/> from a file.
        /// </summary>
        /// <param name="path">The path of the file.</param>
        /// <param name="mode">The mode to open the file.</param>
        /// <returns>A new <see cref="DataStream"/>.</returns>
        public static DataStream FromFile(string path, FileOpenMode mode)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            var baseStream = new LazyFileStream(path, mode);

            return(new DataStream(baseStream));
        }
Esempio n. 4
0
        public void ConstructorSetFileLengthOrZero()
        {
            using var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite);
            Assert.That(stream.Length, Is.EqualTo(0));
            Assert.That(stream.BaseStream, Is.Null);
            Assert.That(File.Exists(tempFile), Is.False);

            using (var fs = new FileStream(tempFile, FileMode.CreateNew)) {
                fs.WriteByte(0xAA);
            }

            using (var stream2 = new LazyFileStream(tempFile, FileOpenMode.ReadWrite)) {
                Assert.That(stream2.Length, Is.EqualTo(1));
                Assert.That(stream2.BaseStream, Is.Null);
            }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a new <see cref="DataStream"/> from a section of a file.
        /// </summary>
        /// <param name="path">The path of the file.</param>
        /// <param name="mode">The mode to open the file.</param>
        /// <param name="offset">Offset from the start of the file.</param>
        /// <param name="length">Length of the new stream.</param>
        /// <returns>A new <see cref="DataStream"/>.</returns>
        public static DataStream FromFile(string path, FileOpenMode mode, long offset, long length)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            long fileSize = new FileInfo(path).Length;

            if (offset < 0 || offset > fileSize)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (length < 0 || offset + length > fileSize)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            var baseStream = new LazyFileStream(path, mode);

            return(new DataStream(baseStream, offset, length, true));
        }
Esempio n. 7
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>());
        }
Esempio n. 8
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));
            }
        }
Esempio n. 9
0
 public void ConstructorDoesNotOpenStream()
 {
     using var stream = new LazyFileStream(tempFile, FileOpenMode.ReadWrite);
     Assert.That(stream.BaseStream, Is.Null);
 }