public void UpdateLengthIfInputStreamGrowsAfterStartReading() { using (var stream = new MemoryStream()) { long initialLength = 100; long length = initialLength; byte[] buffer = new byte[initialLength]; stream.Write(buffer, 0, buffer.Length); using (var progress = new ProgressStream(stream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var t = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => t.Position)) { Assert.That(t.Position, Is.LessThanOrEqualTo(length)); Assert.That(t.Length, Is.LessThanOrEqualTo(length)); } }; progress.Read(buffer, 0, buffer.Length / 2); stream.Write(buffer, 0, buffer.Length); length = length + buffer.Length; progress.Read(buffer, 0, buffer.Length / 2); progress.Read(buffer, 0, buffer.Length / 2); progress.Read(buffer, 0, buffer.Length / 2); stream.Write(buffer, 0, buffer.Length); length = length + buffer.Length; progress.Read(buffer, 0, buffer.Length); } } }
public void TestRead() { using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { var buffer = new byte[1024]; int expected = 517; stream.Source.Write(buffer, 0, expected); stream.Source.Position = 0; progress = 0; int n = stream.Read(buffer, 0, buffer.Length); Assert.AreEqual(expected, n, "nread"); Assert.AreEqual(expected, progress, "progress"); } using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { var buffer = new byte[1024]; int expected = 517; stream.Source.Write(buffer, 0, expected); stream.Source.Position = 0; progress = 0; int n = stream.Read(buffer, 0, buffer.Length, CancellationToken.None); Assert.AreEqual(expected, n, "nread"); Assert.AreEqual(expected, progress, "progress"); } }
public void reports_progress_via_progress_t() { var callback = new ProgressReporter(); Mock <Stream> mockStream; var buffer = CreateStreamMock(out mockStream); using (var progressStream = new ProgressStream(mockStream.Object, callback)) { callback.CallbackCount.Should().Be(0); callback.LastPercentage.Should().Be(0); while (true) { var bytesRead = progressStream.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { break; } } } mockStream.Verify(); callback.CallbackCount.Should().BeGreaterThan(0); callback.LastPercentage.Should().Be(100f); }
public void reports_progress_via_action() { var callbackCount = 0; var lastPercentage = 0f; Action <ProgressStream.Progress> callback = progress => { callbackCount++; lastPercentage = progress.Percentage; Debug.WriteLine(lastPercentage); }; Mock <Stream> mockStream; var buffer = CreateStreamMock(out mockStream); using (var progressStream = new ProgressStream(mockStream.Object, callback)) { callbackCount.Should().Be(0); lastPercentage.Should().Be(0); while (true) { var bytesRead = progressStream.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { break; } } } mockStream.Verify(); callbackCount.Should().BeGreaterThan(0); lastPercentage.Should().Be(100f); }
internal static void WriteToRequest(HttpWebRequest request, ProgressStream progressStream) { log.Info("Writing data to request synchronously."); if (progressStream == null) { return; } try { log.Info("Reporting progress requested."); var buffer = new byte[CHUNK_SIZE]; using (var req = request.GetRequestStream()) { progressStream.Position = 0; int read = 0; for (int i = 0; i < progressStream.Length; i += read) { read = progressStream.Read(buffer, 0, CHUNK_SIZE); req.Write(buffer, 0, read); req.Flush(); // flushing is required or else we jump to 100% very fast } progressStream.ReportMaxValue(); } } catch (WebException ex) { HandleWebException(ex); } }
public void Inner_Stream_Calls_Read() { string key = "test"; var mockStream = new Mock <Stream>(); var progressStream = new ProgressStream(key, mockStream.Object, null, null); byte[] buffer = new byte[0]; int offset = 0; int count = 0; progressStream.Read(buffer, offset, count); mockStream.Verify(s => s.Read(buffer, offset, count), Times.Once); }
public static void TestPropertiesAndMethods() { using (MemoryStream memoryStream = FakeRuntimeFileInfo.ExpandableMemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })) { string kilroy = String.Empty; using (FakeStream testStream = new FakeStream(memoryStream, (string wasHere) => { kilroy += wasHere; })) { using (ProgressStream progressStream = new ProgressStream(testStream, new ProgressContext())) { kilroy = String.Empty; Assert.That(progressStream.CanRead, Is.True, "The underlying stream is readable."); Assert.That(kilroy, Is.EqualTo("CanRead"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.CanWrite, Is.True, "The underlying stream is writable."); Assert.That(kilroy, Is.EqualTo("CanWrite"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.CanSeek, Is.True, "The underlying stream is seekable."); Assert.That(kilroy, Is.EqualTo("CanSeek"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Flush(); Assert.That(kilroy, Is.EqualTo("Flush"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Length, Is.EqualTo(10), "There are 10 bytes in the underlying stream."); Assert.That(kilroy, Is.EqualTo("Length"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Seek(-4, SeekOrigin.End), Is.EqualTo(6), "4 bytes from the end of 10 should be 6."); Assert.That(kilroy, Is.EqualTo("Seek"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(progressStream.Position, Is.EqualTo(6), "The position should still be at 6."); Assert.That(kilroy, Is.EqualTo("getPosition"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Position = 0; Assert.That(kilroy, Is.EqualTo("setPosition"), "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Write(new byte[] { 13 }, 0, 1); Assert.That(kilroy.Contains("Write"), Is.True, "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; progressStream.Position = 0; byte[] firstByte = new byte[1]; progressStream.Read(firstByte, 0, 1); Assert.That(kilroy.Contains("Read"), Is.True, "ProgressStream should delegate to the underlying stream."); kilroy = String.Empty; Assert.That(firstByte[0], Is.EqualTo(13), "13 was just written to the first position."); progressStream.SetLength(5); Assert.That(kilroy, Is.EqualTo("SetLength"), "ProgressStream should delegate to the underlying stream."); } } } }
public static void TestInvalidArguments() { Stream nullStream = null; ProgressContext nullProgress = null; ProgressStream progressStream; Assert.Throws <ArgumentNullException>(() => { progressStream = new ProgressStream(nullStream, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { progressStream = new ProgressStream(new MemoryStream(), nullProgress); }); progressStream = new ProgressStream(new MemoryStream(), new ProgressContext()); byte[] nullBuffer = null; Assert.Throws <ArgumentNullException>(() => { progressStream.Write(nullBuffer, 0, 0); }); Assert.Throws <ArgumentNullException>(() => { progressStream.Read(nullBuffer, 0, 0); }); }
private int DownloadNextChunk(IDocument remoteDocument, long offset, long remainingBytes, FileTransmissionEvent status, Stream outputstream, HashAlgorithm hashAlg) { lock (this.disposeLock) { if (this.disposed) { status.ReportProgress(new TransmissionProgressEventArgs() { Aborted = true }); throw new ObjectDisposedException(status.Path); } IContentStream contentStream = remoteDocument.GetContentStream(remoteDocument.ContentStreamId, offset, remainingBytes); status.ReportProgress(new TransmissionProgressEventArgs { Length = remoteDocument.ContentStreamLength, ActualPosition = offset, Resumed = offset > 0 }); using (Stream remoteStream = contentStream.Stream) using (ForwardReadingStream forwardstream = new ForwardReadingStream(remoteStream)) using (OffsetStream offsetstream = new OffsetStream(forwardstream, offset)) using (ProgressStream progress = new ProgressStream(offsetstream, status)) { byte[] buffer = new byte[8 * 1024]; int result = 0; int len; while ((len = progress.Read(buffer, 0, buffer.Length)) > 0) { outputstream.Write(buffer, 0, len); hashAlg.TransformBlock(buffer, 0, len, buffer, 0); result += len; outputstream.Flush(); } return(result); } } }
public void ReadTest() { using (Stream stream = new MemoryStream()) { FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.ActualPosition != null) { this.positionCalls++; this.position = (long)args.ActualPosition; this.percent = (double)args.Percent; } }; byte[] buffer = new byte[10]; using (ProgressStream progress = new ProgressStream(stream, transmissionEvent)) { progress.SetLength(buffer.Length * 10); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, this.position); Assert.AreEqual(10, this.percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 2, this.position); Assert.AreEqual(20, this.percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 3, this.position); Assert.AreEqual(30, this.percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 4, this.position); Assert.AreEqual(40, this.percent); progress.Read(buffer, 0, buffer.Length / 2); Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position); Assert.AreEqual(45, this.percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 5) + (buffer.Length / 2), this.position); Assert.AreEqual(55, this.percent); progress.SetLength(buffer.Length * 100); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position); Assert.AreEqual(6.5, this.percent); } } }
public void Read_ChunkedPercentage(long total, long increment, int chunkSize, int[] expectedPercentages) { List <int> actualPercentages = new List <int>(); using (MemoryStream input = _streams.FillMemoryStream(total)) using (ProgressStream progressStream = input.WithReadProgress()) { progressStream.Progress.Percentage(chunkSize).Subscribe(progress => { actualPercentages.Add(progress.PercentComplete); }); byte[] buffer = new byte[increment]; int bytesRead = 0; do { bytesRead = progressStream.Read(buffer, offset: 0, count: buffer.Length); } while(bytesRead > 0); } Assert.Equal(expectedPercentages.Length, actualPercentages.Count); Assert.Equal(expectedPercentages, actualPercentages); }
private long SendBinary(ProgressStream input, String filename, Stream output, String boundary) { Write(GetFileSegmentStart(filename), output); output.Flush(); byte[] tmp = new byte[BUFFER_SIZE]; int l; // Wrap the stream to get some progress updates.. long uploadedBytes = 0; while ((l = input.Read(tmp, 0, BUFFER_SIZE)) != 0) { output.Write(tmp, 0, l); output.Flush(); uploadedBytes += l; } Write(CRLF, output); input.Flush(); return(uploadedBytes); }
public void Test_ProgressStream() { int count = 0; try { using (MemoryStream memoryStream = new MemoryStream(new byte[4096 * 10])) using (ProgressStream progressStream = new ProgressStream( memoryStream, (object sender, long readSize, long writeSize, out bool isStop) => { count++; if (readSize >= 8192) { isStop = true; } else { isStop = false; } }, 4096)) { byte[] tbyte = new byte[4096]; for (int i = 0; i < 4; i++) { progressStream.Read(tbyte, 0, tbyte.Length); } } } catch (StopIoException) { Assert.AreEqual(count, 2, "ProgressStream"); } }
public void Read() { using (var stream = new MemoryStream()) { byte[] buffer = new byte[10]; using (var progress = new ProgressStream(stream)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.positionCalls++; this.position = (long)p.Position; } }; progress.SetLength(buffer.Length * 10); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, this.position); Assert.AreEqual(10, progress.Percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 2, this.position); Assert.AreEqual(20, progress.Percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 3, this.position); Assert.AreEqual(30, progress.Percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 4, this.position); Assert.AreEqual(40, progress.Percent); progress.Read(buffer, 0, buffer.Length / 2); Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position); Assert.AreEqual(45, progress.Percent); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 5) + (buffer.Length / 2), this.position); Assert.AreEqual(55, progress.Percent); progress.SetLength(buffer.Length * 100); progress.Read(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position); Assert.AreEqual(6.5, progress.Percent); } } }