Skip to content

Commit

Permalink
feat(storage): Complete removal of direct file system access in FileIO
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 9, 2020
1 parent bc9540e commit f913d02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 20 additions & 14 deletions src/Uno.UWP/Storage/FileIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Windows.Foundation;
using Windows.Storage.Streams;
using System.Collections.Generic;
using System.Threading;
using Uno.Extensions;
using UwpUnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding;
using UwpBuffer = Windows.Storage.Streams.Buffer;
Expand Down Expand Up @@ -256,14 +257,15 @@ private static async Task WriteBytesTaskAsync(IStorageFile file, byte[] buffer,
throw new ArgumentNullException(nameof(file));
}

using var fs = File.Create(file.Path);
using var fs = await file.OpenStreamForWriteAsync();
await fs.WriteAsync(buffer, 0, buffer.Length);
}

private static async Task<IBuffer> ReadBufferTaskAsync(IStorageFile file)
{
using var fs = File.OpenRead(file.Path);
using var fs = await file.OpenStreamForReadAsync();
var bytes = await fs.ReadBytesAsync();

return new UwpBuffer(bytes);
}

Expand All @@ -275,22 +277,26 @@ private static async Task WriteBufferTaskAsync(IStorageFile file, IBuffer buffer

private static async Task<Encoding> GetEncodingFromFileAsync(IStorageFile file)
{
if (File.Exists(file.Path))
// If the file has a local path, try to not create it
if (file.Path is {} path && !string.IsNullOrWhiteSpace(path) && !File.Exists(path))
{
return Encoding.UTF8;
}

using Stream fileStream = await file.OpenStreamForReadAsync();
var bytes = new byte[2];
if (await fileStream.ReadAsync(bytes, 0, bytes.Length) == 2)
{
using Stream fileStream = await file.OpenStreamForReadAsync();
var bytes = new byte[2];
if (await fileStream.ReadAsync(bytes, 0, bytes.Length) == 2)
if (bytes[0] == 0xff && bytes[1] == 0xfe)
{
if (bytes[0] == 0xff && bytes[1] == 0xfe)
{
return Encoding.Unicode;
}
else if (bytes[0] == 0xfe && bytes[1] == 0xff)
{
return Encoding.BigEndianUnicode;
}
return Encoding.Unicode;
}
else if (bytes[0] == 0xfe && bytes[1] == 0xff)
{
return Encoding.BigEndianUnicode;
}
}

return Encoding.UTF8;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/System.IO/WindowsRuntimeStorageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static async Task<Stream> OpenStreamForWriteAsync(this IStorageFile windo
}
else
{
var writeTransaction = await windowsRuntimeFile.OpenTransactedWriteAsync();
return writeTransaction.AsAutoCommitStream();
var raStream = await windowsRuntimeFile.OpenAsync(FileAccessMode.ReadWrite);
return raStream.AsStreamForWrite();
}
}

Expand Down

0 comments on commit f913d02

Please sign in to comment.