An extended memory stream that tracks closing and disposing
Inheritance: System.IO.MemoryStream
Example #1
0
        public void InputStreamOwnership()
        {
            var memStream = new TrackedMemoryStream();
            var s = new LzwInputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.Close();

            Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
            Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");

            memStream = new TrackedMemoryStream();
            s = new LzwInputStream(memStream);

            Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
            Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");

            s.IsStreamOwner = false;
            s.Close();

            Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
            Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
        }
Example #2
0
        public void DoubleClose()
        {
            var memStream = new TrackedMemoryStream();
            var s = new GZipOutputStream(memStream);
            s.Finish();
            s.Close();
            s.Close();

            memStream = new TrackedMemoryStream();
            using (GZipOutputStream no2 = new GZipOutputStream(memStream)) {
                s.Close();
            }
        }
        public void WriteAfterClose()
        {
            TrackedMemoryStream memStream=new TrackedMemoryStream();
            GZipOutputStream s=new GZipOutputStream(memStream);
            s.Close();

            try
            {
                s.WriteByte(7);
                Assert.Fail("Write should fail");
            }
            catch
            {
            }
        }
 public void DoubleFooter()
 {
     TrackedMemoryStream memStream=new TrackedMemoryStream();
     GZipOutputStream s=new GZipOutputStream(memStream);
     s.Finish();
     Int64 length=memStream.Length;
     s.Close();
     Assert.AreEqual(length, memStream.ToArray().Length);
 }
		public void DeflatorStreamOwnership()
		{
			TrackedMemoryStream memStream = new TrackedMemoryStream();
			DeflaterOutputStream s = new DeflaterOutputStream(memStream);
			
			Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
			Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
			
			s.Close();
			
			Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
			Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
			
			memStream = new TrackedMemoryStream();
			s = new DeflaterOutputStream(memStream);
			
			Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
			Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
			
			s.IsStreamOwner = false;
			s.Close();
			
			Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
			Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
			
		}
Example #6
0
        byte[] MakeLocalHeader(string asciiName, short versionToExtract, short flags, short method,
            int dostime, int crc, int compressedSize, int size)
        {
            using ( TrackedMemoryStream ms = new TrackedMemoryStream())
            {
                ms.WriteByte((byte)'P');
                ms.WriteByte((byte)'K');
                ms.WriteByte(3);
                ms.WriteByte(4);

                ms.WriteLEShort(versionToExtract);
                ms.WriteLEShort(flags);
                ms.WriteLEShort(method);
                ms.WriteLEInt(dostime);
                ms.WriteLEInt(crc);
                ms.WriteLEInt(compressedSize);
                ms.WriteLEInt(size);

                byte[] rawName = Encoding.ASCII.GetBytes(asciiName);
                ms.WriteLEShort((short)rawName.Length);
                ms.WriteLEShort(0);
                ms.Write(rawName, 0, rawName.Length);
                return ms.ToArray();
            }
        }
Example #7
0
        public void BaseNotClosedWhenNotOwner()
        {
            TrackedMemoryStream ms=new TrackedMemoryStream();

            Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed");

            using( ZipOutputStream stream=new ZipOutputStream(ms) ) {
                Assert.IsTrue(stream.IsStreamOwner, "Should be stream owner by default");
                stream.IsStreamOwner=false;
            }
            Assert.IsFalse(ms.IsClosed, "Underlying stream should still NOT be closed");
        }
Example #8
0
        public void BaseClosedAfterFailure()
        {
            TrackedMemoryStream ms=new TrackedMemoryStream(new byte[32]);

            Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially");
            bool blewUp = false;
            try {
                using( ZipOutputStream stream=new ZipOutputStream(ms) ) {
                    Assert.IsTrue(stream.IsStreamOwner, "Should be stream owner by default");
                    try {
                        stream.PutNextEntry(new ZipEntry("Tiny"));
                        stream.Write(new byte[32], 0, 32);
                    }
                    finally {
                        Assert.IsFalse(ms.IsClosed, "Stream should still not be closed.");
                        stream.Close();
                        Assert.Fail("Exception not thrown");
                    }
                }
            }
            catch {
                blewUp = true;
            }

            Assert.IsTrue(blewUp, "Should have failed to write to stream");
            Assert.IsTrue(ms.IsClosed, "Underlying stream should be closed");
        }
Example #9
0
        public void WriteAfterFinish()
        {
            var memStream = new TrackedMemoryStream();
            var s = new GZipOutputStream(memStream);
            s.Finish();

            try {
                s.WriteByte(7);
                Assert.Fail("Write should fail");
            } catch {
            }
        }