Skip to content

Commit 331b8a9

Browse files
committed
feat: Add DataWriter.StoreAsync
1 parent 0179781 commit 331b8a9

File tree

7 files changed

+161
-94
lines changed

7 files changed

+161
-94
lines changed

src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Streams/Given_DataWriter.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
using System.Threading.Tasks;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
66
using Uno.UI.RuntimeTests.Extensions;
7+
using Windows.Foundation;
8+
using Windows.Storage;
79
using Windows.Storage.Streams;
810
using UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding;
911

10-
namespace Uno.UI.Tests.Windows_Storage.Streams
12+
namespace Uno.UI.RuntimeTests.Tests.Windows_Storage.Streams
1113
{
1214
[TestClass]
1315
public class When_DataWriter
@@ -40,7 +42,7 @@ public async Task When_WriteThenRead()
4042
}
4143

4244
var buffer = writer.DetachBuffer();
43-
var reader = DataReader.FromBuffer(buffer);
45+
var reader = DataReader.FromBuffer(buffer);
4446

4547
try
4648
{
@@ -763,5 +765,36 @@ public void When_WriteGuid_Zero()
763765
var bytes = dataWriter.DetachBuffer().ToArray();
764766
CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, bytes);
765767
}
768+
private string GenerateRandomFileName() => $"{Guid.NewGuid()}.txt";
769+
770+
[TestMethod]
771+
public async Task When_StoreAsync()
772+
{
773+
774+
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
775+
IAsyncOperation<StorageFile> createOp =
776+
localFolder.CreateFileAsync(GenerateRandomFileName(),
777+
CreationCollisionOption.ReplaceExisting);
778+
779+
createOp.Completed = (asyncInfo1, asyncStatus1) =>
780+
{
781+
IStorageFile storageFile = asyncInfo1.GetResults();
782+
IAsyncOperation<IRandomAccessStream> openOp =
783+
storageFile.OpenAsync(FileAccessMode.ReadWrite);
784+
openOp.Completed = (asyncInfo2, asyncStatus2) =>
785+
{
786+
IRandomAccessStream stream = asyncInfo2.GetResults();
787+
DataWriter dataWriter = new DataWriter(stream);
788+
dataWriter.WriteString("Test StoreAsync");
789+
DataWriterStoreOperation storeOp = dataWriter.StoreAsync();
790+
storeOp.Completed = (asyncInfo3, asyncStatus3) =>
791+
{
792+
dataWriter.Dispose();
793+
localFolder.DeleteAsync();
794+
Assert.AreEqual(asyncStatus3, AsyncStatus.Completed);
795+
};
796+
};
797+
};
798+
}
766799
}
767800
}

