Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Azure/InedoExtension/FileSystems/RandomAccessBlobStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Storage.Blob; | ||
|
||
namespace Inedo.ProGet.Extensions.Azure.PackageStores | ||
{ | ||
internal sealed class RandomAccessBlobStream : Stream | ||
{ | ||
private readonly ICloudBlob blob; | ||
|
||
public RandomAccessBlobStream(ICloudBlob blob) | ||
{ | ||
this.blob = blob; | ||
this.Length = blob.Properties.Length; | ||
} | ||
|
||
public override bool CanRead => true; | ||
public override bool CanSeek => true; | ||
public override bool CanWrite => false; | ||
public override long Length { get; } | ||
public override long Position { get; set; } | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
if (buffer == null) | ||
throw new ArgumentNullException(nameof(buffer)); | ||
if (offset < 0) | ||
throw new ArgumentOutOfRangeException(nameof(offset)); | ||
if (count < 0) | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
|
||
int bytesToRead = (int)Math.Min(count, this.Length - this.Position); | ||
if (bytesToRead <= 0) | ||
return 0; | ||
|
||
return this.blob.DownloadRangeToByteArray(buffer, offset, this.Position, bytesToRead); | ||
} | ||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
if (buffer == null) | ||
throw new ArgumentNullException(nameof(buffer)); | ||
if (offset < 0) | ||
throw new ArgumentOutOfRangeException(nameof(offset)); | ||
if (count < 0) | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
|
||
int bytesToRead = (int)Math.Min(count, this.Length - this.Position); | ||
if (bytesToRead <= 0) | ||
return 0; | ||
|
||
return await this.blob.DownloadRangeToByteArrayAsync(buffer, offset, this.Position, bytesToRead, cancellationToken).ConfigureAwait(false); | ||
} | ||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
return this.Position = origin switch | ||
{ | ||
SeekOrigin.Begin => offset, | ||
SeekOrigin.Current => this.Position + offset, | ||
SeekOrigin.End => this.Length + offset, | ||
_ => throw new ArgumentOutOfRangeException(nameof(origin)) | ||
}; | ||
} | ||
public override void Flush() | ||
{ | ||
} | ||
public override void SetLength(long value) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters