Abortable stream wraps the given stream and add the possibility to abort the stream read and write by throwing an exception.
Inheritance: NotifyPropertyChangedStream
コード例 #1
0
 public void AbortWriteIfAbortIsCalled() {
     using (var inputStream = new MemoryStream(new byte[1024 * 1024 * 10]))
     using (var stream = new Mock<MemoryStream>() { CallBase = true }.Object)
     using (var underTest = new AbortableStream(stream)) {
         underTest.Abort();
         Assert.Throws<AbortException>(() => inputStream.CopyTo(underTest));
         Mock.Get(stream).Verify(s => s.WriteByte(It.IsAny<byte>()), Times.Never());
         Mock.Get(stream).Verify(s => s.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never());
     }
 }
コード例 #2
0
 public void AbortReadIfAbortIsCalled() {
     byte[] content = new byte[1024];
     using (var stream = new Mock<MemoryStream>(content) { CallBase = true }.Object)
     using (var underTest = new AbortableStream(stream)) {
         underTest.Abort();
         Assert.Throws<AbortException>(() => underTest.ReadByte());
         Mock.Get(stream).Verify(s => s.ReadByte(), Times.Never());
         Mock.Get(stream).Verify(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never());
     }
 }
コード例 #3
0
 public void WritingStreamWithoutAbortionWorks() {
     var length = 1024 * 1024;
     var content = new byte[length];
     using (var inputStream = new MemoryStream(content))
     using (var outputStream = new MemoryStream())
     using (var underTest = new AbortableStream(outputStream)) {
         inputStream.CopyTo(underTest);
         Assert.That(outputStream.Length, Is.EqualTo(length));
     }
 }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.TransmissionStream"/> class.
        /// </summary>
        /// <param name="wrappedStream">Wrapped stream.</param>
        /// <param name="transmission">Transmission object to be notified about changes and listened to events as well.</param>
        public TransmissionStream(Stream wrappedStream, Transmission transmission) {
            if (transmission == null) {
                throw new ArgumentNullException("transmission");
            }

            this.abort = new AbortableStream(wrappedStream);
            this.pause = new PausableStream(this.abort);
            this.bandwidthNotify = new BandwidthNotifyingStream(this.pause);
            this.progress = new ProgressStream(this.bandwidthNotify);
            this.abort.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var a = sender as AbortableStream;
                if (e.PropertyName == Utils.NameOf(() => a.Exception)) {
                    transmission.Status = TransmissionStatus.ABORTED;
                    transmission.FailedException = a.Exception;
                }
            };
            this.bandwidthNotify.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var s = sender as BandwidthNotifyingStream;
                if (e.PropertyName == Utils.NameOf(() => s.BitsPerSecond)) {
                    transmission.BitsPerSecond = s.BitsPerSecond;
                }
            };
            this.progress.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var p = sender as ProgressStream;
                if (e.PropertyName == Utils.NameOf(() => p.Position)) {
                    transmission.Position = p.Position;
                } else if (e.PropertyName == Utils.NameOf(() => p.Length)) {
                    transmission.Length = p.Length;
                }
            };
            transmission.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Status)) {
                    if (t.Status == TransmissionStatus.ABORTING) {
                        this.abort.Abort();
                        this.pause.Resume();
                    } else if (t.Status == TransmissionStatus.PAUSED) {
                        this.pause.Pause();
                    } else if (t.Status == TransmissionStatus.TRANSMITTING) {
                        this.pause.Resume();
                    }
                }
            };
            if (transmission.Status == TransmissionStatus.ABORTING || transmission.Status == TransmissionStatus.ABORTED) {
                this.abort.Abort();
            }
        }
コード例 #5
0
        public void NotificationSendOutOnAbortion() {
            bool notified = false;
            byte[] content = new byte[1024];
            using (var stream = new Mock<MemoryStream>(content) { CallBase = true }.Object)
            using (var underTest = new AbortableStream(stream)) {
                underTest.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => {
                    Assert.That(e.PropertyName, Is.EqualTo(Utils.NameOf((AbortableStream s) => s.Exception)));
                    Assert.That(underTest.Exception, Is.Not.Null);
                    notified = true;
                };
                underTest.Abort();
                Assert.Throws<AbortException>(() => underTest.ReadByte());
            }

            Assert.That(notified, Is.True);
        }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.TransmissionStream"/> class.
        /// </summary>
        /// <param name="wrappedStream">Wrapped stream.</param>
        /// <param name="transmission">Transmission object to be notified about changes and listened to events as well.</param>
        public TransmissionStream(Stream wrappedStream, Transmission transmission)
        {
            if (transmission == null)
            {
                throw new ArgumentNullException("transmission");
            }

            this.abort                  = new AbortableStream(wrappedStream);
            this.pause                  = new PausableStream(this.abort);
            this.bandwidthNotify        = new BandwidthNotifyingStream(this.pause);
            this.progress               = new ProgressStream(this.bandwidthNotify);
            this.abort.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var a = sender as AbortableStream;
                if (e.PropertyName == Utils.NameOf(() => a.Exception))
                {
                    transmission.Status          = TransmissionStatus.ABORTED;
                    transmission.FailedException = a.Exception;
                }
            };
            this.bandwidthNotify.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var s = sender as BandwidthNotifyingStream;
                if (e.PropertyName == Utils.NameOf(() => s.BitsPerSecond))
                {
                    transmission.BitsPerSecond = s.BitsPerSecond;
                }
            };
            this.progress.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var p = sender as ProgressStream;
                if (e.PropertyName == Utils.NameOf(() => p.Position))
                {
                    transmission.Position = p.Position;
                }
                else if (e.PropertyName == Utils.NameOf(() => p.Length))
                {
                    transmission.Length = p.Length;
                }
            };
            transmission.PropertyChanged += (object sender, PropertyChangedEventArgs e) => {
                var t = sender as Transmission;
                if (e.PropertyName == Utils.NameOf(() => t.Status))
                {
                    if (t.Status == TransmissionStatus.ABORTING)
                    {
                        this.abort.Abort();
                        this.pause.Resume();
                    }
                    else if (t.Status == TransmissionStatus.PAUSED)
                    {
                        this.pause.Pause();
                    }
                    else if (t.Status == TransmissionStatus.TRANSMITTING)
                    {
                        this.pause.Resume();
                    }
                }
            };
            if (transmission.Status == TransmissionStatus.ABORTING || transmission.Status == TransmissionStatus.ABORTED)
            {
                this.abort.Abort();
            }
        }