src/Uno.UWP/Generated/3.0.0.0/Windows.Storage.Streams/DataWriter.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@
22
#pragma warning disable 114 // new keyword hiding
33
namespace Windows.Storage.Streams
44
{
5-
#if false || false || false || false || false || false || false
5+
#if false || false || false || false || false || false || false
66
[global::Uno.NotImplemented]
7-
#endif
8-
public partial class DataWriter : global::Windows.Storage.Streams.IDataWriter,global::System.IDisposable
7+
#endif
8+
public partial class DataWriter : global::Windows.Storage.Streams.IDataWriter, global::System.IDisposable
99
{
1010
// Skipping already declared property UnicodeEncoding
1111
// Skipping already declared property ByteOrder
1212
// Skipping already declared property UnstoredBufferLength
13-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
14-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
15-
public DataWriter( global::Windows.Storage.Streams.IOutputStream outputStream)
16-
{
17-
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Streams.DataWriter", "DataWriter.DataWriter(IOutputStream outputStream)");
18-
}
19-
#endif
2013
// Forced skipping of method Windows.Storage.Streams.DataWriter.DataWriter(Windows.Storage.Streams.IOutputStream)
2114
// Skipping already declared method Windows.Storage.Streams.DataWriter.DataWriter()
2215
// Forced skipping of method Windows.Storage.Streams.DataWriter.DataWriter()
@@ -43,22 +36,23 @@ public DataWriter( global::Windows.Storage.Streams.IOutputStream outputStream)
4336
// Skipping already declared method Windows.Storage.Streams.DataWriter.WriteTimeSpan(System.TimeSpan)
4437
// Skipping already declared method Windows.Storage.Streams.DataWriter.WriteString(string)
4538
// Skipping already declared method Windows.Storage.Streams.DataWriter.MeasureString(string)
46-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
47-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
48-
public global::Windows.Storage.Streams.DataWriterStoreOperation StoreAsync()
49-
{
50-
throw new global::System.NotImplementedException("The member DataWriterStoreOperation DataWriter.StoreAsync() is not implemented in Uno.");
51-
}
52-
#endif
39+
// Skipping already declared method Windows.Storage.Streams.DataWriter.StoreAsync
40+
//#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
41+
// [global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
42+
// public global::Windows.Storage.Streams.DataWriterStoreOperation StoreAsync()
43+
// {
44+
// throw new global::System.NotImplementedException("The member DataWriterStoreOperation DataWriter.StoreAsync() is not implemented in Uno.");
45+
// }
46+
// #endif
5347
// Skipping already declared method Windows.Storage.Streams.DataWriter.FlushAsync()
5448
// Skipping already declared method Windows.Storage.Streams.DataWriter.DetachBuffer()
55-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
49+
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
5650
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
57-
public global::Windows.Storage.Streams.IOutputStream DetachStream()
51+
public global::Windows.Storage.Streams.IOutputStream DetachStream()
5852
{
5953
throw new global::System.NotImplementedException("The member IOutputStream DataWriter.DetachStream() is not implemented in Uno.");
6054
}
61-
#endif
55+
#endif
6256
// Skipping already declared method Windows.Storage.Streams.DataWriter.Dispose()
6357
// Processing: Windows.Storage.Streams.IDataWriter
6458
// Processing: System.IDisposable

src/Uno.UWP/Generated/3.0.0.0/Windows.Storage.Streams/DataWriterStoreOperation.cs

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,35 @@
22
#pragma warning disable 114 // new keyword hiding
33
namespace Windows.Storage.Streams
44
{
5-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
5+
#if false || false || false || false || false || false || false
66
[global::Uno.NotImplemented]
77
#endif
8-
public partial class DataWriterStoreOperation : global::Windows.Foundation.IAsyncOperation<uint>,global::Windows.Foundation.IAsyncInfo
8+
public partial class DataWriterStoreOperation : global::Windows.Foundation.IAsyncOperation<uint>, global::Windows.Foundation.IAsyncInfo
99
{
10-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
11-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
12-
public global::System.Exception ErrorCode
13-
{
14-
get
15-
{
16-
throw new global::System.NotImplementedException("The member Exception DataWriterStoreOperation.ErrorCode is not implemented in Uno.");
17-
}
18-
}
19-
#endif
20-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
21-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
22-
public uint Id
23-
{
24-
get
25-
{
26-
throw new global::System.NotImplementedException("The member uint DataWriterStoreOperation.Id is not implemented in Uno.");
27-
}
28-
}
29-
#endif
30-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
31-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
32-
public global::Windows.Foundation.AsyncStatus Status
33-
{
34-
get
35-
{
36-
throw new global::System.NotImplementedException("The member AsyncStatus DataWriterStoreOperation.Status is not implemented in Uno.");
37-
}
38-
}
39-
#endif
40-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
41-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
42-
public global::Windows.Foundation.AsyncOperationCompletedHandler<uint> Completed
43-
{
44-
get
45-
{
46-
throw new global::System.NotImplementedException("The member AsyncOperationCompletedHandler<uint> DataWriterStoreOperation.Completed is not implemented in Uno.");
47-
}
48-
set
49-
{
50-
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Streams.DataWriterStoreOperation", "AsyncOperationCompletedHandler<uint> DataWriterStoreOperation.Completed");
51-
}
52-
}
53-
#endif
10+
// Skipping already declared ctor DataWriterStoreOperation
11+
// Skipping already declared property ErrorCode
12+
// Skipping already declared property Id
13+
// Skipping already declared property Status
14+
// Skipping already declared property Completed
15+
// Skipping already declared method Windows.Storage.Streams.DataWriterStoreOperation.Close()
16+
// Skipping already declared method Windows.Storage.Streams.DataWriterStoreOperation.Cancel()
17+
// Skipping already declared method Windows.Storage.Streams.DataWriterStoreOperation.GetResults()
18+
// Skipping already declared method System.Void Windows.Storage.Streams.DataWriterStoreOperation.Close()
19+
// Skipping already declared method System.Void Windows.Storage.Streams.DataWriterStoreOperation.Cancel()
20+
// Skipping already declared method System.UInt32 Windows.Storage.Streams.DataWriterStoreOperation.GetResults()
5421
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.Completed.set
5522
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.Completed.get
56-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
57-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
58-
public uint GetResults()
59-
{
60-
throw new global::System.NotImplementedException("The member uint DataWriterStoreOperation.GetResults() is not implemented in Uno.");
61-
}
62-
#endif
23+
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.GetResults
6324
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.Id.get
6425
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.Status.get
6526
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.ErrorCode.get
66-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
67-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
68-
public void Cancel()
69-
{
70-
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Streams.DataWriterStoreOperation", "void DataWriterStoreOperation.Cancel()");
71-
}
72-
#endif
73-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
74-
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
75-
public void Close()
76-
{
77-
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Streams.DataWriterStoreOperation", "void DataWriterStoreOperation.Close()");
78-
}
79-
#endif
27+
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.GetResults()
28+
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.Cancel()
29+
// Forced skipping of method Windows.Storage.Streams.DataWriterStoreOperation.Close()
30+
// Forced skipping of method System.Void Windows.Storage.Streams.DataWriterStoreOperation.Close()
31+
// Forced skipping of method System.Void Windows.Storage.Streams.DataWriterStoreOperation.Cancel()
32+
// Forced skipping of method System.UInt32 Windows.Storage.Streams.DataWriterStoreOperation.GetResults()
33+
8034
// Processing: Windows.Foundation.IAsyncOperation<uint>
8135
// Processing: Windows.Foundation.IAsyncInfo
8236
}

src/Uno.UWP/Generated/3.0.0.0/Windows.Storage.Streams/IDataWriter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public partial interface IDataWriter
3333
// Skipping already declared method Windows.Storage.Streams.IDataWriter.WriteTimeSpan(System.TimeSpan)
3434
// Skipping already declared method Windows.Storage.Streams.IDataWriter.WriteString(string)
3535
// Skipping already declared method Windows.Storage.Streams.IDataWriter.MeasureString(string)
36-
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
37-
global::Windows.Storage.Streams.DataWriterStoreOperation StoreAsync();
38-
#endif
36+
// Skipping already declared method Windows.Storage.Streams.IDataWriter.StoreAsync()
3937
// Skipping already declared method Windows.Storage.Streams.IDataWriter.FlushAsync()
4038
// Skipping already declared method Windows.Storage.Streams.IDataWriter.DetachBuffer()
4139
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__

src/Uno.UWP/Storage/Streams/DataWriter.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Windows.Foundation;
67

@@ -12,6 +13,7 @@ namespace Windows.Storage.Streams
1213
public sealed partial class DataWriter : IDataWriter, IDisposable
1314
{
1415
private MemoryStream _memoryStream = null;
16+
private readonly IOutputStream _outputStream = null;
1517

1618
/// <summary>
1719
/// Creates and initializes a new instance of the data writer.
@@ -21,6 +23,11 @@ public DataWriter()
2123
_memoryStream = new MemoryStream();
2224
}
2325

26+
public DataWriter(IOutputStream outputStream) : this()
27+
{
28+
_outputStream = outputStream;
29+
}
30+
2431
/// <summary>
2532
/// Gets or sets the Unicode character encoding for the output stream.
2633
/// </summary>
@@ -176,8 +183,8 @@ public void WriteGuid(Guid value)
176183
/// Writes a date and time value to the output stream.
177184
/// </summary>
178185
/// <param name="value">The value.</param>
179-
public void WriteDateTime( DateTimeOffset value)
180-
{
186+
public void WriteDateTime(DateTimeOffset value)
187+
{
181188
// Implementation in UWP is using Win32 FileTime format (starting on 1st January 1601)
182189
// As contrary to Win32 however also supports pre-1601 dates
183190
var epoch1601 = new DateTimeOffset(1601, 1, 1, 0, 0, 0, TimeSpan.Zero);
@@ -276,5 +283,30 @@ private void WriteChunkToUnstoredBuffer(byte[] chunk)
276283
}
277284
_memoryStream.Write(chunk, 0, chunk.Length);
278285
}
286+
287+
288+
public DataWriterStoreOperation StoreAsync()
289+
{
290+
var dwStore = new DataWriterStoreOperation();
291+
Task.Run(async () =>
292+
{
293+
try
294+
{
295+
var cancelToken = new CancellationTokenSource();
296+
var inBuffer = _memoryStream.GetBuffer();
297+
await _outputStream.WriteAsync(inBuffer, 0, inBuffer.Length, cancelToken.Token);
298+
await _outputStream.FlushAsync().AsTask();
299+
dwStore.Status = AsyncStatus.Completed;
300+
dwStore.Result = (uint)inBuffer.Length;
301+
dwStore.Completed.Invoke(dwStore, dwStore.Status);
302+
}
303+
catch (Exception ex)
304+
{
305+
dwStore.ErrorCode = ex;
306+
dwStore.Status = AsyncStatus.Error;
307+
}
308+
});
309+
return dwStore;
310+
}
279311
}
280312
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices.WindowsRuntime;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Windows.Foundation;
8+
9+
namespace Windows.Storage.Streams
10+
{
11+
public partial class DataWriterStoreOperation : IAsyncOperation<uint>, IAsyncInfo
12+
{
13+
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
14+
private AsyncOperationCompletedHandler<uint> _onCompleted;
15+
private AsyncStatus _status;
16+
public DataWriterStoreOperation()
17+
{
18+
Status = AsyncStatus.Started;
19+
}
20+
21+
public AsyncOperationCompletedHandler<uint> Completed
22+
{
23+
get { return _onCompleted; }
24+
set
25+
{
26+
_onCompleted = value;
27+
if (Status != AsyncStatus.Started) { value?.Invoke(this, Status); }
28+
}
29+
}
30+
31+
public Exception ErrorCode { get; set; }
32+
33+
public uint Id { get; set; }
34+
35+
public AsyncStatus Status
36+
{
37+
get => _status;
38+
set => _status = value;
39+
}
40+
41+
public void Cancel() => _cts.Cancel();
42+
43+
public void Close()
44+
{
45+
_cts.Cancel();
46+
_cts.Dispose();
47+
}
48+
public uint Result { get; set; }
49+
public uint GetResults()
50+
{
51+
return Status == AsyncStatus.Completed ? Result : 0;
52+
}
53+
}
54+
}

src/Uno.UWP/Storage/Streams/IDataWriter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public partial interface IDataWriter
4747

4848
uint MeasureString(string value);
4949

50+
DataWriterStoreOperation StoreAsync();
51+
5052
IAsyncOperation<bool> FlushAsync();
5153

5254
IBuffer DetachBuffer();

0 commit comments

Comments
 (0)