/// <summary> /// Downloads a blob to a local file asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult DownloadBlobAsync(string localFile) { lock (workingLock) { if (!working) { working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Download; fileName = localFile; this.blob.FetchAttributes(); FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(this.blob.Properties.Length); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return(asyncresult); }
public void DownloadBlobAsync(ICloudBlob blob, string LocalFile) { // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems. // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client. // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called. lock (WorkingLock) { if (!Working) { Working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Download; m_Blob = blob; m_FileName = LocalFile; m_Blob.FetchAttributes(); FileStream fs = new FileStream(m_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(m_Blob.Properties.Length); m_Blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 10; asyncresult = m_Blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream)); }
public void Position() { var mockedStream = new Mock <Stream>(); mockedStream.Setup(s => s.SetLength(It.IsAny <long>())); mockedStream.SetupProperty(s => s.Position); using (var underTest = new ProgressStream(mockedStream.Object)) { underTest.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var p = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => p.Length)) { this.lengthCalls++; } if (e.PropertyName == Utils.NameOf(() => p.Position)) { this.positionCalls++; } }; underTest.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.AreEqual(0, this.positionCalls); underTest.Position = 50; underTest.Position = 50; Assert.AreEqual(1, this.positionCalls); underTest.Position = 55; Assert.AreEqual(2, this.positionCalls); Assert.AreEqual(1, this.lengthCalls); } }
public void PositionTest() { var mockedStream = new Mock <Stream>(); FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.Length != null && args.Length != this.length) { this.lengthCalls++; this.length = (long)args.Length; } if (args.ActualPosition != null) { this.positionCalls++; } }; mockedStream.Setup(s => s.SetLength(It.IsAny <long>())); mockedStream.SetupProperty(s => s.Position); using (ProgressStream progress = new ProgressStream(mockedStream.Object, transmissionEvent)) { progress.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.AreEqual(0, this.positionCalls); progress.Position = 50; progress.Position = 50; Assert.AreEqual(1, this.positionCalls); progress.Position = 55; Assert.AreEqual(2, this.positionCalls); Assert.AreEqual(1, this.lengthCalls); } }
/// <summary> /// Uploads a file to an Azure blob asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult UploadBlobAsync(string localFile) { lock (workingLock) { if (!working) { working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Attempt to open the file first so that we throw an exception before getting into the async work using (var fstemp = new FileStream(localFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(this.blob); TransferType = TransferTypeEnum.Upload; fileName = localFile; var file = new FileInfo(fileName); long fileSize = file.Length; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return(asyncresult); }
public void SetLengthTest() { var mockedStream = new Mock <Stream>(); FileTransmissionEvent transmissionEvent = new FileTransmissionEvent(this.transmissionType, this.filename); transmissionEvent.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs args) { if (args.Length != null) { this.lengthCalls++; } }; mockedStream.Setup(s => s.SetLength(It.IsAny <long>())); using (ProgressStream progress = new ProgressStream(mockedStream.Object, transmissionEvent)) { progress.SetLength(100); progress.SetLength(100); Assert.AreEqual(1, this.lengthCalls); } }
public void SetLength() { var mockedStream = new Mock <Stream>(); mockedStream.Setup(s => s.SetLength(It.IsAny <long>())).Callback <long>((l) => mockedStream.Setup(mock => mock.Length).Returns(l)); using (ProgressStream progress = new ProgressStream(mockedStream.Object)) { progress.PropertyChanged += delegate(object sender, System.ComponentModel.PropertyChangedEventArgs args) { if (args.PropertyName == Utils.NameOf((ProgressStream s) => s.Length)) { this.lengthCalls++; } }; progress.SetLength(100); progress.SetLength(100); Assert.AreEqual(1, this.lengthCalls); Assert.That(progress.Length, Is.EqualTo(100)); } }
public void SeekTest() { 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; } }; using (ProgressStream progress = new ProgressStream(stream, transmissionEvent)) { progress.SetLength(100); progress.Seek(10, SeekOrigin.Begin); Assert.AreEqual(10, this.position); Assert.AreEqual(10, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(20, this.position); Assert.AreEqual(20, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(30, this.position); Assert.AreEqual(30, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(40, this.position); Assert.AreEqual(40, this.percent); progress.Seek(5, SeekOrigin.Current); Assert.AreEqual(45, this.position); Assert.AreEqual(45, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(55, this.position); Assert.AreEqual(55, this.percent); progress.SetLength(1000); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(65, this.position); Assert.AreEqual(6.5, this.percent); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(1000, this.position); } } }
public void SeekTest() { using (var stream = new MemoryStream()) { 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; this.percent = (double)p.Percent; } }; progress.SetLength(100); progress.Seek(10, SeekOrigin.Begin); Assert.AreEqual(10, this.position); Assert.AreEqual(10, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(20, this.position); Assert.AreEqual(20, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(30, this.position); Assert.AreEqual(30, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(40, this.position); Assert.AreEqual(40, this.percent); progress.Seek(5, SeekOrigin.Current); Assert.AreEqual(45, this.position); Assert.AreEqual(45, this.percent); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(55, this.position); Assert.AreEqual(55, this.percent); progress.SetLength(1000); progress.Seek(10, SeekOrigin.Current); Assert.AreEqual(65, this.position); Assert.AreEqual(6.5, this.percent); progress.Seek(0, SeekOrigin.End); Assert.AreEqual(100, this.percent); Assert.AreEqual(1000, this.position); } } }
public void Inner_Stream_Calls_SetLength() { string key = "test"; var mockStream = new Mock <Stream>(); var progressStream = new ProgressStream(key, mockStream.Object, null, null); long length = 0; progressStream.SetLength(length); mockStream.Verify(s => s.SetLength(length), Times.Once); }
public void WriteTest() { 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.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, this.position); Assert.AreEqual(10, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 2, this.position); Assert.AreEqual(20, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 3, this.position); Assert.AreEqual(30, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 4, this.position); Assert.AreEqual(40, this.percent); progress.Write(buffer, 0, buffer.Length / 2); Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position); Assert.AreEqual(45, this.percent); progress.Write(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.Write(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position); Assert.AreEqual(6.5, this.percent); } } }
public void Write() { 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 t = sender as ProgressStream; if (e.PropertyName == Utils.NameOf(() => t.Position)) { this.positionCalls++; this.position = (long)t.Position; this.percent = (double)t.Percent.GetValueOrDefault(); } }; progress.SetLength(buffer.Length * 10); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length, this.position); Assert.AreEqual(10, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 2, this.position); Assert.AreEqual(20, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 3, this.position); Assert.AreEqual(30, this.percent); progress.Write(buffer, 0, buffer.Length); Assert.AreEqual(buffer.Length * 4, this.position); Assert.AreEqual(40, this.percent); progress.Write(buffer, 0, buffer.Length / 2); Assert.AreEqual((buffer.Length * 4) + (buffer.Length / 2), this.position); Assert.AreEqual(45, this.percent); progress.Write(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.Write(buffer, 0, buffer.Length); Assert.AreEqual((buffer.Length * 6) + (buffer.Length / 2), this.position); Assert.AreEqual(6.5, this.percent); } } }
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 Stream?OpenFileWriteStream(string path, long fileSize, DateTime lastWriteTime) { _cancellationToken.ThrowIfCancellationRequested(); string relativePath = _normalizePath(path); if (relativePath == null) { return(null); } _pipe = new(); var readStream = new ProgressStream(_pipe.Reader.AsStream()); readStream.SetLength(fileSize); _task = Task.Run(() => _builder.AddFile(relativePath, readStream, DateTime.SpecifyKind(lastWriteTime, DateTimeKind.Utc)), _cancellationToken); return(_pipe.Writer.AsStream()); }
public void UploadBlobAsync(ICloudBlob blob, string LocalFile) { // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems. // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client. // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called. lock (WorkingLock) { if (!Working) { Working = true; } else { throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } } // Attempt to open the file first so that we throw an exception before getting into the async work using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Upload; m_Blob = blob; m_FileName = LocalFile; var file = new FileInfo(m_FileName); long fileSize = file.Length; FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); m_Blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 10; m_Blob.StreamWriteSizeInBytes = GetBlockSize(fileSize); asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream)); }
/// <summary> /// Uploads a file to an Azure blob asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult UploadBlobAsync(string localFile) { lock (workingLock) { if (!working) working = true; else throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } // Attempt to open the file first so that we throw an exception before getting into the async work using (var fstemp = new FileStream(localFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(this.blob); TransferType = TransferTypeEnum.Upload; fileName = localFile; var file = new FileInfo(fileName); long fileSize = file.Length; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return asyncresult; }
public void TestSetLength() { using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) { Assert.Throws <NotSupportedException> (() => stream.SetLength(500)); } }
public void UploadBlobAsync(ICloudBlob blob, string LocalFile) { // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems. // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client. // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called. lock (WorkingLock) { if (!Working) Working = true; else throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } // Attempt to open the file first so that we throw an exception before getting into the async work using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Upload; m_Blob = blob; m_FileName = LocalFile; var file = new FileInfo(m_FileName); long fileSize = file.Length; FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(fileSize); m_Blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream)); }
/// <summary> /// Downloads a blob to a local file asynchronously. /// </summary> /// <param name="localFile">The local file.</param> /// <exception cref="System.Exception">BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.</exception> public ICancellableAsyncResult DownloadBlobAsync(string localFile) { lock (workingLock) { if (!working) working = true; else throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer."); } // Create an async op in order to raise the events back to the client on the correct thread. asyncOp = AsyncOperationManager.CreateOperation(blob); TransferType = TransferTypeEnum.Download; fileName = localFile; this.blob.FetchAttributes(); FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); ProgressStream pstream = new ProgressStream(fs); pstream.ProgressChanged += pstream_ProgressChanged; pstream.SetLength(this.blob.Properties.Length); this.blob.ServiceClient.ParallelOperationThreadCount = 10; asyncresult = this.blob.BeginDownloadToStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(this.blob, pstream)); return asyncresult; }