Skip to content

Commit

Permalink
feat(storage): Add a Buffer.DefaultCapacity
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 9, 2020
1 parent 789ae3f commit e2c49f8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Uno.UWP/Storage/StorageFile.Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Windows.Storage.FileProperties;
using Windows.Storage.Streams;
using Buffer = Windows.Storage.Streams.Buffer;

namespace Windows.Storage
{
Expand Down Expand Up @@ -71,7 +72,7 @@ public virtual async Task CopyAndReplace(CancellationToken ct, IStorageFile targ
using (var src = await Owner.OpenStreamForReadAsync())
using (var dst = await target.OpenStreamForReadAsync())
{
await src.CopyToAsync(dst, 8192, ct);
await src.CopyToAsync(dst, Buffer.DefaultCapacity, ct);
}
}

Expand All @@ -86,7 +87,7 @@ public virtual async Task MoveAndReplace(CancellationToken ct, IStorageFile targ
using (var src = await Owner.OpenStreamForReadAsync())
using (var dst = await target.OpenStreamForReadAsync())
{
await src.CopyToAsync(dst, 8192, ct);
await src.CopyToAsync(dst, Buffer.DefaultCapacity, ct);
}

await Delete(ct, StorageDeleteOption.PermanentDelete);
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.UWP/Storage/StorageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public sealed partial class StorageFile : StorageItem, IStorageFile
public static IAsyncOperation<StorageFile> GetFileFromPathAsync(string path)
=> AsyncOperation.FromTask(async ct => new StorageFile(new Local(path)));

internal static StorageFile GetFileFromPath(string path)
=> new StorageFile(new Local(path));

internal static StorageFile GetFileFromLocalPath(string path)
=> new StorageFile(new Local(path));

Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/Storage/StreamedFileDataRequestedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Windows.Storage
// {
// method ??= HttpMethod.Get;
// client ??= new HttpClient();

// return req => Task.Run(() => FetchAsync(req, uri, method, client, onReady, req.CancellationToken));
// }

Expand Down Expand Up @@ -59,7 +59,7 @@ namespace Windows.Storage
// if (response.Content is { } content)
// {
// var responseStream = await content.ReadAsStreamAsync();
// await responseStream.CopyToAsync(req.AsStreamForWrite(), 8192, ct);
// await responseStream.CopyToAsync(req.AsStreamForWrite(), global::Windows.Storage.Streams.Buffer.DefaultCapacity, ct);
// }
// }
// catch (Exception e)
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UWP/Storage/Streams/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Windows.Storage.Streams
{
public partial class Buffer : IBuffer
{
/// <summary>
/// A default length to use for buffer copy when none specified
/// </summary>
internal const int DefaultCapacity = 1024 * 1024; // 1M

private readonly Memory<byte> _data;

internal static Buffer Cast(IBuffer impl)
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/Storage/Streams/StreamedUriDataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ private async Task Download(HttpResponseMessage response, CancellationToken ct)
using var responseStream = await response.Content.ReadAsStreamAsync();
using var file = File.OpenWeak(FileAccess.Write);

var buffer = new byte[8192];
var buffer = new byte[Buffer.DefaultCapacity];
int read;
while ((read = await responseStream.ReadAsync(buffer, 0, 8192, ct)) > 0)
while ((read = await responseStream.ReadAsync(buffer, 0, Buffer.DefaultCapacity, ct)) > 0)
{
await file.WriteAsync(buffer, 0, read, ct);

Expand Down

0 comments on commit e2c49f8

Please sign in to comment.