Skip to content

Commit

Permalink
Merge pull request #9622 from workgroupengineering/features/Core/InMe…
Browse files Browse the repository at this point in the history
…moryRandomAccessStream

feat: InMemoryRandomAccessStream
  • Loading branch information
jeromelaban authored Aug 30, 2022
2 parents 2178afc + e562016 commit ac0c34e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Streams;
namespace Uno.UI.RuntimeTests.Tests.Windows_Storage.Streams;

[TestClass]
public class Given_InMemoryRandomAccessStream
{
[TestMethod]
public async Task When_Open_ReadWrite()
{
var stream = new InMemoryRandomAccessStream();
var direct = stream.AsStream();

await StreamTestHelper.Test(writeTo: direct, readOn: stream);
await StreamTestHelper.Test(writeTo: stream, readOn: direct);
}

[TestMethod]
public async Task When_GetInputStream()
{
var stream = new InMemoryRandomAccessStream();
var inputstream = stream.GetInputStreamAt(0);
var direct = stream.AsStream();

await StreamTestHelper.Test(writeTo: direct, readOn: inputstream);
}

[TestMethod]
public async Task When_GetOutputStream()
{
var stream = new InMemoryRandomAccessStream();
var outputstream = stream.GetOutputStreamAt(0);
var direct = stream.AsStream();

await StreamTestHelper.Test(writeTo: outputstream, readOn: direct);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Storage.Streams
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented]
#endif
public partial class InMemoryRandomAccessStream : global::Windows.Storage.Streams.IRandomAccessStream,global::Windows.Storage.Streams.IOutputStream,global::System.IDisposable,global::Windows.Storage.Streams.IInputStream
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public ulong Size
{
Expand All @@ -21,7 +21,7 @@ public ulong Size
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public bool CanRead
{
Expand All @@ -31,7 +31,7 @@ public bool CanRead
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public bool CanWrite
{
Expand All @@ -41,7 +41,7 @@ public bool CanWrite
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public ulong Position
{
Expand All @@ -51,7 +51,7 @@ public ulong Position
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public InMemoryRandomAccessStream()
{
Expand All @@ -61,29 +61,29 @@ public InMemoryRandomAccessStream()
// Forced skipping of method Windows.Storage.Streams.InMemoryRandomAccessStream.InMemoryRandomAccessStream()
// Forced skipping of method Windows.Storage.Streams.InMemoryRandomAccessStream.Size.get
// Forced skipping of method Windows.Storage.Streams.InMemoryRandomAccessStream.Size.set
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Storage.Streams.IInputStream GetInputStreamAt( ulong position)
{
throw new global::System.NotImplementedException("The member IInputStream InMemoryRandomAccessStream.GetInputStreamAt(ulong position) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Storage.Streams.IOutputStream GetOutputStreamAt( ulong position)
{
throw new global::System.NotImplementedException("The member IOutputStream InMemoryRandomAccessStream.GetOutputStreamAt(ulong position) is not implemented in Uno.");
}
#endif
// Forced skipping of method Windows.Storage.Streams.InMemoryRandomAccessStream.Position.get
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void Seek( ulong position)
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Streams.InMemoryRandomAccessStream", "void InMemoryRandomAccessStream.Seek(ulong position)");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Storage.Streams.IRandomAccessStream CloneStream()
{
Expand All @@ -92,28 +92,28 @@ public void Seek( ulong position)
#endif
// Forced skipping of method Windows.Storage.Streams.InMemoryRandomAccessStream.CanRead.get
// Forced skipping of method Windows.Storage.Streams.InMemoryRandomAccessStream.CanWrite.get
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void Dispose()
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Storage.Streams.InMemoryRandomAccessStream", "void InMemoryRandomAccessStream.Dispose()");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.IAsyncOperationWithProgress<global::Windows.Storage.Streams.IBuffer, uint> ReadAsync( global::Windows.Storage.Streams.IBuffer buffer, uint count, global::Windows.Storage.Streams.InputStreamOptions options)
{
throw new global::System.NotImplementedException("The member IAsyncOperationWithProgress<IBuffer, uint> InMemoryRandomAccessStream.ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync( global::Windows.Storage.Streams.IBuffer buffer)
{
throw new global::System.NotImplementedException("The member IAsyncOperationWithProgress<uint, uint> InMemoryRandomAccessStream.WriteAsync(IBuffer buffer) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false || false || false || false || false || false || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.IAsyncOperation<bool> FlushAsync()
{
Expand Down
75 changes: 75 additions & 0 deletions src/Uno.UWP/Storage/Streams/InMemoryRandomAccessStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#nullable enable
using System;
using System.IO;
using Windows.Foundation;

namespace Windows.Storage.Streams;

public partial class InMemoryRandomAccessStream: IStreamWrapper
{
private readonly MemoryStream _stream;
public InMemoryRandomAccessStream() =>
_stream = new();

private InMemoryRandomAccessStream(MemoryStream stream) =>
_stream = stream;

public ulong Size
{
get => (ulong)_stream.Length;
set => _stream.SetLength((long)value);
}

public bool CanRead => _stream.CanRead;

public bool CanWrite => _stream.CanWrite;

public ulong Position => (ulong)_stream.Position;

public void Seek(ulong position) => _stream.Position = (long)position;

public IRandomAccessStream CloneStream()
{
var destination = new MemoryStream();
_stream.Position = 0;
_stream.CopyTo(destination);
return new InMemoryRandomAccessStream(destination);
}

public void Dispose() => _stream.Dispose();

Stream IStreamWrapper.FindStream() => _stream;

public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) =>
_stream.ReadAsyncOperation(buffer, count, options);

public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) =>
_stream.WriteAsyncOperation(buffer);

public IAsyncOperation<bool> FlushAsync() =>
_stream.FlushAsyncOperation();

public IInputStream GetInputStreamAt(ulong position)
{
if (!CanRead)
{
throw new NotSupportedException("It has not been opened for read.");
}

_stream.Seek((long)position, SeekOrigin.Begin);

return new InputStreamOverStream(_stream);
}

public IOutputStream GetOutputStreamAt(ulong position)
{
if (!CanWrite)
{
throw new NotSupportedException("It has not been opened for write.");
}

_stream.Seek((long)position, SeekOrigin.Begin);

return new OutputStreamOverStream(_stream);
}
}

0 comments on commit ac0c34e

Please sign in to comment.