Skip to content

Commit

Permalink
feat(storage): Add WinRT RA/In/Out Stream adapter to/from system Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 9, 2020
1 parent 922632a commit 2f720ca
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Storage.Streams
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented]
#endif
public partial class InputStreamOverStream : global::Windows.Storage.Streams.IInputStream,global::System.IDisposable
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if 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> InputStreamOverStream.ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
#pragma warning disable 114 // new keyword hiding
namespace Windows.Storage.Streams
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented]
#endif
public partial class OutputStreamOverStream : global::Windows.Storage.Streams.IOutputStream,global::System.IDisposable
{
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if 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> OutputStreamOverStream.WriteAsync(IBuffer buffer) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.Foundation.IAsyncOperation<bool> FlushAsync()
{
throw new global::System.NotImplementedException("The member IAsyncOperation<bool> OutputStreamOverStream.FlushAsync() is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public void Dispose()
{
Expand Down
28 changes: 28 additions & 0 deletions src/Uno.UWP/Storage/Streams/IStreamWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#nullable enable

using System;
using System.IO;
using System.Linq;

namespace Windows.Storage.Streams
{
internal interface IStreamWrapper
{
Stream GetStream();
}

interface IInputStreamWrapper
{
IInputStream GetStream();
}

interface IOutputStreamWrapper
{
IOutputStream GetStream();
}

internal interface IRandomStreamWrapper : IInputStreamWrapper, IOutputStreamWrapper
{
new IRandomAccessStream GetStream();
}
}
29 changes: 29 additions & 0 deletions src/Uno.UWP/Storage/Streams/InputStreamOverStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#nullable enable

using System;
using System.IO;
using System.Linq;
using Windows.Foundation;

namespace Windows.Storage.Streams
{
public partial class InputStreamOverStream : IInputStream, IDisposable, IStreamWrapper
{
private readonly Stream _stream;

internal InputStreamOverStream(Stream stream)
{
_stream = stream;
}

Stream IStreamWrapper.GetStream() => _stream;

/// <inheritdoc />
public void Dispose()
=> _stream.Dispose();

/// <inheritdoc />
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
=> _stream.ReadAsync(buffer, count, options);
}
}
33 changes: 33 additions & 0 deletions src/Uno.UWP/Storage/Streams/OutputStreamOverStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#nullable enable

using System;
using System.IO;
using System.Linq;
using Windows.Foundation;

namespace Windows.Storage.Streams
{
public partial class OutputStreamOverStream : IOutputStream, IDisposable, IStreamWrapper
{
private readonly Stream _stream;

public OutputStreamOverStream(Stream stream)
{
_stream = stream;
}

Stream IStreamWrapper.GetStream() => _stream;

/// <inheritdoc />
public void Dispose()
=> _stream.Dispose();

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

/// <inheritdoc />
public IAsyncOperation<bool> FlushAsync()
=> _stream.FlushAsyncOp();
}
}
75 changes: 75 additions & 0 deletions src/Uno.UWP/Storage/Streams/RandomAccessStreamOverStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#nullable enable

using System;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using Windows.Foundation;
using Windows.Storage.Streams;
using Uno;

namespace Windows.Storage.Streams
{
public partial class RandomAccessStreamOverStream : IRandomAccessStream, IInputStream, IOutputStream, IDisposable, IStreamWrapper
{
private readonly Stream _stream;

internal RandomAccessStreamOverStream(Stream stream)
{
_stream = stream;
}

Stream IStreamWrapper.GetStream() => _stream;

/// <inheritdoc />
public bool CanRead => _stream.CanRead;

/// <inheritdoc />
public bool CanWrite => _stream.CanWrite;

/// <inheritdoc />
public ulong Position => (ulong)_stream.Position;

/// <inheritdoc />
public ulong Size
{
get => (ulong)_stream.Length;
set => throw new NotSupportedException();
}

public IInputStream GetInputStreamAt(ulong position)
{
Seek(position);
return this;
}

public IOutputStream GetOutputStreamAt(ulong position)
{
Seek(position);
return this;
}

public void Seek(ulong position)
=> _stream.Seek((long)position, SeekOrigin.Begin);

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

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

/// <inheritdoc />
public IAsyncOperation<bool> FlushAsync()
=> _stream.FlushAsyncOp();

/// <inheritdoc />
public IRandomAccessStream CloneStream()
=> throw new NotSupportedException($"Cannot clone a {nameof(RandomAccessStreamOverStream)}");

/// <inheritdoc />
public virtual void Dispose()
=> _stream.Dispose();
}
}
75 changes: 75 additions & 0 deletions src/Uno.UWP/Storage/Streams/StreamOverInputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#nullable enable

using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Windows.Storage.Streams
{
internal class StreamOverInputStream : Stream, IInputStreamWrapper
{
private readonly IInputStream _raStream;
private readonly int _bufferSize;

public StreamOverInputStream(IInputStream raStream, int bufferSize)
{
_raStream = raStream;
_bufferSize = bufferSize;
}

IInputStream IInputStreamWrapper.GetStream() => _raStream;

/// <inheritdoc />
public override bool CanRead => true;

/// <inheritdoc />
public override bool CanSeek => false;

/// <inheritdoc />
public override bool CanWrite => false;

/// <inheritdoc />
public override long Length => throw new NotSupportedException("Cannot get Length of an input stream.");

/// <inheritdoc />
public override long Position
{
get => throw new NotSupportedException("Cannot Seek an input stream.");
set => throw new NotSupportedException("Cannot get Position of an input stream.");
}

/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException("Cannot Seek an input stream.");

/// <inheritdoc />
public override void SetLength(long value)
=> throw new NotSupportedException("Cannot SetLength of a input stream.");

/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
=> ReadAsync(buffer, offset, count, CancellationToken.None).Result;

/// <inheritdoc />
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> _raStream.ReadAsync(buffer, offset, count, cancellationToken);

/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
=> WriteAsync(buffer, offset, count, CancellationToken.None).Wait();

/// <inheritdoc />
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> throw new NotSupportedException("Cannot Write an input stream.");

/// <inheritdoc />
public override void Flush()
=> FlushAsync(CancellationToken.None).Wait();

/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
=> throw new NotSupportedException("Cannot Flush an input stream.");
}
}
75 changes: 75 additions & 0 deletions src/Uno.UWP/Storage/Streams/StreamOverOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#nullable enable

using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Windows.Storage.Streams
{
internal class StreamOverOutputStream : Stream, IOutputStreamWrapper
{
private readonly IOutputStream _raStream;
private readonly int _bufferSize;

public StreamOverOutputStream(IOutputStream raStream, int bufferSize)
{
_raStream = raStream;
_bufferSize = bufferSize;
}

IOutputStream IOutputStreamWrapper.GetStream() => _raStream;

/// <inheritdoc />
public override bool CanRead => false;

/// <inheritdoc />
public override bool CanSeek => false;

/// <inheritdoc />
public override bool CanWrite => true;

/// <inheritdoc />
public override long Length => throw new NotSupportedException("Cannot get Length of an output stream.");

/// <inheritdoc />
public override long Position
{
get => throw new NotSupportedException("Cannot Seek an output stream.");
set => throw new NotSupportedException("Cannot get Position of an output stream.");
}

/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException("Cannot Seek an output stream.");

/// <inheritdoc />
public override void SetLength(long value)
=> throw new NotSupportedException("Cannot SetLength of an output stream.");

/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
=> ReadAsync(buffer, offset, count, CancellationToken.None).Result;

/// <inheritdoc />
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> throw new NotSupportedException("Cannot Read an output stream.");

/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
=> WriteAsync(buffer, offset, count, CancellationToken.None).Wait();

/// <inheritdoc />
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> _raStream.WriteAsync(buffer, offset, count, cancellationToken);

/// <inheritdoc />
public override void Flush()
=> FlushAsync(CancellationToken.None).Wait();

/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
=> _raStream.FlushAsync().AsTask(cancellationToken);
}
}
Loading

0 comments on commit 2f720ca

Please sign in to comment.