Example #1
0
        public void Gzip_Flush()
        {
            // Verify behavior of flushing a compression stream
            using (MemoryStream compressed = new MemoryStream())
            {
                using (GzipWriter stream = new GzipWriter(compressed, true))
                {
                    stream.Write(s_sampledata, 0, s_sampledata.Length);

                    // Get the unflushed length of the compressed stream and flush it
                    long unflushed = compressed.Length;
                    stream.Flush();

                    // The expectation is that the output stream will be longer after the flush
                    long flushedonce = compressed.Length;
                    Assert.IsTrue(compressed.Length > unflushed);

                    // Flushing the same data a second time should not have any impact at all
                    stream.Flush();
                    Assert.AreEqual(compressed.Length, flushedonce);

                    // The stream should still be writable after a flush operation
                    stream.Write(s_sampledata, 0, s_sampledata.Length / 10);
                }
            }

            // Verify behavior of flushing a decompression stream
            using (GzipReader stream = new GzipReader(new MemoryStream(s_sampledata)))
            {
                // Flush has no effect on decompression streams, just ensure it doesn't throw
                stream.Flush();
            }
        }
Example #2
0
        public void Gzip_ReaderDispose()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Create a dummy stream and immediately dispose of it
            GzipReader stream = new GzipReader(new MemoryStream(s_sampledata));

            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)); }
        }