Skip to content

Commit

Permalink
added random access read stream
Browse files Browse the repository at this point in the history
  • Loading branch information
gdivis committed Aug 17, 2021
1 parent 3c3fba0 commit 783f67a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Azure/InedoExtension/FileSystems/AzureFileSystem.cs
Expand Up @@ -66,7 +66,11 @@ public override async Task<Stream> OpenReadAsync(string fileName, FileAccessHint

var file = await this.Container.GetBlobReferenceFromServerAsync(path, cancellationToken).ConfigureAwait(false);
await file.FetchAttributesAsync(cancellationToken).ConfigureAwait(false);
return new PositionStream(await file.OpenReadAsync(cancellationToken).ConfigureAwait(false), file.Properties.Length);

if (hints.HasFlag(FileAccessHints.RandomAccess))
return new BufferedStream(new RandomAccessBlobStream(file), 32 * 1024);
else
return new PositionStream(await file.OpenReadAsync(cancellationToken).ConfigureAwait(false), file.Properties.Length);
}
public override async Task<Stream> CreateFileAsync(string fileName, FileAccessHints hints = FileAccessHints.Default, CancellationToken cancellationToken = default)
{
Expand Down
77 changes: 77 additions & 0 deletions Azure/InedoExtension/FileSystems/RandomAccessBlobStream.cs
@@ -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();
}
}
}
4 changes: 2 additions & 2 deletions Azure/InedoExtension/InedoExtension.csproj
Expand Up @@ -8,8 +8,8 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.2.2" />
<PackageReference Include="Inedo.SDK" Version="1.11.0">
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.2.3" />
<PackageReference Include="Inedo.SDK" Version="1.13.0-ci.4">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>
Expand Down

0 comments on commit 783f67a

Please sign in to comment.