-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9622 from workgroupengineering/features/Core/InMe…
…moryRandomAccessStream feat: InMemoryRandomAccessStream
- Loading branch information
Showing
3 changed files
with
127 additions
and
14 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Streams/Given_InMemoryRandomAccessStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |