Example #1
0
        public void Write()
        {
            string        path = GetFile();
            StdFileStream fs   = new StdFileStream(path, FileMode.Open);

            Assert.AreEqual(0, fs.Length);
            Assert.AreEqual(0, fs.Position);

            fs.Write(new byte[] { 100, 103, 103, 104, 105 }, 0, 5);
            Assert.AreEqual(5, fs.Length);
            Assert.AreEqual(5, fs.Position);

            fs.Position = 2;
            Assert.AreEqual(2, fs.Position);

            fs.Write(new byte[] { 100, 103, 103, 104, 105 }, 1, 2);
            Assert.AreEqual(5, fs.Length);
            Assert.AreEqual(4, fs.Position);

            fs.Write(new byte[] { 100, 103, 103, 104, 105 }, 1, 2);
            Assert.AreEqual(6, fs.Length);
            Assert.AreEqual(6, fs.Position);

            fs.Close();
        }
        public void Constructor()
        {
            string path = GetFile("The quick brown fox jumped over the lazy dog");

            StdFileStream fs = new StdFileStream(path, FileMode.Open);

            Assert.IsTrue(fs.CanRead);
            Assert.IsTrue(fs.CanSeek);
            Assert.IsFalse(fs.CanTimeout);
            Assert.IsTrue(fs.CanWrite);

            Assert.AreEqual(new FileInfo(path).Length, fs.Length);
            Assert.AreEqual(0, fs.Position);
            Assert.Throws<InvalidOperationException>(() => { var x = fs.ReadTimeout; });
            Assert.Throws<InvalidOperationException>(() => { var x = fs.WriteTimeout; });

            fs.Close();

            Assert.IsFalse(fs.CanRead);
            Assert.IsFalse(fs.CanSeek);
            Assert.IsFalse(fs.CanTimeout);
            Assert.IsFalse(fs.CanWrite);

            Assert.Throws<ObjectDisposedException>(() => { var x = fs.Length; });
            Assert.Throws<ObjectDisposedException>(() => { var x = fs.Position; });
            Assert.Throws<InvalidOperationException>(() => { var x = fs.ReadTimeout; });
            Assert.Throws<InvalidOperationException>(() => { var x = fs.WriteTimeout; });
        }
Example #3
0
        public void Shared_read_access()
        {
            string path = GetFile();

            File.WriteAllBytes(path, new byte[] { 0x01, 0x02, 0x03 });

            using (StdFileStream fs1 = new StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StdFileStream fs2 = new StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    byte[] buffer = new byte[4];

                    Assert.AreEqual(3, fs1.Read(buffer, 0, 4));
                    CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);

                    Assert.AreEqual(3, fs2.Read(buffer, 0, 4));
                    CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);
                }

            using (FileStream fs1 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    byte[] buffer = new byte[4];

                    Assert.AreEqual(3, fs1.Read(buffer, 0, 4));
                    CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);

                    Assert.AreEqual(3, fs2.Read(buffer, 0, 4));
                    CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);
                }
        }
Example #4
0
        public void Constructor()
        {
            string path = GetFile("The quick brown fox jumped over the lazy dog");

            StdFileStream fs = new StdFileStream(path, FileMode.Open);

            Assert.IsTrue(fs.CanRead);
            Assert.IsTrue(fs.CanSeek);
            Assert.IsFalse(fs.CanTimeout);
            Assert.IsTrue(fs.CanWrite);

            Assert.AreEqual(new FileInfo(path).Length, fs.Length);
            Assert.AreEqual(0, fs.Position);
            Assert.Throws <InvalidOperationException>(() => { var x = fs.ReadTimeout; });
            Assert.Throws <InvalidOperationException>(() => { var x = fs.WriteTimeout; });

            fs.Close();

            Assert.IsFalse(fs.CanRead);
            Assert.IsFalse(fs.CanSeek);
            Assert.IsFalse(fs.CanTimeout);
            Assert.IsFalse(fs.CanWrite);

            Assert.Throws <ObjectDisposedException>(() => { var x = fs.Length; });
            Assert.Throws <ObjectDisposedException>(() => { var x = fs.Position; });
            Assert.Throws <InvalidOperationException>(() => { var x = fs.ReadTimeout; });
            Assert.Throws <InvalidOperationException>(() => { var x = fs.WriteTimeout; });
        }
Example #5
0
        public void Issue7_Length_of_long_file()
        {
            long size = (1024L * 1024L * 1024L * 2L) - 1;

            using (StdFileStream fs = BuildFile(size))
            {
                Assert.AreEqual(size, fs.Position);
                Assert.AreEqual(size, fs.Length);

                fs.WriteByte(0);

                // This was failing with a negative value
                Assert.AreEqual(size + 1, fs.Position);
                Assert.AreEqual(size + 1, fs.Length);
            }
        }
Example #6
0
        public void Shared_read_access_followed_by_shared_readwrite()
        {
            string path = GetFile();

            using (StdFileStream fs = new StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                Assert.Throws <IOException>(() => new StdFileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
                fs.Close();
            }

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                Assert.Throws <IOException>(() => new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
                fs.Close();
            }
        }
Example #7
0
        public void Exclusive_access()
        {
            string path = GetFile();

            using (StdFileStream fs = new StdFileStream(path, FileMode.Open))
            {
                Assert.Throws <IOException>(() => new StdFileStream(path, FileMode.Open));
                fs.Close();
            }

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                Assert.Throws <IOException>(() => new FileStream(path, FileMode.Open));
                fs.Close();
            }
        }
