public void TestSeekableStreamSerialization()
        {
            byte[] srcArray = Encoding.UTF8.GetBytes("test test test");
            var    src      = new DisposableStream(srcArray, false)
            {
                Position = 5
            };

            byte[]       destArray;
            MemoryStream dest;

            byte[] serialized;
            using (var ms = new MemoryStream())
                using (var w = new BinaryWriter(ms)) {
                    w.Write(src);
                    serialized = ms.ToArray();
                }

            Assert.IsTrue(src.Disposed);

            using (var ms = new MemoryStream(serialized, false))
                using (var r = new BinaryReader(ms))
                    using (dest = r.ReadStream()) {
                        destArray = dest.ToArray();
                    }

            Assert.AreNotSame(src, dest);
            Assert.AreEqual("test test test", Encoding.UTF8.GetString(destArray));
        }
Exemple #2
0
        public static BitmapImage LoadThumbnailBitmapImage(string filePath, int decodePixelWidth)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            BitmapImage bitmapImage = new BitmapImage();
            Stream      streamBase;

            if (filePath.StartsWith(@"pack://application:,,,", StringComparison.Ordinal))
            {
                var streamInfo = Application.GetResourceStream(new Uri(filePath));
                streamBase = streamInfo.Stream;
            }
            else
            {
                streamBase = File.OpenRead(filePath);
            }

            using (var stream = new DisposableStream(streamBase))
            {
                bitmapImage.BeginInit();
                bitmapImage.CacheOption      = BitmapCacheOption.OnLoad;
                bitmapImage.CreateOptions    = BitmapCreateOptions.None;
                bitmapImage.StreamSource     = stream;
                bitmapImage.DecodePixelWidth = decodePixelWidth;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
            }

            return(bitmapImage);
        }
Exemple #3
0
 public void Dispose_ShouldNotDisposeStream()
 {
     using (var stream = new DisposableStream())
     {
         using (new StreamRawPinPad(stream)) { }
         stream.WasDisposed.ShouldBeFalse();
     }
 }
        private Stream OpenOutputStream(ArchiveEntry entry)
        {
            MemoryStream ms = new MemoryStream((int)(entry.UncompressedSize * 1.3));

            DisposableStream writer = new DisposableStream(ms);

            writer.BeforeDispose.Add(new DisposableAction(() => _listing.Accessor.OnWritingCompleted(entry, ms, _compression)));

            return(writer);
        }
        public void TestEmailDispose()
        {
            var ts = new DisposableStream(Encoding.UTF8.GetBytes("test test test"));

            using (var msg = new MailMessage()) {
                msg.Attachments.Add(new Attachment(ts, new ContentType("text/plain")));
                Assert.IsFalse(ts.Disposed);
            }
            Assert.IsTrue(ts.Disposed);
        }
        public void DisposesContent()
        {
            // ARRANGE
            var stream = new DisposableStream();
            var response = new Response { Content = stream };

            // ACT
            response.Dispose();

            // ASSERT
            stream.Disposed.Should().BeTrue();
        }
        public void TestAttachmentSerialization()
        {
            var stream = new DisposableStream(Encoding.UTF8.GetBytes("test test test"), false);

            var attachment = new Attachment(stream, "file.txt", "text/plain");

            attachment.ContentDisposition.FileName = "file.txt";
            attachment.Name = null;

            var msg = new MailMessage();

            msg.Attachments.Add(attachment);

            Debug.WriteLine(TestHelper.GetStreamString(attachment.ContentStream, Encoding.UTF8));
            Debug.WriteLine(attachment.ContentType.ToString());
            Debug.WriteLine(attachment.ContentDisposition.ToString());

            Assert.IsFalse(stream.Disposed);

            byte[] serialized = MailMessageBinarySerializer.Serialize(msg);

            Assert.IsTrue(stream.Disposed);

            using (var msg2 = MailMessageBinarySerializer.Deserialize(serialized)) {
                Assert.IsNotNull(msg2);
                Assert.AreEqual(1, msg2.Attachments.Count);

                var attachment2 = msg2.Attachments[0];
                Assert.IsNotNull(attachment2);
                Assert.AreNotSame(attachment, attachment2);

                string data = Encoding.UTF8.GetString(((MemoryStream)attachment2.ContentStream).ToArray());
                Assert.AreEqual("test test test", data);

                Assert.AreNotSame(attachment.ContentType, attachment2.ContentType);
                Assert.AreEqual(attachment.ContentType.MediaType, attachment2.ContentType.MediaType);
                Assert.IsTrue(TestHelper.DictionariesAreEqual(
                                  attachment.ContentType.Parameters, attachment2.ContentType.Parameters));

                Assert.AreNotSame(attachment.ContentDisposition, attachment2.ContentDisposition);
                Assert.AreEqual(attachment.ContentDisposition, attachment2.ContentDisposition);

                Debug.WriteLine(TestHelper.GetStreamString(attachment2.ContentStream, Encoding.UTF8));
                Debug.WriteLine(attachment2.ContentType.ToString());
                Debug.WriteLine(attachment2.ContentDisposition.ToString());
            }
        }