public async Task CreateCopyToDestroyBlob() { using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms)) { // write ~7MB of data for (int i = 0; i < 1000000; ++i) { writer.Write((long)i); } writer.Flush(); await ms.FlushAsync(); ms.Position = 0; IRBlobInfo blob = null; using (RBlobStream blobStream = await RBlobStream.CreateAsync(_session)) { await ms.CopyToAsync(blobStream); blob = blobStream.GetBlobInfo(); } using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _session)) using (MemoryStream ms2 = new MemoryStream()) { await blobStream.CopyToAsync(ms2, 1024 * 1024); await ms2.FlushAsync(); ms.ToArray().Should().Equal(ms2.ToArray()); } } }
public async Task CreateWriteWithSeekBlob() { IRBlobInfo blob = null; using (RBlobStream blobStream = await RBlobStream.CreateAsync(_session)) using (BinaryWriter writer = new BinaryWriter(blobStream)) { // write {1, 2, 3} writer.Write((long)1); writer.Write((long)2); writer.Write((long)3); // go back to position 2 blobStream.Seek(sizeof(long), SeekOrigin.Begin); // change data to {1, 4, 3} writer.Write((long)4); blob = blobStream.GetBlobInfo(); } using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _session)) using (BinaryReader reader = new BinaryReader(blobStream)) { long[] expectedData = { 1, 4, 3 }; for (int i = 0; i < expectedData.Length; ++i) { reader.ReadInt64().Should().Be(expectedData[i]); } } }
private RBlobStream(IRBlobInfo blob, bool canWrite, IRBlobService blobService) { _blob = blob; _canWrite = canWrite; _blobService = blobService; _isDisposed = false; }
public async Task CreateGetDestroyBlobs() { byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5 }; byte[] data2 = new byte[] { 10, 11, 12, 13, 14, 15 }; byte[] data3 = new byte[] { 20, 21, 22, 23, 24, 25 }; var dataSet = new byte[][] { data1, data2, data3 }; using (DataTransferSession dts = new DataTransferSession(_session, null)) { var blob1 = await dts.SendBytesAsync(data1, true, null, CancellationToken.None); var blob2 = await dts.SendBytesAsync(data2, true, null, CancellationToken.None); var blob3 = await dts.SendBytesAsync(data3, true, null, CancellationToken.None); blob1.Id.Should().BeGreaterThan(0); blob2.Id.Should().BeGreaterThan(0); blob3.Id.Should().BeGreaterThan(0); blob1.Id.Should().NotBe(blob2.Id); blob2.Id.Should().NotBe(blob3.Id); blob3.Id.Should().NotBe(blob1.Id); var blobIds = new IRBlobInfo[] { blob1, blob2, blob3 }; for (int i = 0; i < blobIds.Length; ++i) { var blob = await dts.FetchBytesAsync(blobIds[i], false, null, CancellationToken.None); blob.Should().Equal(dataSet[i]); } } }
/// <summary> /// Gets the data for a given blob from R-Host. Saves the data to <paramref name="filePath"/>. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task FetchFileAsync(IRBlobInfo blob, string filePath, bool doCleanUp, IProgress<long> progress, CancellationToken cancellationToken) { using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _blobService)) using (Stream fileStream = _fs.CreateFile(filePath)) { await blobStream.CopyToAsync(fileStream, progress, cancellationToken); } if (doCleanUp) { _cleanup.Add(blob); } }
/// <summary> /// Gets the data for a given blob from R-Host. Saves the data to <paramref name="filePath"/>. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task FetchFileAsync(IRBlobInfo blob, string filePath, bool doCleanUp = true) { var data = await _session.GetBlobAsync(blob.Id); _fs.FileWriteAllBytes(filePath, data); if (doCleanUp) { _cleanup.Add(blob); } }
/// <summary> /// Gets the data for a given blob from R-Host. Saves the data to <paramref name="filePath"/>. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task FetchFileAsync(IRBlobInfo blob, string filePath, bool doCleanUp, IProgress <long> progress, CancellationToken cancellationToken) { using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _blobService)) using (Stream fileStream = _fs.CreateFile(filePath)) { await blobStream.CopyToAsync(fileStream, progress, cancellationToken); } if (doCleanUp) { _cleanup.Add(blob); } }
/// <summary> /// Gets the data for a given blob from R-Host. Decompresses it and saves the data to <paramref name="filePath"/>. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task FetchAndDecompressFileAsync(IRBlobInfo blob, string filePath, bool doCleanUp, IProgress <long> progress, CancellationToken cancellationToken) { using (MemoryStream compressed = new MemoryStream(await FetchBytesAsync(blob, false, progress, cancellationToken))) using (ZipArchive archive = new ZipArchive(compressed, ZipArchiveMode.Read)) { var entry = archive.GetEntry("data"); using (Stream decompressedStream = entry.Open()) using (Stream fileStream = _fs.CreateFile(filePath)) { await decompressedStream.CopyToAsync(fileStream, progress, cancellationToken); } } if (doCleanUp) { _cleanup.Add(blob); } }
/// <summary> /// Gets the data for a given blob from R-Host. This method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task <byte[]> FetchBytesAsync(IRBlobInfo blob, bool doCleanUp, IProgress <long> progress, CancellationToken cancellationToken) { byte[] data = null; using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _blobService, cancellationToken)) using (MemoryStream ms = new MemoryStream((int)blobStream.Length)) { await blobStream.CopyToAsync(ms, progress, cancellationToken); data = ms.ToArray(); } if (doCleanUp) { _cleanup.Add(blob); } return(data); }
/// <summary> /// Sends a block of data to R-Host by creating a new blob. This method adds the blob for clean /// up by default. /// </summary> /// <param name="data">Block of data to be sent.</param> /// <param name="doCleanUp"> /// true to add blob created upon transfer for cleanup on dispose, false to ignore it after transfer. /// </param> public async Task <IRBlobInfo> SendBytesAsync(byte[] data, bool doCleanUp, IProgress <long> progress, CancellationToken cancellationToken) { IRBlobInfo blob = null; using (RBlobStream blobStream = await RBlobStream.CreateAsync(_blobService)) using (Stream ms = new MemoryStream(data)) { await ms.CopyToAsync(blobStream, progress, cancellationToken); blob = blobStream.GetBlobInfo(); } if (doCleanUp) { _cleanup.Add(blob); } return(blob); }
/// <summary> /// Sends file to R-Host by creating a new blob. This method adds the blob for clean up by default. /// </summary> /// <param name="filePath">Path to the file to be sent to R-Host.</param> /// <param name="doCleanUp"> /// true to add blob created upon transfer for cleanup on dispose, false to ignore it after transfer. /// </param> public async Task <IRBlobInfo> SendFileAsync(string filePath, bool doCleanUp, IProgress <long> progress, CancellationToken cancellationToken) { IRBlobInfo blob = null; using (RBlobStream blobStream = await RBlobStream.CreateAsync(_blobService)) using (Stream fileStream = _fs.FileOpen(filePath, FileMode.Open)){ await fileStream.CopyToAsync(blobStream, progress, cancellationToken); blob = blobStream.GetBlobInfo(); } if (doCleanUp) { _cleanup.Add(blob); } return(blob); }
/// <summary> /// Gets the data for a given blob (compressed) from R-Host and decompresses it. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task <byte[]> FetchAndDecompressBytesAsync(IRBlobInfo blob, bool doCleanUp, IProgress <long> progress, CancellationToken cancellationToken) { byte[] data = null; using (MemoryStream compressed = new MemoryStream(await FetchBytesAsync(blob, false, progress, cancellationToken))) using (ZipArchive archive = new ZipArchive(compressed, ZipArchiveMode.Read)) { var entry = archive.GetEntry("data"); using (Stream decompressedStream = entry.Open()) using (MemoryStream ms = new MemoryStream()) { await decompressedStream.CopyToAsync(ms, progress, cancellationToken); data = ms.ToArray(); } } if (doCleanUp) { _cleanup.Add(blob); } return(data); }
public async Task GetBlob_CanceledDuringGet() { var cts = new CancellationTokenSource(); var data = new byte[1024 * 1024]; IRBlobInfo blob = null; using (DataTransferSession dts = new DataTransferSession(_session, null)) { blob = await dts.SendBytesAsync(data, false, null, CancellationToken.None); } Func <Task> f = async() => { while (true) { await _session.BlobReadAllAsync(blob.Id, ct : cts.Token); } }; var assertion = f.ShouldThrowAsync <OperationCanceledException>(); cts.CancelAfter(1); await assertion; }
public async Task GetBlob_DisconnectedDuringGet() { var data = new byte[1024 * 1024]; IRBlobInfo blob = null; using (DataTransferSession dts = new DataTransferSession(_session, null)) { blob = await dts.SendBytesAsync(data, false, null, CancellationToken.None); } using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _session)) { Func <Task> f = async() => { using (MemoryStream ms = new MemoryStream()) { await blobStream.CopyToAsync(ms, 1024); } }; await Task.Delay(100); await _session.StopHostAsync(); await f.ShouldThrowAsync <RHostDisconnectedException>(); } }
public static void Destroy(IRBlobInfo blobInfo, IRBlobService blobService) => blobService.DestroyBlobsAsync(new ulong[] { blobInfo.Id }).GetAwaiter().GetResult();
public static Task DestroyAsync(IRBlobInfo blobInfo, IRBlobService blobService, CancellationToken ct = default(CancellationToken)) { return blobService.DestroyBlobsAsync(new ulong[] { blobInfo.Id }, ct); }
public static void Destroy(IRBlobInfo blobInfo, IRBlobService blobService) { blobService.DestroyBlobsAsync(new ulong[] { blobInfo.Id }).GetAwaiter().GetResult(); }
public static Task<RBlobStream> OpenAsync(IRBlobInfo blobInfo, IRBlobService blobService, CancellationToken ct = default(CancellationToken)) { return Task.FromResult(Open(blobInfo, blobService)); }
public static Task <RBlobStream> OpenAsync(IRBlobInfo blobInfo, IRBlobService blobService, CancellationToken ct = default(CancellationToken)) => Task.FromResult(Open(blobInfo, blobService));
/// <summary> /// Gets the data for a given blob (compressed) from R-Host and decompresses it. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task<byte[]> FetchAndDecompressBytesAsync(IRBlobInfo blob, bool doCleanUp, IProgress<long> progress, CancellationToken cancellationToken) { byte[] data = null; using (MemoryStream compressed = new MemoryStream(await FetchBytesAsync(blob, false, progress, cancellationToken))) using (ZipArchive archive = new ZipArchive(compressed, ZipArchiveMode.Read)) { var entry = archive.GetEntry("data"); using (Stream decompressedStream = entry.Open()) using (MemoryStream ms = new MemoryStream()) { await decompressedStream.CopyToAsync(ms, progress, cancellationToken); data = ms.ToArray(); } } if (doCleanUp) { _cleanup.Add(blob); } return data; }
/// <summary> /// Gets the data for a given blob from R-Host. Decompresses it and saves the data to <paramref name="filePath"/>. This /// method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="filePath">Path to the file where the retrieved data will be written.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task FetchAndDecompressFileAsync(IRBlobInfo blob, string filePath, bool doCleanUp, IProgress<long> progress, CancellationToken cancellationToken) { using (MemoryStream compressed = new MemoryStream(await FetchBytesAsync(blob, false, progress, cancellationToken))) using (ZipArchive archive = new ZipArchive(compressed, ZipArchiveMode.Read)) { var entry = archive.GetEntry("data"); using (Stream decompressedStream = entry.Open()) using (Stream fileStream = _fs.CreateFile(filePath)) { await decompressedStream.CopyToAsync(fileStream, progress, cancellationToken); } } if (doCleanUp) { _cleanup.Add(blob); } }
public static RBlobStream Open(IRBlobInfo blobInfo, IRBlobService blobService) { return(new RBlobStream(blobInfo, false, blobService)); }
public static Task DestroyAsync(IRBlobInfo blobInfo, IRBlobService blobService, CancellationToken ct = default(CancellationToken)) { return(blobService.DestroyBlobsAsync(new ulong[] { blobInfo.Id }, ct)); }
public static Task DestroyAsync(IRBlobInfo blobInfo, IRBlobService blobService, CancellationToken ct = default(CancellationToken)) => blobService.DestroyBlobsAsync(new [] { blobInfo.Id }, ct);
public static RBlobStream Open(IRBlobInfo blobInfo, IRBlobService blobService) { return new RBlobStream(blobInfo, false, blobService); }
/// <summary> /// Gets the data for a given blob from R-Host. This method adds the blob for clean up by default. /// </summary> /// <param name="blob">Blob from which the data is to be retrieved.</param> /// <param name="doCleanUp">true to add blob upon transfer for cleanup on dispose, false to ignore it after transfer.</param> public async Task<byte[]> FetchBytesAsync(IRBlobInfo blob, bool doCleanUp, IProgress<long> progress, CancellationToken cancellationToken) { byte[] data = null; using (RBlobStream blobStream = await RBlobStream.OpenAsync(blob, _blobService, cancellationToken)) using (MemoryStream ms = new MemoryStream((int)blobStream.Length)) { await blobStream.CopyToAsync(ms, progress, cancellationToken); data = ms.ToArray(); } if (doCleanUp) { _cleanup.Add(blob); } return data; }
public static RBlobStream Open(IRBlobInfo blobInfo, IRBlobService blobService) => new RBlobStream(blobInfo, false, blobService);