Example #8
0
        public void Read()
        {
            byte[]        buffer = new byte[1024];
            string        path   = GetFile();
            StdFileStream fs     = new StdFileStream(path, FileMode.Open);

            fs.Write(new byte[] { 100, 102, 103, 104, 105, 106, 107, 108 }, 0, 8);
            fs.Position = 0;

            Assert.AreEqual(2, fs.Read(buffer, 0, 2));
            Assert.AreEqual(new byte[] { 100, 102 }, buffer.Take(2));

            Assert.AreEqual(5, fs.Read(buffer, 2, 5));
            Assert.AreEqual(new byte[] { 100, 102, 103, 104, 105, 106, 107 }, buffer.Take(7));

            Assert.AreEqual(1, fs.Read(buffer, 1, 5));
            Assert.AreEqual(new byte[] { 100, 108, 103, 104, 105, 106, 107 }, buffer.Take(7));

            Assert.AreEqual(0, fs.Read(buffer, 0, 5));

            fs.Close();
        }
        public StdFileStream BuildFile(long length)
        {
            string path = GetFile();
            StdFileStream fs = new StdFileStream(path, FileMode.Open);

            try
            {
                byte[] buffer = new byte[4096];
                long remaining = length;

                while (remaining > 0)
                {
                    fs.Write(buffer, 0, (int)Math.Min(remaining, buffer.Length));
                    remaining -= buffer.Length;
                }

                return fs;
            }
            catch
            {
                fs.Close();
                throw;
            }
        }
Example #10
0
        public StdFileStream BuildFile(long length)
        {
            string        path = GetFile();
            StdFileStream fs   = new StdFileStream(path, FileMode.Open);

            try
            {
                byte[] buffer    = new byte[4096];
                long   remaining = length;

                while (remaining > 0)
                {
                    fs.Write(buffer, 0, (int)Math.Min(remaining, buffer.Length));
                    remaining -= buffer.Length;
                }

                return(fs);
            }
            catch
            {
                fs.Close();
                throw;
            }
        }
        public void Write()
        {
            string path = GetFile();
            StdFileStream fs = new StdFileStream(path, FileMode.Open);

            Assert.AreEqual(0, fs.Length);
            Assert.AreEqual(0, fs.Position);

            fs.Write(new byte[] { 100, 103, 103, 104, 105 }, 0, 5);
            Assert.AreEqual(5, fs.Length);
            Assert.AreEqual(5, fs.Position);

            fs.Position = 2;
            Assert.AreEqual(2, fs.Position);

            fs.Write(new byte[] { 100, 103, 103, 104, 105 }, 1, 2);
            Assert.AreEqual(5, fs.Length);
            Assert.AreEqual(4, fs.Position);

            fs.Write(new byte[] { 100, 103, 103, 104, 105 }, 1, 2);
            Assert.AreEqual(6, fs.Length);
            Assert.AreEqual(6, fs.Position);

            fs.Close();
        }
        public void Shared_read_with_readwrite_access_followed_by_shared_read()
        {
            string path = GetFile();

            using (StdFileStream fs = new StdFileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                Assert.Throws<IOException>(() => new StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read));
                fs.Close();
            }

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                Assert.Throws<IOException>(() => new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read));
                fs.Close();
            }
        }
        public void Shared_read_access()
        {
            string path = GetFile();

            File.WriteAllBytes(path, new byte[] { 0x01, 0x02, 0x03 });

            using (StdFileStream fs1 = new StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (StdFileStream fs2 = new StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] buffer = new byte[4];

                Assert.AreEqual(3, fs1.Read(buffer, 0, 4));
                CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);

                Assert.AreEqual(3, fs2.Read(buffer, 0, 4));
                CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);
            }

            using (FileStream fs1 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] buffer = new byte[4];

                Assert.AreEqual(3, fs1.Read(buffer, 0, 4));
                CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);

                Assert.AreEqual(3, fs2.Read(buffer, 0, 4));
                CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x00 }, buffer);
            }
        }
        public void Read()
        {
            byte[] buffer = new byte[1024];
            string path = GetFile();
            StdFileStream fs = new StdFileStream(path, FileMode.Open);

            fs.Write(new byte[] { 100, 102, 103, 104, 105, 106, 107, 108 }, 0, 8);
            fs.Position = 0;

            Assert.AreEqual(2, fs.Read(buffer, 0, 2));
            Assert.AreEqual(new byte[] { 100, 102 }, buffer.Take(2));

            Assert.AreEqual(5, fs.Read(buffer, 2, 5));
            Assert.AreEqual(new byte[] { 100, 102, 103, 104, 105, 106, 107 }, buffer.Take(7));

            Assert.AreEqual(1, fs.Read(buffer, 1, 5));
            Assert.AreEqual(new byte[] { 100, 108, 103, 104, 105, 106, 107 }, buffer.Take(7));

            Assert.AreEqual(0, fs.Read(buffer, 0, 5));

            fs.Close();
        }
        public void Exclusive_access()
        {
            string path = GetFile();

            using (StdFileStream fs = new StdFileStream(path, FileMode.Open))
            {
                Assert.Throws<IOException>(() => new StdFileStream(path, FileMode.Open));
                fs.Close();
            }

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                Assert.Throws<IOException>(() => new FileStream(path, FileMode.Open));
                fs.Close();
            }
        }