Esempio n. 1
0
        public void Xz_Dispose()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Create a memorystream to hold the compressed sample data
            using (MemoryStream ms = new MemoryStream())
            {
                XzEncoder encoder = new XzEncoder();
                encoder.Encode(s_sampledata, ms);

                // Create a decompression stream and immediately dispose of it
                XzReader stream = new XzReader(ms);
                stream.Dispose();

                // Test double dispose
                stream.Dispose();

                // All properties and methods should throw an ObjectDisposedException
                try { var bs = stream.BaseStream; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { var b = stream.CanRead; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { var b = stream.CanSeek; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { var b = stream.CanWrite; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { stream.Flush(); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { var l = stream.Length; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { var l = stream.Position; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { stream.Position = 12345L; Assert.Fail("Property access should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { stream.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { stream.Seek(0, SeekOrigin.Current); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { stream.SetLength(12345L); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

                try { stream.Write(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }
            }
        }
Esempio n. 2
0
        public void Xz_Write()
        {
            XzEncoder encoder    = new XzEncoder();
            var       compressed = encoder.Encode(s_sampledata);

            using (XzReader reader = new XzReader(new MemoryStream(compressed)))
            {
                byte[] buffer = new byte[2048];
                try { reader.Write(buffer, 0, buffer.Length); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
            }
        }